【问题标题】:when use autofac accoure this error : : 'ConfigureServices returning an System.IServiceProvider isn't supported.'使用 autofac 时会出现此错误:“不支持返回 System.IServiceProvider 的配置服务。”
【发布时间】:2020-06-23 03:43:59
【问题描述】:

我不想在我的项目中使用 autofac。

我写这个启动:

  public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(Assembly.GetEntryAssembly())
                .AsImplementedInterfaces();
        builder.Populate(services);
        builder.AddDispatchers();

        var conteiner = builder.Build();

        return new AutofacServiceProvider(conteiner);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

这是我的 program.cs :

public class Program
{
    public static void Main(string[] args)
    {
        var host = Host.CreateDefaultBuilder(args)
     .UseServiceProviderFactory(new AutofacServiceProviderFactory())
     .ConfigureWebHostDefaults(webHostBuilder =>
     {
         webHostBuilder
          .UseContentRoot(Directory.GetCurrentDirectory())
          .UseIISIntegration()
          .UseStartup<Startup>();
     })
     .Build();

        host.Run();
    }
}

但它告诉我这个错误:

'不支持返回 System.IServiceProvider 的配置服务。'

如何解决这个问题?

【问题讨论】:

  • @vasily.sib in Program

标签: c# asp.net asp.net-core asp.net-core-webapi autofac


【解决方案1】:

这是因为您正在尝试 3.0 之前的方式。 Check the ConfigureServices docs。它不支持 IServiceProvider 返回类型。

public virtual void ConfigureServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services);

来自 autofac 文档:

这不适用于 ASP.NET Core 3+ 或 .NET Core 3+ 通用托管支持 - ASP.NET Core 3 已弃用从 ConfigureServices 返回服务提供者的能力

查看Autofac net core guide发布3.0

public class Program
{
  public static void Main(string[] args)
  {
    // ASP.NET Core 3.0+:
    // The UseServiceProviderFactory call attaches the
    // Autofac provider to the generic hosting mechanism.
    var host = Host.CreateDefaultBuilder(args)
        .UseServiceProviderFactory(new AutofacServiceProviderFactory())
        .ConfigureWebHostDefaults(webHostBuilder => {
          webHostBuilder
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>();
        })
        .Build();

    host.Run();
  }
}
public class Startup
{
  public Startup(IHostingEnvironment env)
  {
    // In ASP.NET Core 3.0 `env` will be an IWebHostingEnvironment, not IHostingEnvironment.
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    this.Configuration = builder.Build();
  }

  public IConfigurationRoot Configuration { get; private set; }

  public ILifetimeScope AutofacContainer { get; private set; }

  // ConfigureServices is where you register dependencies. This gets
  // called by the runtime before the ConfigureContainer method, below.
  public void ConfigureServices(IServiceCollection services)
  {
    // Add services to the collection. Don't build or return
    // any IServiceProvider or the ConfigureContainer method
    // won't get called.
    services.AddOptions();
  }

  // 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 by the factory.
  public void ConfigureContainer(ContainerBuilder builder)
  {
    // Register your own things directly with Autofac, like:
    builder.RegisterModule(new MyApplicationModule());
  }

  // Configure is where you add middleware. This is called after
  // ConfigureContainer. You can use IApplicationBuilder.ApplicationServices
  // here if you need to resolve things from the container.
  public void Configure(
    IApplicationBuilder app,
    ILoggerFactory loggerFactory)
  {
    // If, for some reason, you need a reference to the built container, you
    // can use the convenience extension method GetAutofacRoot.
    this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();

    loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    app.UseMvc();
  }
}

【讨论】:

  • 我用过,但它不行
  • 请详细说明为什么它不起作用,以便我能提供更多帮助。
  • 请看repostiroy。
  • 您的存储库使用您在问题中描述的解决方案。这行不通。你需要完全重构你的代码,使用 3.0 后的 autofac 配置方式。
  • 这是您需要注入的模块示例。您需要创建自己的。
猜你喜欢
  • 2020-01-27
  • 2020-10-05
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
相关资源
最近更新 更多