Autofac RegisterAssemblyTypes from tests

443 views Asked by At

In my Startup class I a have method for RegisterAssemblyTypes through autofac as the following:

public class Startup
{
    public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterModule(new ApplicationModule());
    }
}

public class ApplicationModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder
            .RegisterAssemblyTypes(
                typeof(EventHandler).GetTypeInfo().Assembly)
                .AsClosedTypesOf(typeof(IIntegrationEventHandler<>));
    }
}

What is the suggested way of registering assemblies using Autofac (e.g., RegisterAssemblyTypes) when the Startup class is called from integration tests; e.g.,

public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{ }

I am using Autofac 4.9.4.

Question:

how do you register an assembly type in WebApplicationFactory<Startup> using Autofac?

1

There are 1 answers

0
Dr. Strangelove On BEST ANSWER

Due to incomplete documentation and a very poor support from Autofac maintainers, we decided to revert Autofac usage throughout all the projects in our institute.

Now we use ASP.NET Core's built-in registration, which works as expected when called in either of the following methods:

public class IntegrationTestApplicationFactory : WebApplicationFactory<Startup>
{
    protected override IHost CreateHost(IHostBuilder builder)
    {
        // register services here,
        // or in the ConfigureWebHost method
    }

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            services.AddTransient<YourServiceType>();
        }
    }
}