Autofac Extension

The Autofac extension will help you intialize your Autofac container by scanning for classes that implement AutofacRegistration and executing it's register method.

Installation

You can install the Autofac extension via nuget over here.

Creating Registrations

To initialize your container you must declare classes that implements IAutofacRegistration and implement the Register method. For example:
    public class RegisterMyTypes: IAutofacRegistration
    {
        public void Register(ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterType<MyType>().As<IMyType>();
        }
    }

Native Registrations (new in 2.0.3.0)

Bootstrapper now supports the use of native registrations. Native registrations can be used by themselves or in conjunction with Bootstrapper generic and specific registration. For Autofac, just declare a class that inherits from Autofac.Module and Bootstrapper will use it to initialize the container.
 public class TestAutofacModule: Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<TestImplementation>().As<ITestInterface>();
        }
    }

Using the extension

To use the extension simply write this code:
Bootstrapper.With.Autofac().Start();

AutoRegistration

You can use the AutoRegistration option to indicate that you want to automatically register al types that implement an interface of the same name. For example, if you have an interface called IMyType and a type called MyType that implements IMyType then the Autoregistration option will Bind IMyType to MyType. To use the AutoRegistration option simply write this code:
Bootstrapper.With.Autofac().UsingAutoRegistration().Start();

Accessing the initialized Container

After Bootstrapper has been invoked and the container has been initialized it can be accessed with the Container property of Boostrapper. For example:
   var container = (IContainer)Bootstrapper.Container;

Using An Existing Container (new in 2.0.3.7)

You can tell Bootstrapper to use an existing container instead of creating a new one like this:
Bootstrapper.With.Autofac().WithContainer(anExistingContainer).Start();

Generic Registration

You can use Generic Registration with the Autofac extension. For details go to the Generic Registration page.

Last edited Apr 11, 2013 at 4:33 AM by luisbocaletti, version 9

Comments

luisbocaletti Apr 11, 2013 at 4:15 AM 
Is it possible that your service module is in another assembly that has not been loaded yet?
You can force loading the assembly using the Including method like this:

Bootstrapper.Including.
Assembly(Assembly.GetAssembly(typeof(MyServiceModuleAssembly))).
With.Autofac()
Start();

imdingji May 12, 2012 at 10:01 AM 
Hi, I want to use this Autofac bootstrapper for my mvc3 project but failed. I have integrated Autofac into my project successfully without bootstrapper. My steps:

1. Create a class "ServiceModule" inherits from Autofac.Module and implemente the "Load" method.
2. Then write the code "Bootstrapper.With.Autofac().Start();" in Application_Start().
3. Run this mvc project I got an error message: "No parameterless constructor defined for this object."

Did i miss some steps? Can you give me some help? Thanks!