We have a common Class Library Project which contains the Startup.cs and some of the common middle wares used across all the Applications(ASP .Net Core Web Applications which are dependent on the common Class library).
Th Startup.cs adds these common middle wares to the application's request pipeline and also registers the common framework Services to the IServiceCollection.
Each of the Web Application projects define their own Controllers. We want to use this same Startup.cs from all our applications.
Now, there are two issues we are encountering
- The Controllers in the Web application project are not discovered due to the Startup.cs being defined inside another Assembly( the common class library -Similar to the issue - http://stackoverflow.com/questions/37725934/asp-net-core-mvc-controllers-in-separate-assembly ). I had to put a workaround to store the reference to the Web Application Assembly as a static variable (http://stackoverflow.com/questions/40483189/what-is-the-equivalent-of-assembly-getentryassembly-in-net-core) and call the following in Startup.cs
services.AddMvc().AddApplicationPart(AssemblyHelper.GetEntryAssembly()).AddControllersAsServices();
This is just a workaround and I would like to understand the right approach of handling this case
- The second issue is with Dependency injection. As I mentioned earlier, the framework services in the Class library are registered in the Startup.cs and they work fine. However each of the Web Application projects could have their own Services and we would like to register them to the same IServiceCollection. This way the Controllers can inject both the framework services as well as the ones registered in the web application project itself. The problem is, how can I get hold of the IServiceCollection object in my Web Application project. I could not come across the right approach for implementing this so far and had to put another workaround to get this working temporarily.
Please let me know the right approach of implementing this.
Thanks,
Ranjith