gpt4 book ai didi

c# - 在 ASP.NET Core 2.0 中使用 KeyFilter

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:05 27 4
gpt4 key购买 nike

我在简单的 ASP.NET Core 2.0 WebAPI 应用程序中使用 KeyFilter 属性时遇到问题。

<PackageReference Include="Autofac" Version="4.6.1" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />

程序.cs:

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddAutofac())
.UseStartup<Startup>()
.Build();
}

启动.cs:

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

public IConfiguration Configuration { get; }

public IContainer ApplicationContainer { get; private set; }

// This method gets called by the runtime.
// Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();

// Autofac
var builder = new ContainerBuilder();

builder.Populate(services);
builder.RegisterType<ClassX1>()
.Keyed<IInterfaceX>("first")
.WithParameter("name", "X1")
.SingleInstance();

builder.RegisterType<ClassX1>()
.Keyed<IInterfaceX>("second")
.WithParameter("name", "X2")
.SingleInstance();

builder.RegisterType<ClassX1>()
.As<IInterfaceX>()
.WithParameter("name", "X3")
.SingleInstance();

builder.RegisterType<ValuesController>()
.WithAttributeFiltering();

ApplicationContainer = builder.Build();

return ApplicationContainer.Resolve<IServiceProvider>();
// return new AutofacServiceProvider(this.ApplicationContainer);
}

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

app.UseMvc();
}

ClassX1.cs:

public class ClassX1: IInterfaceX
{
public ClassX1(string name)
{
Name = name;
}

public string Name { get; }
}

IInterfaceX.cs:

public interface IInterfaceX
{
string Name { get; }
}

ValuesController.cs:

[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IInterfaceX _x;

public ValuesController([KeyFilter("second")] IInterfaceX x)
{
_x = x;
}

// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2", _x.Name };
}
}

调用“/api/values”产生 [“value1”,“value2”,“X3”]。但我希望 ["value1","value2","X2"].

如果我删除第三个注册(使用 X3)则

InvalidOperationException: Unable to resolve service for type 'WebApplicationAutofac.IInterfaceX' while attempting to activate 'WebApplicationAutofac.Controllers.ValuesController'.

尝试直接解析工作正确:

var temp = ApplicationContainer.ResolveKeyed<IInterfaceX>("second");

怎么了?

最佳答案

是的,它对 WebAPI Controller 非常有用。

解决方案是添加.AddControllersAsServices():

public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddControllersAsServices();

关于c# - 在 ASP.NET Core 2.0 中使用 KeyFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45917583/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com