Asp.net core release deployment model choice

Foreword

Before writing the code when preoccupied with writing the business logic code, release and deployment of ASP.net core basically did not get to know, the attitude of deployment models are run up on the line, this time free of this serious intention Dislike pit.
Looked at Microsoft's own documentation, know every word, but they do not understand what that means. Independent release, dependent on the framework, IIS, Kestrel, Nginx, select it and made a bunch of little disoriented.

Selection Process

As Microsoft's document said, we deploy an ASP.net core projects do need to select the appropriate deployment model according to our type of application or deployment environment.
The two-step selection process. First, select the deployment model. Second, select the managed host. The following description will unfold.

Deployment mode selection

Independent publishing framework relies VS

ASP.net Core currently deployed in three modes, independent release (SCD), frame-dependent (FDD), dependent on the framework of the executable file (FDE). FDD and FDE which is not very different, not discussed here, are interested can look at the official documentation.
https://docs.microsoft.com/zh-cn/dotnet/core/deploying/

Independent release

Independent deployment. Unlike FDD, independently deployment (SCD) does not depend on the presence of shared components on the target system. All components (.NET Core .NET Core runtime library and including) are included in the application, and independently of the other .NET Core applications.

Framework relies

Dependence deployment framework. As the name implies, relies deployment framework (FDD) rely on shared system-level version of the .NET Core exist on the target system. Since the existing .NET Core, thus applied between .NET Core installer it is also portable. Application contains only its own code and any third parties located outside the .NET Core library dependencies.

how to choose

Simply put, the difference between dependence and independence framework release is the difference between core pretend not installed .Net runtime. After many .net core libraries can be run on shared equipment, otherwise you need to go in another package, so independent publishing program will depend on the volume is much larger than the frame. I ASP.net core default template to use measured, independent of the size of the release of about 100M, about the size of the frame dependent 5M.
Here Insert Picture Description
When independent release, then no need to install .net core operation, and rely on the framework you need to install .net core runtime or directly SDK.
So, the focus here. If your server usually are running other programs that only occasionally need to run a few special circumstances ASP.net core, it is recommended that you use a separate release mode. If you intend to run multiple server ASP.net core program, and the future is also intended to ASP.net core technology as a route, framework relies is your best choice.
Here the following Windows .Net Core 2.2, for example, only 89M .net core runtime. So, if you run more than two ASP.net core or want to save the students, choose a frame mounted directly dependent on, provincial and easy.
For Linux, Windows and above, as a place to note is that, if you choose an independent release, .net core runtime is not installed, then can not use dotnet xxx.dll this command to run the program, cd into the directory program will then ./ xxx, xxx is the filename of your output.

Managed hosting choice

IIS VS Kestrel VS Windows Service

After selecting a deployment mode in relation to consider when choosing managed hosting, managed hosting selection reflects the Program.cs in ASP.net core application, the method can be selected in CreateWebHostBuilder UseIIS or UseKestrel.
According to Microsoft documentation, we have three kinds of hosting selection under Windows: IIS, Kestrel, Windows services. First, Windows service is essentially a Kerstrel, but ran in the service where you can do the boot from Kai; IIS same function can be done. Select managed hosting how embodied in the code? The following code illustrates the meeting. Here we discuss the different circumstances how to choose the host.
Because the service is essentially Windows Kerstrel (pro-test UseIIS simply not up and running). So here to discuss the difference between the IIS and Kerstrel. Here to learn about the big brother of the article, which compares the performance difference between IIS and Kestrel in detail.
https://www.cnblogs.com/savorboard/p/dotnet-benchmarks.html#asp.net-core-vs-asp.net-corekestrel-vs-iis
First of all, performance is better than Kestrel IIS this is the fact that iron , but it does not Kestrel reverse proxy functionality, simply, is that it can not do a port mapping multiple applications, if it is tied to the port 443 do https, other applications can not use port 443, but in IIS, a 443 tie multiple applications can be said that the basic operation.
Therefore, the application is typically deployed outside the network will not only use Kestrel, but will add a IIS, Nginx or Apache as a reverse proxy, Kestrel first deployed on a different port (say 8081), Nginx listening on port 443, a request is received according to the requested domain name to distinguish which application requests it, and then forwarded to the corresponding port (8081) process.
Here Insert Picture Description
This will produce a very embarrassing phenomenon, performance becomes determined by the reverse proxy or a Kestrel slowest. If I use IIS as a reverse proxy, no matter how tough you Kestrel performance, not to be stuck IIS? Why I do not use the IIS? Also it saves a step forward.
In addition, the Kestrel is the essence of a console application, if special circumstances it is really hung up hung up, will not boot from the start, it will not restart itself, of course, it can be used to deploy Windows service in Watchdog to restart it, but in IIS does not need to deal with these things.
So, we can not choose the host superstition performance, but also with the use of scenarios to choose from.
So now the focus here, if your application deployment within the network, carried out with IP + port access, then choose Kestrel on the right, to make it run more stable, it can be deployed in a Windows service. If your application is deployed outside the network, access by domain name, but also on the server to run more than one application, then it is recommended to use Kestrel + reverse proxy operation. But if you reverse proxy with IIS, then it is recommended that you use IIS as the host, the other accompanied by InProcess mode in the program, InProcess mode useful only if you use IIS, you can enhance certain properties. So, if your server has been running for some applications with IIS, in order to reduce maintenance complexity would not have to do so many bells and whistles, honestly go back to IIS, IIS performance is not bad reason.
In addition, here select reverse proxy, it is recommended to use Nginx.
Host selected code.

    public class Program
    {
        public static void Main(string[] args)
        {
            string runMode = "Kestrel"; //"IIS" "Kestrel" "Service"
            if (runMode == "Service")
            {
                var isService = !(Debugger.IsAttached || args.Contains("--console"));
                
                if (isService)
                {
                    var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
                    var pathToContentRoot = Path.GetDirectoryName(pathToExe);
                    Directory.SetCurrentDirectory(pathToContentRoot);
                }
                
                var builder = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray());
                var host = builder.Build();
                
                if (isService)
                {
                    host.RunAsService();
                }
                else
                {
                    host.Run();
                }
            }
            else
            {
                CreateWebHostBuilder(args).Build().Run();
            }
        }
        
        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            IWebHostBuilder builder = null;
            string runMode = "Kestrel"; //"IIS" "Kestrel" "Service"

            if (runMode == "IIS")
            {
                builder = WebHost.CreateDefaultBuilder(args)
                    .UseIIS()
                    .UseIISIntegration()
                    .UseStartup<Startup>();
            }
            else if (runMode == "Kestrel")
            {
                builder = WebHost.CreateDefaultBuilder(args)
                    .ConfigureKestrel((context, option) =>
                    {
                        option.Listen(System.Net.IPAddress.Any, 80, (cfg) =>
                        {
                            // 根据需要使用https
                            // cfg.UseHttps(caPath,caPsd);
                        });
                    })
                    .UseKestrel()
                    .UseStartup<Startup>();
            }
            else if (runMode == "Service")
            {
                builder = WebHost.CreateDefaultBuilder(args)
                    .ConfigureLogging((hostingContext, logging) =>
                    {
                        logging.AddEventLog();
                    })
                    .ConfigureAppConfiguration((context, config) =>
                    {
                        // Configure the app here.
                    })
                    .ConfigureKestrel((context, option) =>
                    {
                        option.Listen(System.Net.IPAddress.Any, 80, (cfg) =>
                        {
                            // 根据需要使用https
                            // cfg.UseHttps(caPath,caPsd);
                        });
                    })
                    .UseKestrel()
                    .UseStartup<Startup>();
            }

            return builder;
        }
    }

If you choose here Kestrel + reverse proxy and a reverse proxy https has been set, then the code can not just leave UseHttps, this is equivalent to twice https encryption and decryption operations, pro-test will be more significant loss of performance.

Operating system-related

Windows VS Linux

众所周知,Linux上是没有IIS这种东西的,以上关于托管宿主的讨论仅针对Windows系统。对于Linux系统下,首先根据需求选择部署模式(独立 or 框架依赖),然后宿主的话只能选Kestrel。如果是内网应用就没必要装反向代理了,如果是外网应用就需要用反向代理 + Kestrel + Linux服务的操作了,Linux Service和Windows Service的作用一样,都是为了使应用开机自启。
这里放上Asp.net core在Linux上的安装教程(可选装SDK或者只装运行时)。
安装 .net core SDK
https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install
仅安装 .net core 运行时
https://dotnet.microsoft.com/download/linux-package-manager/centos7/runtime-current
Nginx反向代理 + Kestrel + Linux Service教程
https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2

如果这篇文章有幸能帮助到你,请不要吝啬你的赞。

Reference
https://docs.microsoft.com/zh-cn/dotnet/core/deploying/
https://www.cnblogs.com/savorboard/p/dotnet-benchmarks.html#asp.net-core-vs- --VS-corekestrel ASP.NET IIS
https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install
https://docs.microsoft.com/zh-cn/aspnet/core/host- and-deploy / linux-nginx? view = aspnetcore-2.2

Published 15 original articles · won praise 2 · Views 4858

Guess you like

Origin blog.csdn.net/weixin_38138153/article/details/100529246