Autofac 2.2 Released
It’s that time again! There are some great features from the original plan still in the pipeline, but the completed work is important to some users so binaries for Autofac 2.2 have been made available on the Autofac site.
What’s new?
Version 2.2 is highly backwards-compatible with 2.1. Along with many small improvements, the significant additions in the new version are the return of a mutable container model, support for ASP.NET MVC 2.0 and lightweight adapters.
ContainerBuilder.Update() and full container mutability
One of the most controversial changes to Autofac in the 2.1 release was the deliberate omission of an easy way to add more components to an already-built container. The reasons for and against this are worth a blog post on their own, but in the end it turns out that integration into third-party frameworks is especially tough without this feature.
To add components to an existing container, use ContainerBuilder.Update()
:
var container = // something already built
var updater = new ContainerBuilder();
updater.RegisterType<A>();
updater.Register(c => new B()).As<IB>();
// Add the registrations to the container
updater.Update(container);
Configuration this way is often much harder to follow than a simple ‘register then build’ style, so again, use this feature only where necessary.
ASP.NET MVC 2.0 support
The new release is built against the ASP.NET MVC 2 binaries; you’ll have to compile from source if you want to target ASP.NET MVC 1. There can’t be many projects out there that can upgrade to Autofac 2.2, but not to the new release of ASP.NET MVC, so we expect this will be convenient for most users.
Out of the box, Autofac now supports ASP.NET MVC Areas, and integration with other ASP.NET MVC features is getting richer.
The AutofacControllerFactory
policies have changed in this release, so if you previously added controllers by hand (as named services) you will need to update your code to register controllers by their concrete type (without an As()
or Named()
configuration clause..)
Lightweight adapters
Lightweight adapters are an easy way of expressing “for every registered X
, provide an adapter of type Y
.”
The introductory article is the best place to find out how this feature works.
Other changes
-
COM interfaces using No-PIA (or ‘ embedded interop types’) can now be exposed as services
-
Simplified WCF configuration for self-hosted services
-
Scoped registration improvements:
SingleInstance()
now behaves as expected and binds the instance’s lifetime to the nested scope -
Generic registration constraint checking improvements - more complex type constraints can now be handled when determining the suitability of an open generic type to satisfy a closed generic service
-
Additional
Resolve()
/TryResolve()
convenience overloads have been added to fill some gaps -
The same type can now be registered multiple times in a single XML configuration block
-
Several usability enhancements have been made to the scanning feature
Credits
This release, as with every Autofac release, is the product of the lively (and growing) community. There have been some especially fine patches and contributions this time around - thank you everyone for your hard work!