Autofac (a .NET IoC container) makes .NET code better. Simple. Controlling the way software interacts with it’s components (dependencies) is one powerful way to the increase its “bendability”. Bendability meaning how well a chunk of software is able to cope with change (this is inevitable). WCF’s unique object model (bindings, endpoints, behaviors, contracts, etc) can make doing IoC more of a challenge, however Autofac’s native WCF integration comes to the rescue.

Autofac (a .NET IoC container) makes .NET code better. Simple. Controlling the way software interacts with it’s components (dependencies) is a powerful way to the increase its “bendability”. Bendability being softwares’ ability to adapt with change (which is inevitable). The Autofac project provides a native extension that supports WCF, called WcfIntegration. WCF’s unique object model (bindings, endpoints, behaviors, contracts, etc) can make doing IoC more of a challenge, however Autofac’s native WCF integration comes to the rescue. Using this integration, Autofac can host services in a WCF server, and can improve the reliability of WCF clients.

There’s no problem in Computer Science that can’t be solved by adding another level of indirection to it

Autofac.Integration.Wcf.dll is included in the Autofac binary downloads. To use the integration you need to reference it, in addition to the core Autofac.dll.

As per most IoC containers, when Autofac instantiates a component, it satisfies the component’s dependencies by finding and instantiating other components. Components express their dependencies to Autofac as constructor parameters.

Using Autofac in the context of an IIS HTTP activated WCF service is pretty straight forward:

2. Set AutofacHostFactory.Container with this built container.
3. Update your .svc files to use the AutofacServiceHostFactory (for BasicHttpBinding or WSHttpBinding services) or the AutofacWebServiceHostFacotry (for WebHttpBinding services).

Below are some tiny code snippets that highlight how to configure a “contract type registration” with Autofac. Contract type binding is one of 3 methods that Autofac supports, which are: implementation type registration (e.g. Service="TestService.Service1, TestService"), contract type registration (e.g. Service="TestService.IService1, TestService") and named service registration (e.g. Service="my-service").

### Global.asax

Your done, that’s it! In addition to the WCF integration, Autofac has fantastic support for ASP.NET MVC and MEF.

For bonus points, having the IoC container configure itself from XML configuration may be desirable given your situation. For example, you may want to stub out objects loaded by the container in a development environment, for testing purposes. Most IoC containers provide this support, and Autofac is no exception.

### Global.asax

You need to declare a section handler somewhere near the top of your config file.

### web.config

Then, provide a section describing your components: