I am adding a new singleton (Service B) into my .NET project that depends on a value obtained from another service. The singleton can be one of two class instances depending on the value returned by ServiceA.
serviceList
.AddSingleton<ServiceA, ServiceAImpl>()
.AddSingleton<ServiceB, ServiceBImpl>()
...
builder.Build()
As mentioned earlier, depending on a value from ServiceA, ServiceB can either be ServiceBImpl1 or ServiceBImpl2. Registering both services seems unnecessary.
I figured I might be able to use a lambda function to pull this off, but it seems like it cannot find the dependent service correctly:
.AddSingleton<ServiceB>(serviceList => {
var serviceA = serviceList.GetRequiredService<ServiceAImpl>();
var someResponseFromA = serviceA.doSomething();
if (someResponseFromA == 1)
{
return new ServiceBImpl1();
}
else
{
return new ServiceBImpl2();
}
})
doSomething in this case returns a null value always and complains that ServiceAImpl is not available (for context, doSomething is a blocking call). How would I get the values I want from ServiceAImpl to use to register ServiceB appropriately?
I am adding a new singleton (Service B) into my .NET project that depends on a value obtained from another service. The singleton can be one of two class instances depending on the value returned by ServiceA.
serviceList
.AddSingleton<ServiceA, ServiceAImpl>()
.AddSingleton<ServiceB, ServiceBImpl>()
...
builder.Build()
As mentioned earlier, depending on a value from ServiceA, ServiceB can either be ServiceBImpl1 or ServiceBImpl2. Registering both services seems unnecessary.
I figured I might be able to use a lambda function to pull this off, but it seems like it cannot find the dependent service correctly:
.AddSingleton<ServiceB>(serviceList => {
var serviceA = serviceList.GetRequiredService<ServiceAImpl>();
var someResponseFromA = serviceA.doSomething();
if (someResponseFromA == 1)
{
return new ServiceBImpl1();
}
else
{
return new ServiceBImpl2();
}
})
doSomething in this case returns a null value always and complains that ServiceAImpl is not available (for context, doSomething is a blocking call). How would I get the values I want from ServiceAImpl to use to register ServiceB appropriately?
Share Improve this question edited Mar 14 at 7:56 Qiang Fu 9,4071 gold badge6 silver badges16 bronze badges asked Mar 14 at 0:54 SitarHeroSitarHero 214 bronze badges 2 |1 Answer
Reset to default 4ServiceAImpl
is the implementation of ServiceA
. It is not directly registered to service. So you should use
var serviceA = serviceList.GetRequiredService<ServiceA>();
Only when you register the implementation directlybuilder.Services.AddSingleton<ServiceAImpl>();
,then you can use
var serviceA = serviceList.GetRequiredService<ServiceAImpl>();
ServiceA
– Sir Rufo Commented Mar 14 at 4:20serviceA.doSomething()
) while resolving services, because that makes resolving, slow and unreliable, and wheneverdoSomething
becomes async, it becomes even impossible to do this, because the registration and resolution process is completely synchronous. Instead, consider using the Factory or Adapter patterns. Which one to use, I can't say. For better advice, please update your question and show usages ofServiceB
. – Steven Commented Mar 14 at 7:55