I am following onion architecture whose Bootstrapper part is build with Autofac
.
Architecture is as follows:
- Core
- DependencyInjection (Autofac is here)
- Service
- Presentation (MVC 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