Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'ConfigureServices returning a System.IServiceProvider isn't supported.'

Tags:

I need ti use this AutoFac in ASP core 3.0

When I use this code in startu up:

public IServiceProvider ConfigureServices(IServiceCollection services) {     services.AddControllers();     return services.BuildAutofacServiceProvider(); } 

It show me this error:

'ConfigureServices returning an System.IServiceProvider isn't supported.'

And I change the program.cs by this:

public class Program {     public static void Main(string[] args)     {         CreateHostBuilder(args).Build().Run();     }      public static IHostBuilder CreateHostBuilder(string[] args) =>         Host.CreateDefaultBuilder(args)         .UseServiceProviderFactory(new AutofacServiceProviderFactory())             .ConfigureWebHostDefaults(webBuilder =>             {                 webBuilder.UseStartup<Startup>();             }); } 

But it not solved.

This is BuildAutofacServiceProvider() Code:

public static IServiceProvider BuildAutofacServiceProvider(this IServiceCollection services) {     var ContainerBuilder = new ContainerBuilder();     ContainerBuilder.Populate(services);     ContainerBuilder.AddService();     var container = ContainerBuilder.Build();      return new AutofacServiceProvider(container); } 

How can I solve this problem?

like image 859
mr-dortaj Avatar asked Sep 27 '19 11:09

mr-dortaj


People also ask

How do I get an instance of IServiceProvider?

An instance of IServiceProvider itself can be obtained by calling a BuildServiceProvider method of an IServiceCollection. IServiceCollection is a parameter of ConfigureServices method in a Startup class. It seems to be magically called with an instance of IServiceCollection by the framework.

What is an IServiceProvider?

The IServiceProvider is responsible for resolving instances of types at runtime, as required by the application. These instances can be injected into other services resolved from the same dependency injection container. The ServiceProvider ensures that resolved services live for the expected lifetime.

What is IServiceCollection in .NET core?

IServiceCollection is the collection of the service descriptors. We can register our services in this collection with different lifestyles (Transient, scoped, singleton) IServiceProvider is the simple built-in container that is included in ASP.NET Core that supports constructor injection by default.

What is a service provider in C#?

C# public interface IServiceProvider { object GetService(Type serviceType); } A service provider is basically a dictionary or a registry that manages a set of objects that provide certain services.


2 Answers

Startup syntax has changed for configuring Autofac for ASP.NET Core 3.0+

In addition to using the following on the host builder

.UseServiceProviderFactory(new AutofacServiceProviderFactory()) 

In Startup do the following format

public void ConfigureServices(IServiceCollection services) {     //... normal registration here      // Add services to the collection. Don't build or return     // any IServiceProvider or the ConfigureContainer method     // won't get called.      services.AddControllers(); }  // ConfigureContainer is where you can register things directly // with Autofac. This runs after ConfigureServices so the things // here will override registrations made in ConfigureServices. // Don't build the container; that gets done for you. If you // need a reference to the container, you need to use the // "Without ConfigureContainer" mechanism shown later. public void ConfigureContainer(ContainerBuilder builder) {     // Register your own things directly with Autofac     builder.AddMyCustomService();      //... } 

Reference Autofac documentation for ASP.NET Core 3.0+

like image 137
Nkosi Avatar answered Sep 16 '22 17:09

Nkosi


Instead of Host in Program.cs you can use WebHost

public class Program {   public static void Main(string[] args)   {     CreateWebHostBuilder(args).Build().Run();   }    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>     WebHost.CreateDefaultBuilder(args)       .UseStartup<Startup>(); } 

In this case following code works

public IServiceProvider ConfigureServices(IServiceCollection services) {   ...   var builder = new ContainerBuilder();    builder.Populate(services);   var container = builder.Build();   return new AutofacServiceProvider(container); } 
like image 35
R.Titov Avatar answered Sep 20 '22 17:09

R.Titov