当前位置:网站首页>ASP. Net startup and running mechanism
ASP. Net startup and running mechanism
2022-06-26 03:43:00 【yuyue5945】
ASP.NET Core
- ASP.NET Core Operation mechanism of
- ASP .NET Core Start of
- ASP .NET Core Pipeline middleware
ASP.NET Core Operation mechanism of

Illustration
1、Web server: ASP.NET CORE Two kinds of servers are available :kestrel and HTTP.sys
- kestrel It's a cross platform Web The server
- HTTP.sys Can only be used in Windows in
2、Internet : In Wan , need windows When verifying , You can choose HTTP.sys
3、IIS、Apache、Nginx: Reverse proxy
ASP .NET Core Start of
The start-up flow chart is as follows

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}- Main: The starting point of the program ,.Net Core Applications are essentially console applications
- CreateDefaultBuilder: Create and configure WebHostBuilder, First call CreateDefaultBuilder, Make a series of configurations .
- UseStartup: Appoint StartUp To start the configuration file . stay StartUp To do two important jobs in
- ConfigureServices The method is to register the service
- Configure The method is to configure the pipeline , Used to specify how to handle each http Requested , For example, we can let this program know that I use mvc To deal with it http request , It is called app.UseMvc() This method will do .
- BuildWebHost: Generate WebHostBuilder And after a series of configuration , adopt CreateHostBuilder(args) Object to Build a IHostBuilder.
- Run: call IWebHost Of Run Method to get it running .
Reverse compile into CreateDefaulBuilder Methods can see the key keywords
public static IHostBuilder CreateDefaultBuilder(string[] args)
{
HostBuilder hostBuilder = new HostBuilder();
hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
hostBuilder.ConfigureHostConfiguration((Action<IConfigurationBuilder>)(config =>
{
config.AddEnvironmentVariables("DOTNET_");
if (args == null)
return;
config.AddCommandLine(args);
}));
hostBuilder.ConfigureAppConfiguration((Action<HostBuilderContext, IConfigurationBuilder>)((hostingContext, config) =>
{
IHostEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
bool reloadOnChange = hostingContext.Configuration.GetValue<bool>("hostBuilder:reloadConfigOnChange", true);
config.AddJsonFile("appsettings.json", true, reloadOnChange).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, reloadOnChange);
if (hostingEnvironment.IsDevelopment() && !string.IsNullOrEmpty(hostingEnvironment.ApplicationName))
{
Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
if (assembly != (Assembly)null)
UserSecretsConfigurationExtensions.AddUserSecrets(config, assembly, true);
}
config.AddEnvironmentVariables();
if (args == null)
return;
config.AddCommandLine(args);
})).ConfigureLogging((Action<HostBuilderContext, ILoggingBuilder>)((hostingContext, logging) =>
{
bool flag = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (flag)
logging.AddFilter<EventLogLoggerProvider>((Func<LogLevel, bool>)(level => level >= LogLevel.Warning));
logging.AddConfiguration((IConfiguration)hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
if (flag)
logging.AddEventLog();
logging.Configure((Action<LoggerFactoryOptions>)(options => options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId | ActivityTrackingOptions.TraceId | ActivityTrackingOptions.ParentId));
})).UseDefaultServiceProvider((Action<HostBuilderContext, ServiceProviderOptions>)((context, options) =>
{
bool flag = context.HostingEnvironment.IsDevelopment();
options.ValidateScopes = flag;
options.ValidateOnBuild = flag;
}));
return (IHostBuilder)hostBuilder;
}UseKestrel Specify the server to use Kestrel, If you use HttpSys, Need to use UseHttpSys. UseContentRoot Specify the root directory ConfigureAppConfiguration Read configuration file ConfigureLogging Configure log handler UseIISIntegration Configure the application to be in IIS Run in . If the application is not using IIS As a reverse proxy , that UseIISIntegration It won't have any effect . therefore , Even if the application is not IIS Running in the scheme , You can also safely call this method . UseDefaultServiceProvider Set the default dependency injection container .
ASP .NET Core Pipeline middleware
Request pipeline Handle http requests And back to responses That's the code that makes up request pipeline( Request pipeline ). middleware : We can use some programs to configure the request pipeline (request pipeline) To deal with requests and responses. Like processing validation (authentication) The program , MVC It's a middleware in itself (middleware).
When a request is received , The request will be handed over to the middleware pipeline composed of middleware for processing , Pipeline is composed of multiple middleware , Requests come in from one end of a middleware , Coming out of the other end of the middleware , Every middleware can be used for HttpContext Request start and end to process .

Blogger GitHub Address
Pay attention to the official account

边栏推荐
- navicat16无线试用
- [appium stepping pit] io appium. uiautomator2. common. exceptions. InvalidArgumentException: ‘capabilities‘ are mand
- 【Appium踩坑】io.appium.uiautomator2.common.exceptions.InvalidArgumentException: ‘capabilities‘ are mand
- 栖霞消防开展在建工地消防安全培训
- Do you want to add a key to the applet or for sequence?
- Analysis of technological changes in social robots
- Uni app custom selection date 2 (September 16, 2021)
- mysql 常用语句
- kotlin快速上手
- 个人用同花顺软件买股票安全吗?怎么炒股买股票呢
猜你喜欢

Add an "open search description" to the site to adapt to the browser's "site search"“

图扑软件数字孪生海上风电 | 向海图强,奋楫争先

小米电视的网页和珠宝的网页

给网站添加“开放搜索描述“以适配浏览器的“站点搜索“

MySQL数据库基础

Kotlin quick start

智能制造学习记录片和书籍

进度条
![[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time](/img/e3/1bedf03493283da327fef9ecc54542.jpg)
[hash table] improved, zipper hash structure - directly use two indexes to search, instead of hashing and% every time

Analysis of the multiple evaluation system of children's programming
随机推荐
Uni app custom navigation bar component
双碳红利+基建大年 | 图扑深耕水利水电绿色智能装备领域
jupyter notebook的插件安装以及快捷键
Analysis of technological changes in social robots
mysql 常用语句
The "eye" of industrial robot -- machine vision
路由跳轉之點擊列錶的操作按鈕,跳轉至另一個菜單頁面並激活相應的菜單
评价——层次分析
Cliquez sur le bouton action de la liste pour passer à une autre page de menu et activer le menu correspondant
Class diagram
Insect structure and Deconstruction
Redux thunk simple case, advantages, disadvantages and thinking
Partition, column, list
智能制造学习记录片和书籍
【哈希表】改进,拉链法哈希结构——直接用两个索引查找,不用每次都hash和%一遍
Non H5 end of uni app, regional setting of status bar on the top of mobile phone
Android gap animation translate, scale, alpha, rotate
类图
MySQL advanced Chapter 1 (installing MySQL under Linux) [2]
"Renegotiation" agreement