本文介绍了将KeyFilter与ASP.NET Core 2.0结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have problem to using KeyFilter attribute in a simple ASP.NET Core 2.0 WebAPI application.

<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" />

Program.cs:

Program.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();
    }

Startup.cs:

Startup.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:

ClassX1.cs:

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

    public string Name { get; }
}

IInterfaceX.cs:

IInterfaceX.cs:

public interface IInterfaceX
{
    string Name { get; }
}

ValuesController.cs:

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"].

Calling "/api/values" yield ["value1","value2","X3"].But i expect ["value1","value2","X2"].

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

if i remove the third registration (with X3) then

尝试直接解析可以正确运行:

Trying to direct resolving works correct:

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

怎么了?

推荐答案

是的,它对于WebAPI控制器非常有效.

Yes, it works pretty cool for WebAPI Controllers.

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

The solution is to add .AddControllersAsServices():

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

这篇关于将KeyFilter与ASP.NET Core 2.0结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 02:42