Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Dependency injection in episerver asp.net web forms site

Vote:
 

Hi.

In my project I want to use Dependency injection (Autofac). I found nice example (with setup dependency for IClinet (EPiServer Find)) on github https://github.com/200OK/TrufflerSample/blob/master/TrufflerSample/Global.asax.cs
for asp.net mvc, but I couldn't find example for asp.net webfroms.

I saw example http://joelabrahamsson.com/introducing-epimvp-a-framework-for-using-web-forms-mvp-with-episerver-cms/ but it requires a big change in my project.
What can you advise
Thanks in advance

 

#69611
Mar 29, 2013 15:54
Vote:
 

Hi

I haven´t used Autofac before, but my guess is that in order to use dependency injection in web forms code-behind, you need to use property injection.

protected override void OnInit(EventArgs e)
    {
        var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
        var cp = cpa.ContainerProvider;
        cp.RequestLifetime.InjectProperties(this);

        base.OnInit(e);
    }
publicIServiceClassServiceClass{get;set;}


(notice the public property)

The rest should be pretty much the same. Register on start up etc.

Hope this helps

Frederik 

#69616
Mar 31, 2013 8:56
Vote:
 

Nice, Thank's Frederik Vig. I use that with configuration https://code.google.com/p/autofac/wiki/WebFormsIntegration. But In general, it would be perfectly hide OnInit functionality. What better way to?

Thanks in advance

#69625
Mar 31, 2013 22:43
Vote:
 

I guess yu could use an HttpModule or IInitializableModule instead of a base page. I personally prefer the base page though.

#69627
Apr 01, 2013 9:12
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.