Ich folge Zwiebel Architektur, deren Bootstrapper Teil mit Autofac
gebaut wird.Autofac WebForms in Onion Architektur
Architektur ist wie folgt:
- Kern
- Dependency Injection (Autofac ist hier)
- Dienst
- Präsentation (MVC 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
Aber dann Es wird das Design der Zwiebelarchitektur verletzen. –
@UsmanKhalid Antwort aktualisiert –
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. –