Reduction in dependency on app_start


Is it possible to access the IOC container in a RequestFilter? The use-case I have is I have multiple instances of the same codebase, but within global.asax.cs, each webapp loads a different “ITenant” implementation based on a value in web.config/azure webapp config.

The downside of this setup is that I have a webapp for each tenant running in Azure (I mentioned this in a SO post) As we grow, the memory footprint on azure goes up linearly with each new customer we onboard, and bearing in mind each customer only has a handful of users, it’s not efficient.

Before migrating to .net core/docker, I want to get the “all tenants running in a single web-app” working. In order to do this, I need to move what I’m currently doing in global.asax.cs to a RequestFilter, which executes as each request comes in; specifically:

  1. Examine Host/Domain from incoming Request.
  2. Resolve that host against which tenant should be loaded (e.g. from Redis/cache).
  3. Create instance of correct ITenant via Activator.CreateInstance
  4. Register ITenant in Funq (each Tenant has differing dependencies).

Is this possible? Is this bad for performance? I know I can resolve dependencies via GetResolver() but I can’t find a way of gaining access to Funq container.

If this isn’t possible, I can register a type that stores all ITenant implementations and use this to resolve the ITenant and attach to the Custom UserSession, but the refactoring involved would be enormous.


If I understand correctly your question you can use static HostContext.Resolve<YourType>() in methods are located outside of the Service-derived classes (where can be used GetResolver())

Not quite; I need to regsiter and auto-wire a new instance within a RequestFilter. Is this possible?

No the IOC should be immutable after AppHost.Configure(), it’s not ThreadSafe to modify it at runtime.