3

Google/MyBrain are failing me. Without using a framework (which I'll never get past my colleagues), how do you inject a dependency into an HTTPModule given that you (the programmer) are not in control of creating the instance?

Are we into hacking up custom web.config sections + reflection or is there something cleaner I'm not seeing?

e.g. using Karl Seguin's example module as a base, and assuming the implementation of an ILogger. .Net 2.0 fwiw

public class ErrorModule : IHttpModule
{
    private ILogger injectedLogger; //how to set this?

    public void Init(HttpApplication application)
    {
        application.Error += (new EventHandler(this.application_Error));
    }

    public void Dispose() { /* required by IHttpModule */ }

    private void application_Error(object sender, EventArgs e)
    {
        this.injectedLogger.doStuff(e.ExceptionObject as Exception);
    }
}

It's things like this make me realise how much I despise MSDN.

annakata
  • 74,572
  • 17
  • 113
  • 180

3 Answers3

5

I've stumbled on a programatic solution. Eliminating web.config references entirely, you can add a module into global.asax if you're careful with the life-cycle.

In global.asax add:

public static ErrorModule myErrorModule;

public override void Init()
{
    base.Init();
    myErrorModule = new ErrorModule(new LogImplementer());
    myErrorModule.Init(this);
}

where "LogImplementer" implements ILogger, and just add a constructor to the httpmodule:

public ErrorModule(ILogger injected)
{
    this.Logger = injected;
}

There are some health warnings. You have to be really careful about where you do this int he global.asax (I'm pretty sure that Init() is right, but I'm not certain) and the module isn't added to the readonly HttpApplication.Modules collection. Other than that, works beautifully.

annakata
  • 74,572
  • 17
  • 113
  • 180
  • Accepting my own answer just to improve my accept % - not at all silly :/ – annakata Nov 04 '10 at 11:11
  • thanks for tip, works well. Not sure myErrorModule needs to be static. At least for me local variable works as well. – Jaroslav Urban Feb 17 '11 at 16:36
  • 1
    @ParthShah: I completely disagree. This is exactly DI for the module which was the issue. If you're complaining about the use of global.asax and lack of a DI framework well that's a given in the question and actually irrelevant to the point as far as ErrorModule is concerned. – annakata Sep 22 '14 at 09:05
  • @annakata from the perspective of constructing ErrorModule, it is definitely DI. From the perspective of using ErrorModule, (say some other class needs an instance of ErrorModule for the construction of it), the public static member in global.asax solution turns into a Service Locator pattern then for the other class who needs it. However, while writing this comment, I realized the odds of anyone need an instance of ErrorModule, which is a HttpModule, are super low. Thus I decided to remove my earlier comment. – Parth Shah Sep 24 '14 at 02:38
0

Please take a look at my answer here on similar question:

IoC Dependancy injection into Custom HTTP Module - how? (ASP.NET)

Community
  • 1
  • 1
andrey.tsykunov
  • 2,896
  • 2
  • 32
  • 21
0

You can use a very simple one-class ioc An IoC Container In 15 Minutes and 33 Lines

loraderon
  • 4,658
  • 2
  • 32
  • 35
  • Thanks for that - Make my own framework, eh? I have a working solution right now, but I'll review that link to see if it's a better one – annakata Jan 14 '09 at 14:31