当前位置:网站首页>ASP. Net core introduction I
ASP. Net core introduction I
2022-07-04 13:46:00 【51CTO】
ASP.NET Core (Main Method )
Why? ASP.NET Core use Main Method ?
The most important thing to remember is ,ASP.NET Core Web The application is initially launched as a console application ,Main() The method is the entry point of the application . therefore , When we execute ASP.NET Core Web Application time , First, it looks for Main() Method , This is how execution begins . then ,Main() Methods will ASP.NET Configure and start it . here , The application will become ASP.NET Core Web Applications . If you look further Main() The body of the method , You will find that it passes the command line parameter args Pass as a parameter to call CreateHostBuilder() Method , As shown in the figure below .
As shown in the figure below ,CreateHostBuilder() Method returns the implementation IHostBuilder Object of the interface . The host is a static class , Can be used to create a with preconfigured defaults IHostBuilder example .CreateDefaultBuilder() Method creates a new instance of the host builder with preconfigured defaults . In the internal , It's configured Kestrel( be used for ASP.NET Internal Web The server )、IIS Consolidation and other configurations .
stay Main() In the method , Here it is IHostBuilder On the object , call Build() Method , This method actually generates Web host . then , It will make us asp.net Web Hosting our Asp.net Core Web Applications . Last , stay Web On a host , It's called Run() Method , This method will actually run Web Applications , And start listening for incoming HTTP request .
Start class (Startup)
When setting up the host , Also used IHostBuilder Class UseStartup() The extension method configures the startup class . There are two ways to start a class , As shown in the figure below .
ConfigureServices() Method to configure the services required by the application .Startup Class Configure() Method to set the request processing pipeline of the application .
CreateDefaultBuilder() What is the task performed by the method ?
As a setting Web Part of the host ,CreateDefaultBuilder() Method performs several operations . Some of them are as follows :
- Set up Web The server
- Load host and application configurations from various configuration sources
- Configure logging
Let's talk about CreateDefaultBuilder() Method on configuration and setting Web The role of the server . From a hosting perspective , One ASP.NET Web Applications can be hosted in two ways , Process hosting (InProcess) Or out of process hosting (OutOfProcess).
notes : When we use any template to create a new ASP.NET Core Web Application time , By default , Use InProcess Create a managed project file , This hosting is used in IIS or IIS Express Managed applications in the scenario .
How to verify whether it is in process ?
To test the above point , Open project properties . Right click on the item , Then select the properties option... From the context menu . After opening the properties window , choice " debugging " And look at " Hosting model " The value of the drop-down list , As shown in the figure below . The drop-down list contains three values , Default value ( In process )、 In process and out of process .
that , Confirm that by default it uses " In progress " Hosting model .
How to be in ASP.NET Core Middle configuration InProcess trusteeship ?
for ASP.NET Core Web Application configuration InProcess trusteeship , There is only one simple setup , Just put the Element is added to a with a value of InProcess In the application project file . So , Just right-click the application from Solution Explorer , Then click context Menu " Edit project files " Options , As shown in the following code .
image-20200923095508271 After opening the application project file , Please modify it , As shown below . Add... Here <AspNetCoreHostingModel> Element and set its value to InProcess. This node can also be OutOfProcess.
Set the managed model to InProcess What happens when ?
stay InProcess In the case of custody ( When CreateDefaultBuilder() Treat the value as in the project file AspNetCoreHostingModel Elemental InProcess when ), Behind the scene CreateDefaultBuilder() Method internal invocation UseIIS() Method . And then in IIS Working procedure ( namely iIS Of w3wp.exe and Iisxpress.exe Of IISExpress) Managed applications in . From a performance perspective ,InProcess The request throughput provided by the managed model is significantly higher than OutOfProcess Hosting model . stay IIS in , The name of the process that will be used to execute the application is w3wp. Again , If it is IIS Express, Then the process name will be iisexpress.
How to determine if you are InProcess Process execution ?
By default , If you want to run the application , Then you will see the following in the browser “Hello World!” Output .
below , Marked output “Hello World” Code snippet for location , be located Startup Class Configure() In the method , As shown below .
To confirm that the application is in InProcess Process execution , take "Hello World!" Modify to display the current process name , By calling : System.Diagnostics.Process.GetCurrentProcess().ProcessName Get the current process name , As shown below .
Start the program again , The following information will be displayed in the browser , As shown below .
This is because by default ,Visual Studio When the application is running IISExpress, As shown in the figure below .
What is? IIS Express?
IIS Express yes IIS Lightweight self-contained version of . It's for Web Application development is optimized . The most important thing you need to remember is , We only use... In development IIS Express, Not production . In production , We usually use IIS.
Why? InProcess Than OutOfProcess Provide better performance ?
Out of process (OutOfProcess) In the case of custody , Yes 2 platform Web The server
- An interior Web The server
- An external Web The server .
Inside Web The server is called Kestrel, external Web The server can be IIS、Nginx or Apache. Use InProcess When hosting a model , only one Web The server , namely IIS. therefore , about InProcess Hosting model , We are inside and outside Web There is no performance loss for navigation requests between servers . This is it. InProcess Managed models provide better performance than OutOfProcess The reason for the higher request throughput of the managed model .
InProcess Hosting( Process hosting )
In the above content, it is said that , We can right-click to select Properties , Change the managed model of the application , You can also right-click to edit the project file <AspNetCoreHostingModel> Node to set , As shown below .
stay ASP.NET Core in , Use Inprocess Hosting model , Our app will be in IIS Managed in the working program . The most important point , Here's the thing to remember , We only have one Web The server , namely IIS In the case of servers ,InProcess trusteeship , This will host our application , As shown in the figure below .
What is? Kestrel Web The server ?
ASP.NET Core It's a cross platform framework . This means that it supports different types of operating systems ( for example Windows,Linux or Mac) Develop and run applications on . Kestrel yes ASP.NET Core Cross platform application Web The server . This means that the server supports ASP.NET Core All platforms and versions supported . By default , It is contained in .NET Core As an internal... In the application Web The server . Kestrel Web Servers are often used as edge servers , That is, facing Internet Of Web The server , It handles incoming data directly from the client HTTP request . about Kestrel Web The server , Used to carry and operate ASP.NET Core The name of the application process is Project name .
How to use Kestrel Web The server runs the application ?
In the use of Kestrel Before the server runs the application , Let's first open... In the properties folder of the application launchSettings.json file . open launchSettings.json After the document , By default , You will find the following code .
ad locum , You can see , We have two parts . One for IIS Express(IIS The server ), Another for Kestrel The server . stay Visual Studio in , You can find the above two configuration files (IIS Express and FirstCoreWebApplication), As shown below .
If you choose IIS Express, Then it will use IIS The server , If you choose FirstCoreWeb Applications , Then it will use Kestrel The server .
Use IIS Express Running the application :
If you use IIS Express Running the application , Then it will use the startup settings .json Mentioned in the document URL And port number . To prove this, use IIS Express Running the application , See the output shown below ( On the left is the configuration file 、 On the right is the browser output ).
Use Kestrel The server runs the application :
In order to use Kestrel The server Visual Studio Run the application , First , You need to change your startup method to your current application name , As shown below .
choice After application mode startup , Now run the application . ad locum , We need to observe two things . First , It will launch a command prompt and use Kestrel Server hosted applications , As shown below . ad locum , You need to pay attention to URL And port number , And it should be launchSettings.json Of documents WebApplication4( Here, according to your actual program name ) Mentioned in the configuration file URL And port number .
See the output shown below ( On the left is the configuration file 、 On the right is the browser output and open .NETCLI Output window ).
notes : In this example , about IIS Express, The port number is 12166, The worker process is iisexpress, And for Kestrel The server , The port number is 5000, The worker process name is WebApplication4 Applications ( It's just your application name ).
How to use .NET Core CLI function .NET Core Applications ?
We can also use .NET Core CLI Run... From the command line ASP.NET Core Applications . CLI Represents the command line interface . When we use .NET Core CLI function ASP.NET Core Application time ,.NET Core The runtime will Kestrel Used as a Web The server . Later articles will discuss in detail .NET Core CLI. Now? , Let's see how to use .NET Core CLI The command runs .NET Core Applications . First , Press down windows+r Input cmd, You can enter cmd window , Next input , "dotnet --" , Press down enter after , As shown below :
Use CLI( The above order ) You can use new Command to create a new project , You can also use build Command to build the project , Or you can use publish Command to publish the project . have access to CLI Restore .net Dependencies and tools required for core projects . Use .NET Core CLI function .NET Core Applications : In the window just now , We use cd command , Move to the project path we created , And then execute “ dotnet run” command , As shown in the figure below .
Input dotnet run After the command , Press enter key , then .NET Core CLI Build and run applications , It is the same way as the application startup selected above , cmd The window will output the of your current application URL, But with the VisualStudo Different , It doesn't give you the default to open this address with a browser , You can choose to enter the address in the browser , The corresponding content will be displayed .
What is? ASP.NET Core Out of process hosting in (OutOfProcess)?
about ASP.NET Core OutOfProcess Hosting model , There are two Web The server .
- Inside Web The server , namely Kestrel Web The server
- And can use IIS、Apache and Nginx The outside of the Web The server .
The most important thing you need to remember is that you use OutOfProcess The managed model runs the application , external Web Server can use , You can also not use .
Method 1:
We can use Kestrel Web The server is oriented to Internet Of Web The server , It will deal directly with the incoming HTTP request . under these circumstances , Use only Kestrel The server , Instead of using another server , That is, the outside Web The server . therefore , When we use .NET The core CLI When running the application ,Kestrel Is the only way to process and process incoming HTTP Requested Web The server , As shown in the figure below .
If you want to confirm this , Then open the command prompt and run the application , As shown in the figure below . stay CMD In the window , First go to the... You created Web Folder location of the application , Then input dotnet run command .
notes : dotnet cli in , There are many command line interfaces available for use , The role will be explained in detail later .
type dotnet run command And press enter Post key , It will generate 、 Hosting and running applications , As shown in the figure below .
Now? , Put the address marked in red , Enter into the browser , You'll see the following ( If you follow the tutorial above to modify the output ):
therefore , under these circumstances ,Kestrel Is the only way to pass processing and processing into HTTP Requested server .
Method 2:
Kestrel Web The server can also be connected with the reverse proxy server ( Such as IIS、Apache or Nginx) A combination of . Now? , The question you think of is , If Kestrel Can be used as a Web The server , You can directly process and process incoming HTTP request , So why do we need a reverse proxy server ? This is because the reverse proxy server provides Kestrel The server cannot provide Additional configuration and Security layer . It also maintains load balancing . therefore , Use Kestrel Server and reverse proxy server are good choices .
therefore , When we use Kestrel Server And reverse proxy server , The reverse proxy server will receive an incoming message from the client HTTP request , Then forward the request to Kestrel The server does the processing . once Kestrel Server Process the request , Then it sends the response back to the reverse proxy server , And then through Internet Send the response back to the requesting client , As shown in the figure below .
When we directly from Visual Studio When running the application , By default Visual Studio Use IIS Express. Now change AspNetCoreHostingModel Element value , The project file of the application shown below .
OutofProcess
Because we have configured the out of process hosting model , Now? IIS Express Acting as a reverse proxy ,Kestrel Act as an internal Web The server . Now? ,IIS Express Receive incoming HTTP request , Then forward it to Kestrel Web The server does the processing .Kestrel Web The server processes the request and sends the response back to IIS Express, and IIS Express Send the response back to the client , That is, send to the browser . Now run the application , You think of the work process as the project name . therefore , When you use " Out of process hosting " Model time ,Kestrel Web The server will host the application and process the request , Whether you use a reverse proxy server or not .
Use .NET CLI Enable ASP.NET Core What happens when the application ?
When we use .NET Core CLI When running the application , By default , It will ignore your project files in the application ( namely csproj file ) Managed settings specified in . therefore , In that case ,AspNetCoreHostingModel The value of the element will be ignored . .NET Core CLI Always use a OutOfProcess Hosting model , and Kestrel Is to host ASP.NET Core Application and process HTTP Requested Web The server .
Whether it is possible to use the built-in Kestrel Web Run without a server asp.net Core applications ?
CLI When running the application , By default , It will ignore your project files in the application ( namely csproj file ) Managed settings specified in . therefore , In that case ,AspNetCoreHostingModel The value of the element will be ignored . .NET Core CLI Always use a OutOfProcess Hosting model , and Kestrel Is to host ASP.NET Core Application and process HTTP Requested Web The server .
Whether it is possible to use the built-in Kestrel Web Run without a server asp.net Core applications ?
yes . When we use InProcess Hosting Model time , Then host the application in IIS In the process of work , That is for IIS by w3wp.exe, about IIS Express by iisexpress.exe. It means Kestrel Web The server is not connected to InProcess Use with managed models .
边栏推荐
- After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
- FS4056 800mA充电ic 国产快充电源ic
- Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
- C语言宿舍管理查询软件
- Optional values and functions of the itemized contenttype parameter in the request header
- Database lock table? Don't panic, this article teaches you how to solve it
- WPF double slider control and forced capture of mouse event focus
- After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
- Comparative study of the gods in the twilight Era
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
猜你喜欢
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
CVPR 2022 | TransFusion:用Transformer进行3D目标检测的激光雷达-相机融合
CA: efficient coordinate attention mechanism for mobile terminals | CVPR 2021
JVM系列——栈与堆、方法区day1-2
n++也不靠谱
Reptile exercises (I)
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
【AI系统前沿动态第40期】Hinton:我的深度学习生涯与研究心法;Google辟谣放弃TensorFlow;封神框架正式开源
JVM series - stack and heap, method area day1-2
嵌入式编程中五个必探的“潜在错误”
随机推荐
8 expansion sub packages! Recbole launches 2.0!
#yyds干货盘点# 解决名企真题:连续最大和
读《认知觉醒》
Comparative study of the gods in the twilight Era
8个扩展子包!RecBole推出2.0!
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Reinforcement learning - learning notes 1 | basic concepts
C basic supplement
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
从0到1建设智能灰度数据体系:以vivo游戏中心为例
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
Cors: standard scheme of cross domain resource request
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
unity不识别rider的其中一种解决方法
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
WPF双滑块控件以及强制捕获鼠标事件焦点
高效!用虚拟用户搭建FTP工作环境
eclipse链接数据库中测试SQL语句删除出现SQL语句语法错误