语境:
- 使用 .NET 依赖注入 (.NET Core 3.1)
- WebHostBuilder,使用 Startup.cs 进行配置
- 使用 ConfigureServices(IServiceCollection services) 配置主要服务
在这个方法中,我需要像这样调用单例服务的方法......
services.AddSingleton((sp) =>
{
var svc = sp.GetRequiredService<Dependency_A>();
var setConfig = new ConfigurationDataClass(svc);
setConfig = setConfig.CallMethod().Result;
return setConfig;
});
//-- Need to get value stored in setConfig here somehow without using
//-- services.BuildServiceProvider()
//-- Because it creates a whole other container which I want to avoid doing
//-- After getting setConfig.OtherData
//-- I'll use to pass other data to subsequent services that need it
不幸的是,由于现有设计结构的限制,我无法更改后者服务从配置数据服务接收数据的方式,我只能找到一个阻止调用来避免创建重复容器。任何建议表示赞赏,因为我想用尽所有可能性。