Having net5.0
app, I would like to utilize UseNServiceBus(...)
extension provided by NServiceBus.Extensions.Hosting
to build the endpoint, wrap it to an IHostedService
and run/stop it on app start/stop.
Unfortunately I would need to access other classes from container to fully create the EndpointConfiguration
. This is not possible, because there is no container (IServiceCollection
) nor the provider (IServiceProvider
).
Is there any trick, I could resolve services in the lambda passed to UseNServiceBus
?
Something like:
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();
});