最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How do I create a singleton dependent on a value obtained by another service via dependency injection in .NET 8.0? - Stack

programmeradmin1浏览0评论

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
  • 2 You have to request the registered type ServiceA – Sir Rufo Commented Mar 14 at 4:20
  • In general, you should prevent calling into services (i.e. serviceA.doSomething()) while resolving services, because that makes resolving, slow and unreliable, and whenever doSomething 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 of ServiceB. – Steven Commented Mar 14 at 7:55
Add a comment  | 

1 Answer 1

Reset to default 4

ServiceAImpl 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>();

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论