question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RegisterTypes will attempt to register abstract types (undocumented breaking change in v6.5.0)

See original GitHub issue

Describe the Bug

In Autofac v6.4.0, a scanning registration using RegisterTypes that includes types that cannot be constructed (e.g. interfaces, abstract classes) will work fine.

In Autofac v6.5.0 onwards, it will attempt to register those non-constructible types resulting in an Autofac.Core.Activators.Reflection.NoConstructorsFoundException upon building the container.

This is a breaking change that I couldn’t find documented anywhere.

Steps to Reproduce

public class ReproTest
{
    interface IInterface { }

    class Class : IInterface
    {
    }

    [Fact]
    public void Repro()
    {
        var containerBuilder = new ContainerBuilder();

        containerBuilder
            .RegisterTypes(typeof(IInterface), typeof(Class))
            .AssignableTo<IInterface>();

        var exception = Record.Exception(() => containerBuilder.Build());

        Assert.Null(exception);
    }
}

Expected Behavior

No exception, test passes (v6.4.0 behaviour)

Exception with Stack Trace

v6.5.0 onwards throws:

Autofac.Core.Activators.Reflection.NoConstructorsFoundException: No accessible constructors were found for the type 'ReproTestProject.ReproTest+IInterface'.
   at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.GetDefaultPublicConstructors(Type type)
   at Autofac.Core.Activators.Reflection.DefaultConstructorFinder.FindConstructors(Type targetType)
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ConfigurePipeline(IComponentRegistryServices componentRegistryServices, IResolvePipelineBuilder pipelineBuilder)
   at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices, IResolvePipelineBuilder pipelineBuilder)
   at Autofac.Core.Registration.ComponentRegistration.BuildResolvePipeline(IComponentRegistryServices registryServices)
   at Autofac.Core.Registration.ComponentRegistryBuilder.Build()
   at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
   at ReproTestProject.ReproTest.<>c__DisplayClass2_0.<Repro>b__0()

Dependency Versions

Autofac: v6.5.0

Additional Info

A workaround is for the user to filter out abstract types from the types passed to RegisterTypes themselves.

In trying to figure out why I was getting this exception after upgrading to v6.5.0 from v6.4.0 I took a look at the diff between the two versions, and the removal of !t.IsAbstract in CanBeRegistered in this commit was something that stood out to me that might be the cause.

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tilligcommented, Aug 11, 2023

This is going out in 7.1.0 which should be published on NuGet within the next hour.

1reaction
tilligcommented, Aug 1, 2023

Let me see if I can figure out whether this was intentional or not so we can determine what to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unity DI Container RegisterType method breaking changes ...
1 Answer. They have changed the signature of the RegisterType in this PR and it is now taking a ITypeLifetimeManager instead of a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found