IHostBuilder
在.NET Core 2.1控制台应用程序中。Main看起来像这样:
public static async Task Main(string[] args)
{
var hostBuilder = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureServices(services =>
{
// Register dependencies
// ...
// Add the hosted service containing the application flow
services.AddHostedService<RoomService>();
});
await hostBuilder.RunConsoleAsync();
}
}
IWebHostBuilder
,我有
Configure()
让我这样做的方法:
public void Configure(IApplicationBuilder applicationBuilder, IHostingEnvironment environment)
{
// Resolve something unrelated to the primary dependency graph
var thingy = applicationBuilder.ApplicationServices.GetRequiredService<Thingy>();
// Register it with the ambient context
applicationBuilder.AddAmbientThingy(options => options.AddSubscriber(thingy));
// Use MVC or whatever
// ...
}
这允许我注册一些环境(使用环境上下文模式),而不是应用程序主依赖关系图的一部分(如您所见,我仍然使用容器来实例化它,这当然比手动更新它更可取。我们可以将其视为次要的环境依赖关系图。)
IServiceProvider
或者
IApplicationBuilder
. 在这种情况下,我如何实现相同的注册?