首页 文章

Onion Architecture中的Autofac WebForms

提问于
浏览
0

我正在关注洋葱架构,其Bootstrapper部件是用 Autofac 构建的 .

架构如下:

  • 核心

  • DependencyInjection(Autofac在这里)

  • 服务

  • 演讲(MVC 5)

  • 测试

我需要一些WebForm.aspx页面来显示我的报告 . 所以我按照WebForms与Autofac集成链接的说明进行操作:http://docs.autofac.org/en/latest/integration/webforms.html

以下是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);
        }
    }
}

在DependencyInjection的App.Config中,我添加了:

<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>

它仍然没有用 . MyService仍然是 null . 当我将App.Config设置添加到我的演示文稿的Web.Config时,我收到以下错误:

此模块要求HttpApplication(全局应用程序类)实现IContainerProviderAccessor

1 回答

  • 0

    Autofac ASP.net WebForm集成要求 HttpApplication 实现 IContainerProviderAccessor .

    如果要将 WebActivatorIocConfig 类一起使用 . Global.asax类仍然必须实现 IContainerProviderAccessor 接口以让HttpModule找到Autofac容器 .

    您可以依靠 IocConfig 实现来实现此接口

    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; }
      }
    }
    

    并且您的 IocConfig 不需要实现 IContainerProviderAccessor 但需要为 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; }
            }
    

相关问题