How to inject constructor/properties properly in ASP.NET HttpModule with Simple Injector

前端 未结 1 373
野的像风
野的像风 2021-01-15 11:46

I have a class called UsersOnlineModule that is being created from an IHttpModul. Within this class I would like to have two properties injected, I

相关标签:
1条回答
  • 2021-01-15 11:56

    The integration guide shows how to use a HttpModule to initialize HttpHandlers. A HttpModule however doesn't get created per request; it is created once for the application and will register to the HttpApplication's events when its initialize method is called.

    Since a module is a singleton, you should not inject dependencies into your module. You should resolve dependencies per request, and since the HttpModule can't be configured and is not a dependency of its own, you will have to call back into the container from within your HttpModule. There's really no other way, but the trick here is to minimize the amount of code needed. Example:

    public class UsersOnlineModule : IHttpModule
    {
        public void Init(HttpApplication context) {
            context.PreRequestHandlerExecute += (s, e) => {
                var handler = Global.GetInstance<UsersOnlineHandler>();
                handler.Handle();
            };
        }
    }
    

    In this example, the UsersOnlineModule does do nothing more than resolving one single service and call its method on it. This service (UsersOnlineHandler in this case) should capture all logic and dependencies needed. So in other words, your HttpModule becomes a Humble Object and all logic is extracted into the UsersOnlineHandler:

    public class UsersOnlineHandler
    {
        private readonly ITenantStore tenantStore;
        private readonly ICacheManager cm;
    
        public UsersOnlineHandler(ITenantStore tenantStore, ICacheManager cm) {
            this.tenantStore = tenantStore;
            this.cm = cm;
        }
    
        public void Handle() {
           // logic here
        }
    }
    

    WARNING: Be sure not to store any dependencies into class or instance fields or properties of your HttpModule, because that might cause a dependency to become a Captive Dependency. Instead, as described above, just resolve, use, and forget about that dependency, all inside that method call. Do not store it.

    0 讨论(0)
提交回复
热议问题