2017-07-24 4 views
0

Ich folge Zwiebel Architektur, deren Bootstrapper Teil mit Autofac gebaut wird.Autofac WebForms in Onion Architektur

Architektur ist wie folgt:

  1. Kern
  2. Dependency Injection (Autofac ist hier)
  3. Dienst
  4. Präsentation (MVC 5)
  5. -Test

brauchte ich einige WebForm.aspx-Seiten zum Anzeigen meiner Berichte. so dass ich nach den Anweisungen gegeben die die Verbindung zur Integration von WebForms mit Autofac: http://docs.autofac.org/en/latest/integration/webforms.html

Hier ist der Code von Dependency Injection:

[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 von Dependency Injection, ich habe hinzugefügt:

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

Immer noch es funktioniert nicht. MeinService ist immer noch null. Als ich die App.Config Einstellung in Web.Config meiner Präsentation hinzufügen, bekam ich folgende Fehlermeldung:

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

Antwort

0

Autofac ASP.net WebForm Integration setzt voraus, dass die HttpApplicationIContainerProviderAccessor implementiert.

Wenn Sie WebActivator mit Ihrer IocConfig Klasse verwenden möchten. Die Global.asax-Klasse muss weiterhin die Schnittstelle IContainerProviderAccessor implementieren, damit das HttpModule den Autofac-Container finden kann.

können Sie diese Schnittstelle implementieren auf der IocConfig, indem sie sich die Umsetzung

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

und Ihre IocConfig nicht IContainerProviderAccessor implementieren müssen, sondern brauchen eine öffentliche statische haben Eigenschaft für die 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; } 
     } 
+0

Aber dann Es wird das Design der Zwiebelarchitektur verletzen. –

+0

@UsmanKhalid Antwort aktualisiert –

+0

Diese Lösung erfordert, dass Autofac in der Präsentationsschicht vorhanden ist, wodurch das Design der Zwiebelarchitektur verletzt wird. Alle Abhängigkeiten Injection-bezogene Bibliotheken sollten sich auf DependencyInjection Layer befinden. –

Verwandte Themen