Autofac - how to configure ResolutionPipeline on a controller?

Viewed 25

I'm using ASP.NET Core 3.1 / Autofac 6, and would like to configure a resolution pipeline middleware on a controller.

This is how I do it on a class I'm the owner of:

builder.RegisterType<MyService>().As<IMyService>().ConfigurePipeline(p => { /* */ });

However, when I do it on a controller, it doesn't work:

builder.RegisterType<WeatherForecastController>().AsSelf().ConfigurePipeline(...);

I know that the reason might be that I already have:

services.AddControllers();

but I'm not sure what to do about it. Any ideas?

1 Answers

Thanks @Alistair Evans, that's it. The referenced docs are mentioning .AddMvc(), but for the API it would be:

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddControllers().AddControllersAsServices();
}

Then in container registration:

public void ConfigureContainer(ContainerBuilder builder)
{
    //...
    builder.RegisterType<WeatherForecastController>().ConfigurePipeline(p =>
       {
         /* ... */
       }
}
Related