Skip to content

Simple Injector v4.8

Compare
Choose a tag to compare
@dotnetjunkie dotnetjunkie released this 24 Nov 14:43
· 37 commits to v4.8.x since this release

Release Notes

Simple Injector can be downloaded using NuGet.

The most prominent improvements in this release are:

  • Full support for ASP.NET Core 3.
  • The container can now perform verification on first resolve. This can be enabled using an option switch.
  • The container now allows sending notifications to inform that the container is about to be locked.
  • Several small improvements and bug fixes.

For the last few years, the development of ASP.NET Core has been in flux. We've seen breaking changes from release to release and we engaged in discussions with Microsoft about all kinds of issues and bugs that we came across. The introduction of ASP.NET Core v3 again caused some issues with Simple Injector. Those problems took us more time to analyze, discuss, design, test, document, and release.

Finally, in this release we now truly support ASP.NET Core 3. To pull this off, we had to mark quite a few methods as obsolete and replace them with new methods. For the most part, you can follow the compiler-generated obsolete messages that you get once you upgrade to v4.8. Or refer to the documentation, if freshly apply Simple Injector to an ASP.NET Core application.

Note that the v4.8 integration libraries can be used in ASP.NET Core v2 as well, and we advise ASP.NET Core v2 users to upgrade to Simple Injector v4.8 as well, because of the bug fixes we made, and to make it easier later on to migrate to ASP.NET Core v3.

The Container can now perform verification on first resolve. We found that many developers that are new to Simple Injector don't realize that Simple Injector allows verifying the configuration. To mitigate this, v5.0 will automatically verify the container for you when you resolve your first component, in case you, or one of your team members, forgot to call Container.Verify(). In this v4.8 release we add this feature, but leave it disabled by default to prevent any breaking changes. In v5 we will turn the switch. You can enable it by setting Container.Options.EnableAutoVerification to true or you can already set it to false to prevent verification in case you have your own safety net in place (e.g. by calling container.Verify() inside a unit test).

We added a new event that you can subscribe and receive a notification when the container is about to get locked—this would typically happen when the first component gets resolved. This enables making last-minute registrations and allows spotting who is locking the container. The ASP.NET Core integration package is now the primary consumer of this event—it needs this notification to successfully integrate with ASP.NET Core v3.

Improvements

Simple Injector core library

  • #555 Allowed performing verification upon first resolve. This feature can be enabled by setting Container.Options.EnableAutoVerification = true;. The value is false by default, but this will change in v5.
  • #767 Added the Container.ContainerLocking event that signals when the container is about to get locked.
  • #441 Resolving an unregistered collection now throws an exception with a message that explains that collections need to be registered explicitly and how to solve the issue.

ServiceCollection Integration package

  • #754 Added ASP.NET Core 3 support. To support ASP.NET Core 3, the following methods are added:
    • SimpleInjectorAddOptions.AutoCrossWireFrameworkComponents property. Can be used from within the AddSimpleInjector setup action.
    • New CrossWire() extension methods on top of SimpleInjectorAddOptions. They replace the CrossWire extension methods on top of SimpleInjectorUseOptions.
    • New AddLogging() and AddLocalization() extension methods on top of SimpleInjectorAddOptions. They replace the UseLogging() and UseLocalization() extension methods on top of SimpleInjectorUseOptions.

The following class members are now marked obsolete:

  • SimpleInjectorServiceCollectionExtensions:

    • UseLogging(SimpleInjectorUseOptions). You can call AddLogging(SimpleInjectorAddOptions) instead as part of the AddSimpleInjector setup action.
    • UseLocalization(SimpleInjectorUseOptions). You can call AddLocalization(SimpleInjectorAddOptions) instead as part of the AddSimpleInjector setup action.
    • CrossWire<TService>(SimpleInjectorUseOptions). You can call CrossWire<TService>(SimpleInjectorAddOptions) instead as part of the AddSimpleInjector setup action.
    • CrossWire(SimpleInjectorUseOptions, Type). You can call CrossWire(SimpleInjectorAddOptions, Type) instead as part of the AddSimpleInjector setup action.
  • SimpleInjectorUseOptions:

    • AutoCrossWireFrameworkComponents. Setting it has no effect any longer and using it will therefore cause a compile error. You can use SimpleInjectorAddOptions.AutoCrossWireFrameworkComponents instead as part of the AddSimpleInjector setup action.

ASP.NET Generic Host Integration package

  • #754 Enabled hosted services to be integrated with Simple Injector and ASP.NET Core 3.

ASP.NET Core Integration packages

  • #754 Added ASP.NET Core 3 support.

The following class members are now marked obsolete:

  • SimpleInjectorAspNetIntegrationExtensions:
    • EnableSimpleInjectorCrossWiring(). A call to .AddSimpleInjector() automatically enables cross wiring.
    • CrossWire<TService>(Container, IApplicationBuilder). Instead of calling CrossWire on the Container, you can now call CrossWire on SimpleInjectorAddOptions.
    • CrossWire(this Container, Type, IApplicationBuilder). Instead of calling CrossWire on the Container, you can now call CrossWire on SimpleInjectorAddOptions.
    • AutoCrossWireAspNetComponents(Container, IApplicationBuilder). Auto cross wiring is automatically enabled when calling .AddSimpleInjector().
    • AutoCrossWireAspNetComponents(Container, IServiceProvider). Auto cross wiring is automatically enabled when calling .AddSimpleInjector().
  • SimpleInjectorUseOptionsAspNetCoreExtensions:
    • UseMiddleware<TMiddleware>(SimpleInjectorUseOptions, IApplicationBuilder). This method can cause your middleware to be applied at the wrong stage in the pipeline. This will, for instance, cause your middleware to be executed before the static files middleware (i.e. the .UseStaticFiles() call) or before authorization is applied (i.e. the .UseAuthorization() call). Instead call app.UseMiddleware<TMiddleware>(Container), and make sure you call it at the right stage. This typically means after .UseStaticFiles() and .UseAuthorization(), but before .UseEndpoints(...).
    • UseMiddleware(SimpleInjectorUseOptions, Type, IApplicationBuilder). Same as previous. Call app.UseMiddleware(Container, Type) instead.

Bug fixes

ServiceCollection Integration package

  • #713 Resolving cross-wired transient service outside of scope threw a too generic exception.

ASP.NET Core Integration packages

  • #750 Prevented TagHelper classes with __Generated__ in their name to be resolved from Simple Injector.
  • #761 AddTagHelperActivation incorrectly uses DefaultScopedLifestyle for tag helpers instead of Lifestyle.Transient.