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
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.