简体   繁体   中英

Autofac WebForms in Onion Architecture

I am following onion architecture whose Bootstrapper part is build with Autofac .

Architecture is as follows:

  1. Core
  2. DependencyInjection (Autofac is here)
  3. Service
  4. Presentation (MVC 5)
  5. Test

I needed some WebForm.aspx pages to show my reports. so I am following the instructions given the the link for integration of WebForms with Autofac: http://docs.autofac.org/en/latest/integration/webforms.html

Here is the code from DependencyInjection:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]
namespace DependencyInjection.App_Start
{
    public class IocConfig : 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; }
        }

        public static void RegisterDependencies()
        {
            // Build up your application container and register your dependencies.
            var builder = new ContainerBuilder();

            // Register your MVC controllers. (MvcApplication is the name of
            // the class in Global.asax.)
            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            // OPTIONAL: Register model binders that require DI.
            builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
            builder.RegisterModelBinderProvider();

            // OPTIONAL: Register web abstractions like HttpContextBase.
            builder.RegisterModule<AutofacWebTypesModule>();

            // OPTIONAL: Enable property injection in view pages.
            builder.RegisterSource(new ViewRegistrationSource());



            // OPTIONAL: Enable property injection into action filters.
            builder.RegisterFilterProvider();

 builder.RegisterType<MyService().As<IMyService().InstancePerRequest();

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

In App.Config of DependencyInjection, I have added:

<system.web>
    <httpModules>      
      <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>
    <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>

Still it is not working. MyService is still null . When I add the App.Config setting into Web.Config of my Presentation, I got the following error:

This module requires that the HttpApplication (Global Application Class) implements IContainerProviderAccessor

Autofac ASP.net WebForm integration requires that the HttpApplication implements IContainerProviderAccessor .

If you want to use WebActivator with your IocConfig class. The Global.asax class still have to implement the IContainerProviderAccessor interface to let the HttpModule find the Autofac container.

You can implement this interface by relying on the IocConfig implementation

Global.asax.cs

public class Global : HttpApplication, IContainerProviderAccessor
{
  // Instance property that will be used by Autofac HttpModules
  // to resolve and inject dependencies.
  public IContainerProvider ContainerProvider
  {
    get { return IocConfig.ContainerProvider; }
  }
}

and your IocConfig doesn't need to implement IContainerProviderAccessor but need to have a public static property for the IContainerProvider

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig), 
                                                   "RegisterDependencies")]

namespace DependencyInjection.App_Start
{
    public class IocConfig : 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 static IContainerProvider ContainerProvider
        {
            get { return _containerProvider; }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM