.net autofac Web Forms

autofac.web usually injects properties into Page or user/custom controls

public class Article:System.Web.UI.Page
{
       public IArticleService ArticleService{get;set;} 
        //PageLoad event or other code places directly call ArticleService.Get....
}

 

1 Install autofac.web nuget way

 

2 Configuration in web.config

<configuration>
  <system.web>
    <httpModules>
      <!-- This section is used for IIS6 -->
      <add
        name="ContainerDisposal"
        type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
      <add
        name="PropertyInjection"
        type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"/>
    </httpModules>
  </system.web>
  <system.webServer>
    <!-- This section is used for IIS7 -->
    <modules>
      <add
        name="ContainerDisposal"
        type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"
        preCondition="managedHandler"/>
      <add
        name="PropertyInjection"
        type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web"
        preCondition="managedHandler"/>
    </modules>
  </system.webServer>
</configuration>

ContainerDisposalModule releases related objects and autofacScope created by autofac at the end of the page release request

The PropertyInjectionModule guesses that this is dependency injection after the page is created. Another type, UnsetPropertyInjectionModule, can also be used here. The only difference is that if we have already assigned the dependent properties in the constructor, the former method is to continue dependency injection, while the latter does not. Only one of the two can be chosen

 

3 Implement IContainerProviderAccessor

Sometimes we want to manually resolve the specified interface or type from the container in the code. At this time, we need to access the container or life cycle (see related articles for the global container and life cycle). The purpose of implementing IContainerProviderAccessor is to provide us with an access container or a point in the life cycle

In autofac, Global is usually used to implement this interface

public class Global : HttpApplication, IContainerProviderAccessor
{
  // Provider that holds the application container.
  static IContainerProvider _containerProvider;

  // Instance property that will be used by Autofac HttpModules
  // to resolve and inject dependencies.
  public IContainerProvider ContainerProvider
  {
    get { return _containerProvider; }
  }

  protected void Application_Start(object sender, EventArgs e)
  {
    // Build up your application container and register your dependencies.
    var builder = new ContainerBuilder();
    builder.RegisterType<SomeDependency>();
    // ... continue registering dependencies...

    // Once you're done registering things, set the container
    // provider up with your registrations.
    _containerProvider = new ContainerProvider(builder.Build());
  }
}

 This way we can access the global container or lifecycle in our code

var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
var cp = cpa.ContainerProvider;
cp.RequestLifetime....//The current request life cycle
cp.ApplicationContainer //Global container

 

4.1 Automatic injection

public class Article:System.Web.UI.Page
{
       public IArticleService ArticleService{get;set;} 
        //PageLoad event or other code places directly call ArticleService.Get....
}

Note: If our user control is also registered to autofac, then dependency injection will also be performed, but if we create a user control through code or similar Repeater traversal, automatic injection will not be performed. At this time, you can refer to the following manual injection.

 

4.2 Manual injection

var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
var cp = cpa.ContainerProvider;
cp.RequestLifetime.InjectProperties(objectToSet);

 

4.3 Selective injection

Sometimes (such as adding autofac to an existing project), we have exposed properties, but some we need to inject and some don't need to be injected. Use another injection HttpModule, with Attribute for selective injection

<configuration>
  <system.web>
    <httpModules>
      <!-- This section is used for IIS6 -->
      <add
        name="ContainerDisposal"
        type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
      <add
        name="AttributedInjection"
        type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web"/>
    </httpModules>
  </system.web>
  <system.webServer>
    <!-- This section is used for IIS7 -->
    <modules>
      <add
        name="ContainerDisposal"
        type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"
        preCondition="managedHandler"/>
      <add
        name="AttributedInjection"
        type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web"
        preCondition="managedHandler"/>
    </modules>
  </system.webServer>
</configuration>

 At this point all the properties exposed by the page will not be injected by default, unless InjectPropertiesAttribute or InjectUnsetPropertiesAttribute is added, the same is true for user controls

 

4.4 Do not use HttpModule injection

The HttpModule method will inject all pages and user controls used. If we only want to inject some pages, we can implement it by defining the base class Page

protected void Page_PreInit(object sender, EventArgs e)
{
  var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
  var cp = cpa.ContainerProvider;
  cp.RequestLifetime.InjectProperties(this);
}

 

4.5 Custom Dependency Injection Module

Define an HttpModule that implements Autofac.Integration.Web.DependencyInjectionModule

protected abstract IInjectionBehaviour GetInjectionBehaviourForHandlerType(Type handlerType);

The returned IInjectionBehaviour can be one of the predefined NoInjection, PropertyInjection, or UnsetPropertyInjection properties; or a custom implementation of the IInjectionBehaviour interface.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326143433&siteId=291194637