NOTE: Simple Injector v2.0 is NOT the latest release. Please click on the Downloads tab above to find the latest Simple Injector release.
Warning: You need .NET 4.0 or up to use Simple Injector 2.0.
Warning: This major release contains several breaking changes. Please take a good look at the list of breaking changes when you're upgrading from a 1.x release to this release.
Besides the usual bug fixes, this major release adds two important new pieces of functionality to the core library, namely explicit lifestyle support and debugger diagnostic services.
The core framework now has explicit notion of lifestyles. There is an abstract Lifestyle
base class and built-in lifestyles are Lifestyle.Transient
. Explicit lifestyle support gives more flexibility in the way users can register types using a lifestyle. Decorators, open generic types and batch registered types can now simply be registered by supplying any given lifestyle. The addition of the Lifestyle.CreateHybrid
method makes creating mixed (a.k.a. hybrid) lifestyles a breeze and custom lifestyles can be created by inheriting from Lifestyle
or simply by calling Lifestyle.CreateCustom
The Debugger Diagnostic Services
supplies users with diagnostic information, much like Microsoft Code Analysis (a.k.a. FxCop) does for .NET assemblies. These services are implemented as Visual Studio debug view and part of the core library. This means that it is integrated with Visual Studio and is able to analyze the configured container at runtime while running in the debugger. You should definitely check this out!
New features and improvements for the SimpleInjector.dll
- Debug View added to diagnose possible misconfigurations in the container. Read all about it here.
- Lifestyle base class added for implementing custom object lifetime management.
- The SimpleInjector.Extensions.dll library has been merged with the SimpleInjector.dll core library. The RegisterAll extension methods have been replaced with instance methods on the Container and all other extensions (batch registration, decorator registration, and open generic registration) are still located in the SimpleInjector.Extensions namespace.
- Overloads have been added for all registration methods that take Lifetyle as argument to enable registering any service using any lifestyle.
- ExpressionBuilding event added to the Container. The ExpressionBuilding event gets fired right after the expression is built that creates a new instance (usually a NewExpression). Initializers and lifestyle caching will not yet be applied at this point.
- RegisterManyForOpenGeneric extension methods now ignore any found non-generic decorator. The method would throw an exception when such type was found. The new behavior makes it easier to use batch registration while having non-generic decorators in the same project as the implementation types are.
- AddRegistration(Type, Registration) method added to allow directly adding a registration. This allows binding two or more service types to the same registration.
- New GetTypesToRegister overloads have been added that take a Container as argument. These new overloads allow filtering out non-generic decorator types (that can only be registerd using the RegisterDecorator methods).
Breaking changes for the SimpleInjector.dll
- All Func<T> factory delegates registered using one of the Register overloads (such as Register<T>(Func<T>)) are now considered to return transient instances. Custom lifestyles should not be applied using this factory delegate, but by supplying a custom Lifestyle implementation (which can be created using the new Lifestyle.CreateCustom method).
- Action<T> delegates registered using RegisterInitializer<T>(Action<T>) are now also applied to Func<T> delegates registered using Register<T>(Func<T>), RegisterSingle<T>(Func<T>) and to instances registered using RegisterSingle<T>(T) and RegisterSingle(Type, object). Note that this could lead to inconsistent behavior or performance issues if the Func<T> delegate implements its own caching/lifetime, since for Register<T>(Func<T>) the Action<T> initialization delegate will be applied on each resolve. Instead of registering Func<T> delegates to implement custom lifestyle, derive from Lifestyle base or call the Lifestyle.CreateCustom method.
- Container.Verify() now locks the container. New registrations can't be made or overridden after this. Not locking the container could lead to a corrupt configuration when registrations where replaced or analyzed using the new Debugger Diagnostic Services.
- Besides value types, the reference types System.Type and System.String are now also considered ambiguous and are disallowed from being registered in the container. An exception is thrown in this case.
- The IInstanceProducer interface has been replaced with a new InstanceProducer class.
- The ExpressionBuilt event will not got called twice anymore for singleton registrations. This is a bug fix, but also a breaking change. The new ExpressionBuilding event will now handle the first call and will do this in a more reliable manner. In practice this means that developers using the RegisterWithContext extension method from the Context based injection documentation, have to use the new version. This new version makes use of the new ExpressionBuilding event instead of the ExpressionBuilt event, making context based injection much more reliable.
- The GetInitializer<T>() method has been removed from the Container. The method cluttered the main API because it was only intended to be used when extending the framework. The method is moved to the AdvancedExtensions class of the SimpleInjector.Advanced namespace. It's an extension method now. (work item 18349).
- The old GetTypesToRegister overloads have been marked obsolete. The new overloads can be used instead.
Bug fixes for the SimpleInjector.dll
- RegisterDecorator overloads wrapped singletons twice. This was caused by the ExpressionBuilt event that was triggered twice for singletons. (work item 19110)
- Memory leak when using decorators with while creating an infinite amount of Container instances. (work item 19241)
- RegisterOpenGeneric overloads failed to resolve types that had a "where TA : TB" type constraint.
Changes in the SimpleInjector.Integration.Web.dll
- WebRequestLifestyle class has been added.
- Order of calling RegisterDecorator and RegisterPerWebRequest mattered for the lifetime of the decorator. (work item 19120)
- Breaking Change The PerWebRequestInstanceCreator<T> class has been removed. The new WebRequestLifestyle can be used instead.
Changes in the SimpleInjector.Integration.Wcf.dll
- WcfOperationLifestyle class has been added.
- Order of calling RegisterDecorator and RegisterPerWcfOperation mattered for the lifetime of the decorator. (work item 19120)
Changes in the SimpleInjector.Extensions.LifetimeScoping.dll
- LifetimeScopeLifestyle class has been added.
- Order of calling RegisterDecorator and RegisterLifetimeScope mattered for the lifetime of the decorator. (work item 19120)