Skip to content

Instantly share code, notes, and snippets.

@alistairjevans
Last active January 22, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistairjevans/40a24afd9cd41111d9a7dcb6b39b70e2 to your computer and use it in GitHub Desktop.
Save alistairjevans/40a24afd9cd41111d9a7dcb6b39b70e2 to your computer and use it in GitHub Desktop.
Autofac Changes for 5.0

Autofac Changes in 5.0

Framework Version Targeting Changes

From Autofac 5, we will no longer provide a target for .NET 4.5. The minimum version of .NET Framework you can use Autofac with is .NET 4.6.1.

The full targeting list is now:

  • netstandard2.1
  • netstandard2.0
  • net461

Immutable Container

The Container Registry can no longer be updated after it has been built.

The ContainerBuilder.Update method had already been marked as obsolete, and has now been removed.

If you want to change registration behaviour, you should do it in a lifetime scope that overrides the behaviour from the root container.

Asynchronous Disposal Support

Autofac Lifetime Scopes now implement the IAsyncDisposable interface, meaning that they can be disposed of asynchronously:

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

Nullable Reference Type Annotations

The Autofac Library is now built with Nullable Reference Type annotations.

https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references

The upshot of this is that users will get sensible compiler warnings if they also use nullable refence functionality, and then use a method like 'TryResolve', or pass null to Autofac where they shouldn't.

image

PR #1056

Build Callbacks in Lifetime Scopes

Fixes #985.

Build callbacks can now execute at the point of lifetime scope creation if new ones have been registered.

So, this works:

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 (same behaviour as the Container).

PR #1054

Enforcing Disposal Hierarchy

Fixes #1020.

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

The effect of this is that disposing a parent scope, and then trying to resolve from a child scope, will throw an ObjectDisposedException, whereas before it would 'sort of' work, until it didn't.

PR #1061

Prevent Autofac Auto-Injecting onto Static Properties

Fixes #1013.

Prevent Autofac injecting services onto static properties when auto-wiring of properties is enabled.

This may be a breaking change for anyone depending on the old accidental behaviour.

PR #1021

Other Fixed Issues

  • #1057 - PackageIcon needed in .csproj
  • #1040 - IsRegistered throws IndexOutOfRangeException for open generic type
  • #1041 - PropertiesAutowired fails when service is decorated using new syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment