Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 use different service containers per route, application part or feature

I am trying to figure out how to possibly configure the use of different Service Containers for different routes/endpoints in a ASP.NET Core (.NET 5) application.

The background is that we have an application on ASP.NET 4.x that allows for plugins to register their own routes/endpoints (under configurable prefixes - that is also something I need to figure out in ASP.NET Core) - each such plugin gets its own Castle Windsor container where it can register services and other things with services we pre-register in that container. This container is also used to activate any ASP.NET WebApi controllers for that particular plugin.

We have done this by building some custom routing on top of ASP.NET 4 WebAPI/MVC where the route has information about which context it originates from (The core Application or a Plugin) as well as automatically prepending the prefix.

However, ASP.NET Core WebAPI/MVC has changed allot of things and I can't quite figure out how to get to the same result.

Because this is driven by plugins, it doesn't have to be done on a PER route basis obviously, instead PER plugin is good enough (Which would be a group of routes under the same prefix), so I have been trying to figure out of any of these provided a viable path for me:

  • Application Parts
  • Feature Providers (that container application parts)
  • Controller activators

I did try out a combination of all the above where a ApplicationPart would return custom implementation of types, there by letting us carry extra info about the controller to the IControllerActivator. However ASP.NET Core requires RuntimeType's rather than just any "TypeInfo" implementation.

like image 589
Jens Avatar asked Feb 02 '26 19:02

Jens


1 Answers

I had a similar question and ran across this blog post which provides an example of using different IServiceCollections for different route prefixes. I think you can adapt it to get what you want.

Apparently the author also published the code in the WebAPIContrib.Core library (nuget package here).

like image 84
Dan Ott Avatar answered Feb 05 '26 07:02

Dan Ott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!