C# 使用autofac Accure时出现以下错误::“不支持ConfigureServices返回System.IServiceProvider。”
我不能在我的项目中使用autofac 我写了这篇文章:C# 使用autofac Accure时出现以下错误::“不支持ConfigureServices返回System.IServiceProvider。”,c#,asp.net,asp.net-core,asp.net-core-webapi,autofac,C#,Asp.net,Asp.net Core,Asp.net Core Webapi,Autofac,我不能在我的项目中使用autofac 我写了这篇文章: public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime.
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();
});
}
}
nad这是我的程序。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();
}
}
但它告诉我这个错误:
“不支持ConfigureServices返回System.IServiceProvider。”
如何解决这个问题?这是因为您正在尝试3.0之前的方法。它不支持IServiceProvider返回类型
public virtual void ConfigureServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services);
从autofac文档:
这不适用于ASP.NET Core 3+或.NET Core 3+通用托管支持-ASP.NET Core 3已不推荐从ConfigureServices返回服务提供程序的功能
检查柱3.0
这是因为您正在尝试3.0之前的方式。它不支持IServiceProvider返回类型
public virtual void ConfigureServices (Microsoft.Extensions.DependencyInjection.IServiceCollection services);
从autofac文档:
这不适用于ASP.NET Core 3+或.NET Core 3+通用托管支持-ASP.NET Core 3已不推荐从ConfigureServices返回服务提供程序的功能
检查柱3.0
@瓦西里Program@vasily.sib在程序中,我使用它,但它不是WIRK。请更具体地说明它不起作用的原因,以便我能提供更多帮助。请参阅报告。您的存储库使用您在问题中描述的解决方案。这是行不通的。您需要使用post 3.0 autofac配置方式完全重新构造代码。这是一个需要注入的模块示例。您需要创建自己的。我使用它,但它不是wirko。请更具体地说明它不起作用的原因,以便我可以提供更多帮助。请参阅报告。您的存储库使用您在问题中描述的解决方案。这是行不通的。您需要使用post 3.0 autofac配置方式完全重新构造代码。这是一个需要注入的模块示例。你需要创造你自己的。
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();
}
}