Autofac v5.0.0 Release Notes

Release Date: 2020-01-27 // about 4 years ago
  • This is the first major-version release we've had in about three years (Autofac 4.0 was released in August 2016). There are some breaking changes and new features you should know about as you decide your upgrade strategy.

    ๐Ÿ’ฅ Breaking Changes

    Framework Version Targeting Changes

    ๐Ÿ‘ Starting with Autofac 5.0 there is no longer support for .NET 4.5.x. .NET 4.5.2, the last release in that line, follows the same support lifecycle as Windows Server 2012 R2 which ended mainstream support in September 2018.

    Autofac 5.0 now targets:

    • netstandard2.0
    • netstandard2.1
    • net461

    Containers are Immutable

    โšก๏ธ The container registry can no longer be updated after it has been built.

    โšก๏ธ The ContainerBuilder.Update method was marked obsolete in November 2016 and there has been a robust discussion to answer questions about how to get the container contents to adjust as needed at runtime.

    โšก๏ธ ContainerBuilder.Update has now been removed entirely.

    ๐Ÿ“š If you need to change registration behavior at runtime, there are several options available to you including the use of lambdas or child lifetime scopes. See this discussion issue for examples and ideas. We will work to add some documentation based on this issue.

    [PR #948 - thanks @weelink!]

    Lifetime Scope Disposal Hierarchy Enforced

    Resolving a service from a lifetime scope will now check all parent scopes to make sure none of them have been disposed.

    If you dispose a lifetime scope, all children of that lifetime scope will stop resolving objects. In cases like this you'll start getting ObjectDisposedException instead.

    โœ… If you have a custom application integration that involves creating/destroying lifetime scopes (e.g., custom per-request support) this may cause issues where proper disposal ordering is not occurring.

    ๐Ÿ›  [Fixes #1020; PR #1061 - thanks @alistairjevans!]

    Prevent Auto-Injecting onto Static Properties

    Autofac will no longer do property injection on static properties when auto-wiring of properties is enabled.

    ๐Ÿ— If your application behavior depends on static property injection you will need to do some additional work like adding a build callback to populate the property.

    ๐Ÿ›  [Fixes #1013; PR #1021 - thanks @alistairjevans!]

    ๐Ÿ”‹ Features and Fixes

    ๐Ÿ‘ Asynchronous Disposal Support

    Autofac lifetime scopes now implement the IAsyncDisposable interface so they can be disposed asynchyronously.

    await using (var scope = container.BeginLifetimeScope()) { var service = scope.Resolve\<ServiceThatImplementsIAsyncDisposable\>(); // When the scope disposes, any services that implement IAsyncDisposable will be// Disposed of using DisposeAsync rather than Dispose.}
    

    [PR #1037 - thanks @alistairjevans!]

    Nullable Reference Type Annotations

    ๐Ÿ— Autofac is now build using nullable reference type annotations. This allows developers to get sensible compiler warnings if they opt-in, thus avoiding NullReferenceException instances where possible.

    โš  Nullable reference type warnings

    [PR #1037 - thanks @alistairjevans!]

    ๐Ÿ— Build Callbacks in Lifetime Scopes

    ๐Ÿ— One method of running code at container build time is by registering a build callback. Previously this only worked at the container level, but we've added the ability to register callbacks that run at lifetime scope creation as well.

    var scope = container.BeginLifetimeScope(cfg =\>{ cfg.RegisterBuildCallback(scope =\> { /\* do something \*/ }); });
    

    The callback will be invoked just prior to BeginLifetimeScope exiting, after any startable components are instantiated.

    ๐Ÿ›  [Fixes #985; PR #1054 - thanks @alistairjevans!]

    ๐Ÿ›  Other Fixes

    Still TODO

    Now that Autofac 5.0 is out, there is still a lot to do. We'll be working on these things as fast as we can:

    • โšก๏ธ Updating integration packages we support so they ensure compatibility with Autofac 5.
    • ๐Ÿ“š Updating the documentation to reflect the above changes.

    ๐Ÿ“ฆ Some of this is sitting in branches ready to go, other things need to be done now that we have this core package out there.

    If your favorite integration isn't ready yet, we're doing our best. Rather than filing "When will this be ready?" issues, consider pull requests with the required updates.