Код: Выделить всё
public interface IService
{
public Task GetString();
}
Код: Выделить всё
public class SampleDAL : IService
{
public async Task GetString()
{
//do something
}
}
Код: Выделить всё
public class SampleBl([FromKeyedServices("SampleBl")]IService service) : IService
{
public Task GetString()
{
var something = service.GetString();
return something;
}
}
Код: Выделить всё
[Route("[controller]")]
[ApiController]
public class SampleController([FromKeyedServices("SampleBl")] IService serviceBl) : Controller
{
[HttpGet("GetSomeString")]
public async Task GetString()
{
var somethingout = serviceBl.GetString();
return somethingout;
}
}
Код: Выделить всё
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddKeyedScoped("SampleDal");
builder.Services.AddKeyedScoped("SampleBl");
Код: Выделить всё
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Lifetime: Scoped ServiceKey: KeyedImplementationType: ': A circular dependency was detected for the service of type ''.Подробнее здесь: https://stackoverflow.com/questions/792 ... eyedscoped
Мобильная версия