You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To get Autofac integrated with web forms you need to reference the web forms integration NuGet package, add the modules to web.config, and implement IContainerProviderAccessor on your Global application class.
Add the modules to web.config:
<configuration>
<system.web>
<httpModules>
<!-- This section is used for IIS6 -->
<addname="ContainerDisposal"type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
<addname="PropertyInjection"type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"/>
</httpModules>
</system.web>
<system.webServer>
<!-- This section is used for IIS7 -->
<modules>
<addname="ContainerDisposal"type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"preCondition="managedHandler"/>
<addname="PropertyInjection"type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"preCondition="managedHandler"/>
</modules>
</system.webServer>
</configuration>
Implement IContainerProviderAccessor:
publicclassGlobal:HttpApplication,IContainerProviderAccessor{// Provider that holds the application container.staticIContainerProvider_containerProvider;// Instance property that will be used by Autofac HttpModules// to resolve and inject dependencies.publicIContainerProviderContainerProvider{get{return_containerProvider;}}protectedvoidApplication_Start(objectsender,EventArgse){// Build up your application container and register your dependencies.varbuilder=newContainerBuilder();builder.RegisterType<SomeDependency>();// ... continue registering dependencies...// Once you're done registering things, set the container// provider up with your registrations._containerProvider=newContainerProvider(builder.Build());}}