WEB主机通常由 Program 类中的代码配置、生成和运行。 Main 方法:
.NET 辅助角色服务模板会生成以下代码来创建通用主机:
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
//装配主机
services.AddHostedService<Worker>();
})
.Build();
host.Run();
配置生成器做了一些工作,作为主机初始配置
主机使用一些默认位置的配置进行配置(此时主机并未启动,在配置完毕后才会启动)
使用 ConfigureServices 方法 装配主机的服务
主机使用 ConfigureHostConfiguration 创建应用配置,主机可以多次调用ConfigureHostConfiguration 创建配置,后来的配置会覆盖前面的配置中的相同项,同样也会覆盖主机初始化后的配置的相同项
iHostBuilder.ConfigureAppConfiguration((context, configBuiler) =>{});
iHostBuilder.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddJsonFile("hostsettings.json", optional: true);
configHost.AddEnvironmentVariables(prefix: "PREFIX_");
configHost.AddCommandLine(args);
})
主机注入自定义服务
iHostBuilder.ConfigureServices((serviceCollection) =>{
serviceCollection.AddScoped<CustomServer>();
});
使用 ConfigureWebHostDefaults构建web服务主机
iHostBuilder.ConfigureWebHostDefaults((webBuilder) =>
{
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
//webBuilder.UseConfiguration(config);
//webBuilder.UseKestrel((builderContext, options) =>
//{
// options.Configure(builderContext.Configuration.GetSection("Kestrel"));
//});//kestrel主机
//webBuilder.ConfigureKestrel(options =>
//{
// options.ConfigureHttpsDefaults(listenOptions =>
// {
// // listenOptions.ClientCertificateMode = ClientCertificateMode.AllowCertificate;
// //listenOptions.ServerCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2("D:\\zhengshu\\lac.pfx", "123456");
// });
//});
webBuilder.UseStartup<Startup>();
})
使用 ConfigureLogging 配置日志组件的行为
iHostBuilder.ConfigureLogging((hostingContext, logging) =>
{
//从appsettings.json中获取Logging的配置
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
//添加控制台输出
logging.AddConsole();
logging.AddSimpleConsole(options => {
options.TimestampFormat = "yyyy-MM-dd HH:mm:ss";
});
//添加调试输出
logging.AddDebug();
})
配置完毕后通过 进行 Build() 生成了IHost 主机,并Rund() 启动主机
从上图我们可以看出,CreateDefaultBuilder()方法完成了五项主要任务:
从图中可以看出CreateDefaultBuilder然后调用。ConfigureWebHostDefaults方法,默认执行以下操作
现在所有的配置都已经创建好了,我们来看看build方法主要做了什么。首先,我们看一下源码
///
/// Run the given actions to initialize the host. This can only be called once.
///
/// An initialized
public IHost Build()
{
if (_hostBuilt)
{
throw new InvalidOperationException("Build can only be called once.");
}
_hostBuilt = true;
BuildHostConfiguration();
CreateHostingEnvironment();
CreateHostBuilderContext();
BuildAppConfiguration();
CreateServiceProvider();
return _appServices.GetRequiredService();
}
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host. Createdefaultbuilder (args) // enable a default universal host builder
.ConfigureAppConfiguration(config => {
//Register configuration files used within the application, such as database links, etc
Console.WriteLine("ConfigureAppConfiguration");
})
.ConfigureServices(service =>
{
//Register service middleware and other operations
Console.WriteLine("ConfigureServices");
})
.ConfigureHostConfiguration(builder => {
//The configuration of components needed for startup, such as the URL address of the listening port, etc
Console.WriteLine("ConfigureHostCOnfiguration");
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
}
本文链接:https://blog.nnwk.net/article/136
有问题请留言。版权所有,转载请在显眼位置处保留文章出处,并留下原文连接
Leave your question and I'll get back to you as soon as I see it. All rights reserved. Please keep the source and links
友情链接:
子卿全栈
全部评论