.net8webapi:
public interface ITestKeyService
{
public int Id {
get; set; }
public string Name {
get; set; }
}
public class Test1KeyService : ITestKeyService
{
public int Id {
get; set; } = 1;
public string Name {
get; set; } = "Name1";
}
public class Test2KeyService : ITestKeyService
{
public int Id {
get; set; } = 2;
public string Name {
get; set; } = "Name2";
}
builder.Services.AddKeyedSingleton<ITestKeyService, Test1KeyService>("ServiceA");
builder.Services.AddKeyedSingleton<ITestKeyService, Test2KeyService>("ServiceB");
[Route("api/private/v1/[controller]/[action]")]
[ApiController]
[EnableCors("any")]
public class TestKeyController : ControllerBase
{
private readonly ITestKeyService service1;
private readonly ITestKeyService service2;
public TestKeyController([FromKeyedServices("ServiceA")] ITestKeyService service1, [FromKeyedServices("ServiceB")] ITestKeyService service2)
{
this.service1 = service1;
this.service2 = service2;
}
[HttpGet]
public ActionResult<Dictionary<string, string>> MyTestKey()
{
Dictionary<string, string> dic = new();
dic["服务1的名字"] = service1.Name;

最低0.47元/天 解锁文章
1438

被折叠的 条评论
为什么被折叠?



