IServiceCollection serviceConfiguration = ConfigureServices();
IServiceProvider serviceProvider = serviceConfiguration.BuildServiceProvider();
IBusControl busControl = CreateServiceBus(serviceProvider);
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IBusControl>(busControl);
IServiceProvider serviceBus = services.BuildServiceProvider();
是否有可能这样做,我需要它,因为CreateServiceBus使用第一个IServiceProvider,它通过依赖注入使用它为什么我需要2个IServiceProvider因为在此之后我想要注册busControl作为IBusControl
答案 0 :(得分:2)
您可以创建工厂方法并在Startup.cs
中加热。
services.AddSingleton<IBusControl>(provider => CreateServiceBus(provider));
然后在Configure
方法
public void Configure(IAppBuilder app, IBusControl bus)
{
}
或
public void Configure(IAppBuilder app, IBusControl bus)
{
app.Services.GetRequriedService<IBusControl>();
}