当前位置:网站首页>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
边栏推荐
- Is it safe to open a fund account? How to apply
- Andorid hide the title bar of the system
- 等保备案是等保测评吗?两者是什么关系?
- Kotlin quick start
- WebRTC系列-网络传输之7-ICE补充之偏好(preference)与优先级(priority)
- Uni app QR code scanning and identification function
- 图扑软件数字孪生海上风电 | 向海图强,奋楫争先
- JS to achieve the effect of text marquee
- todolist未完成,已完成
- MySQL addition, deletion, query and modification (Advanced)
猜你喜欢
Xgboost, lightgbm, catboost -- try to stand on the shoulders of giants
When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
Group counting notes - instruction pipeline of CPU
[paper notes] supersizing self supervision: learning to grasp from 50K tries and 700 robot hours
Some mobile phones open USB debugging, and the solution to installation failure
MySQL addition, deletion, query and modification (primary level)
进度条
Double carbon bonus + great year of infrastructure construction 𞓜 deep ploughing into the field of green intelligent equipment for water conservancy and hydropower
HL7Exception: Can‘t XML-encode a GenericMessage. Message must have a recognized struct
Classic model - Nin & googlenet
随机推荐
Nebula Graph学习篇3_多线程完成6000w+关系数据迁移
Add an "open search description" to the site to adapt to the browser's "site search"“
Cloud Computing Foundation -0
WebRTC系列-网络传输之6-Connections裁剪
2022.6.25 - leetcode. Un doigt d'épée. 091.
Is it safe for Caicai securities to open an account in 2022?
Run multiple main functions in the clion project
Uni app custom navigation bar component
jupyter notebook的插件安装以及快捷键
Class diagram
【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)
优化——多目标规划
Multimedia elements, audio, video
Double carbon bonus + great year of infrastructure construction 𞓜 deep ploughing into the field of green intelligent equipment for water conservancy and hydropower
Sqlitestudio download address
Solve the problem that the input box is blocked by the pop-up keyboard under the WebView transparent status bar
Classic model - Nin & googlenet
进度条
Worm copy construction operator overload
Cliquez sur le bouton action de la liste pour passer à une autre page de menu et activer le menu correspondant