【发布时间】:2021-03-10 20:11:23
【问题描述】:
拥有net5.0 应用程序,我想利用NServiceBus.Extensions.Hosting 提供的UseNServiceBus(...) 扩展来构建端点,将其包装到IHostedService 并在应用程序启动/停止时运行/停止它。
不幸的是,我需要从容器访问其他类才能完全创建EndpointConfiguration。这是不可能的,因为没有容器 (IServiceCollection) 也没有提供者 (IServiceProvider)。
有什么诀窍,我可以解析传递给UseNServiceBus的lambda中的服务吗?
类似:
hostBuilder
.ConfigureServices((hostContext, services) =>
{
services
.AddSingleton<CoolEndpointConfigBuilder>()
.AddSingleton<SomeDependency>(sp =>
{
/*
* We can access sp in this context to resolve anything
* helpful in the process of SomeDependency's creation
*/
})
.AddOtherDependencies()
;
})
.UseNServiceBus(hostContext =>
{
// How to get it here?
IServiceProvider sp = PerformSomeVooDooToGetServiceProvider();
var cecb = sp.GetRequiredService<CoolEndpointConfigBuilder>();
/*
* Which assembles EndpointConfiguration using the myriad of classes
* injected to the CoolEndpointConfigBuilder's constructor
*/
return cecb.Build();
});
【问题讨论】:
标签: .net dependency-injection nservicebus