Click or drag to resize

AutofacContainerConfigurator Class

IContainerConfigurator implementation for registering Enterprise Library dependencies into an Autofac container.
Inheritance Hierarchy
SystemObject
  Autofac.Extras.EnterpriseLibraryConfiguratorAutofacContainerConfigurator

Namespace:  Autofac.Extras.EnterpriseLibraryConfigurator
Assembly:  Autofac.Extras.EnterpriseLibraryConfigurator (in Autofac.Extras.EnterpriseLibraryConfigurator.dll) Version: 4.0.0-CI-216
Syntax
public class AutofacContainerConfigurator : IContainerConfigurator

The AutofacContainerConfigurator type exposes the following members.

Constructors
  NameDescription
Public methodAutofacContainerConfigurator
Initializes a new instance of the AutofacContainerConfigurator class.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRegisterAll
Consume the set of TypeRegistration objects and configure the associated container.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

Enterprise Library uses IContainerConfigurator implementations as a bridge between configuration sources (IConfigurationSource), type registration providers (ITypeRegistrationsProvider), and inversion of control containers. This implementation allows you to use Autofac as the container from which Enterprise Library services get resolved.

Setup of a container via a configurator is done using the ConfigureContainer(IContainerConfigurator, IConfigurationSource) method. Using this configurator, registering Enterprise Library with Autofac looks like this:

C#
// Create the ContainerBuilder and register EntLib.
var builder = new ContainerBuilder();
using (var configSource = ConfigurationSourceFactory.Create())
{
  var configurator = new AutofacContainerConfigurator(builder);
  EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource);
}

// Register other services/dependencies, then set the
// service locator.
var container = builder.Build();
var autofacLocator = new AutofacServiceLocator(container);
EnterpriseLibraryContainer.Current = autofacLocator;

Note the use of AutofacServiceLocator to set the Enterprise Library service locator. You need to do that so Enterprise Library can do any internal resolutions it requires.

Even though you can use this configurator directly, the simplest way to get Enterprise Library configured with Autofac is to use the RegisterEnterpriseLibrary extensions. The above sample code becomes much simpler and more Autofac-styled with the extensions.

C#
// Create the ContainerBuilder and register EntLib.
var builder = new ContainerBuilder();
builder.RegisterEnterpriseLibrary();

// Register other services/dependencies, then set the
// service locator.
var container = builder.Build();
var autofacLocator = new AutofacServiceLocator(container);
EnterpriseLibraryContainer.Current = autofacLocator;

Note that using Autofac as the backing store for service resolution in Enterprise Library, changes in the dependency configuration source are not directly supported. That is, if the set of configured services changes, the container is not automatically rebuilt. This is a different behavior from Unity, which supports configuration change and container update during application execution.

The reason this is important is that some internal Enterprise Library components make the assumption that the container has a configuration change manager (ConfigurationChangeEventSource) registered regardless of support. This configurator will add a placeholder implementation to the container to enable these components to function, but developers should be aware that no action will take place if configuration changes even though the component appears to be present in the container.

See Also

Reference

EnterpriseLibraryRegistrationExtensions.RegisterEnterpriseLibrary