Discovery Program Files Asp net core3 in, Program.cs and universal host (translation)

introduction

Original Address

In this blog I will explore some of Asp.net core 3.0 application on the basis of function -. Csproj project files and source files Program. I will describe them from asp.net core 2.X in the default template is how to change, and explore the changes of Aspnetcore3.0 Api used.
***

Introduction

.Net core 3.0 will occur on September 23 NET Conf released on, but now has a preview version supported (Preview 8). The latest preview version and the final release version can not have too many changes, it is now beginning to try and view 3.0 added functionality of a good time.

The main update Net core 3.0 is a windows desktop application to run on the Net core, but Asp Net core has also increased a lot. Perhaps the biggest new feature is the server Blazor (I personally most interested in is the client version, but is not yet available), but there are a number of iterative changes and new features added to the Asp Net Core.

In this blog, I will explore some of the updates very "based".

If you intend to Asp net Core 2.X software to migrate to 3.0, be sure to review the Migration Guide

In this blog, when you create a new Asp Net core applications, such as you use dotnet new webapi, I will explore the .csproj file and Program.cs file. In the last article, I will compare the StartUp file relative to the 2.X version of how the change and Asp Net core used in the template What is the difference (such as Web, WebAPI, MVC)
***

Changes in the new project and shared framework

When you're finished creating a new Asp Net core project, and then open the .csproj file, which is basically like the following:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

</Project>

If you and Asp net core 2.X project file for comparison, there are similarities and differences as follows

  • <TargetFramework> No longer netcoreapp2.1 or 2.2, but netcoreapp3.0, and this is because we are the target framework 2.1 / 2.2 replaced with 3.0
  • <Project> Element is still Microsoft.Net.Sdk.Web, although has been updated to become ASP.NET Core 3.0, but your project file syntax is still no change
  • Microsoft.AspNetCore.App meta package does not exist.

Finally, an interesting change here. In my previous blog I mentioned that the Asp Net core2.X you reference framework shared metadata package named Microsoft.AspNetCore.App of. This shared framework offers a number of benefits, such as avoiding you manually install all of the individual packages in your application and allows you to scroll forward using runtime update features.

In Asp Net core3.0, Microsoft has not released this shared framework in the form of Nuget metadata packet, there is no version 3.0.0 Microsoft.AspNetCore.App. This shared framework is still the same as before by Net core installation, but you use slightly different in 3.0.

In Asp Net core2.X in order to refer to this shared framework, you would add the following code to your project file:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

In contrast, in 3.0 you want to use <FrameworkReference>the element

<ItemGroup>
  <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

"But pause," you say: "Why is my Asp Net Core project files without this?"
That's a good question, the answer is Microsoft.Net.Sdk.Webincluded by default.
***

There are no packets for sharing frame assembly

3.0 In addition one of the biggest changes is that you no longer need to install a separate program Nuget other parts of the framework of the share package . For example, the Asp Net Core 2.X, you can use such as Microsoft.AspNetCore.Authentication or Microsoft.AspNetCore.Authentication separate package to replace the entire frame is dependent on the package:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.1.0"/>
  <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.0"/>
</ItemGroup>

This is often most useful to the library, because the application always need to rely on a shared framework. However, Net Core3.0 in all this impossible. These Nuget package will not be released. Conversely, if you need to refer to one of the libraries in your project, you have to add this <FrameworkReference> element to your project

For example things EF Core and social authentication providers of these packages is another to note that they are no longer part of a shared framework , if you need to use these packages, you must manually install from nuget to your project.

For a complete list of these packages, access to the Github Issue
***

Program.cs file from 2.X to 3.0 changes

Asp Net Core Proram.cs file versions 2.X and 3.0 at first glance very similar. But there are many types changed, because in the Net Core 3.0, Asp Net Core to run on general purpose host has been rebuilt , using a separate Web Host has been replaced.

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>();
            });
}

General Host from version 2.1 was released, which is a very good idea, but I found it on a variety of issues , the main problem is that it had too much work for the library , it is fortunate that, in this 3.0 a change should be able to solve these problems.

To a large extent, this is the end result of changes produced and Net Core2.X version you used in the past are similar, but the method used to configure all of your app's configuration WebHost.CreateDefaultBuilder()has been replaced with two logical steps, the two independent the method is called:

  • Host.CreateDefaultBuilder() Responsible for configuring your app configuration, logs, and dependency injection container

  • IHostBuilder.ConfigureWebHostDefaults() All things responsible for the classic Asp Net Core applications add needed, such as: Kestrel configure and use a Startup.cs used to configure your DI container and middleware pipelines


General Host builder

As I said before, General Host is Asp Net core build 3.0 provides the basis. It also provides the basis of Microsoft.Extensions you previously used in applications * Asp Net core elements, such as: logs, configuration and dependency injection.

The following code is a simplified version of the Host.CreateDefaultBuilder() method . It 2.X version of WebHost.CreateDefaultBuilder()the same role. But I will briefly say something noteworthy changes.

public static IHostBuilder CreateDefaultBuilder(string[] args)
{
    var builder = new HostBuilder();

    builder.UseContentRoot(Directory.GetCurrentDirectory());
    builder.ConfigureHostConfiguration(config =>
    {
        // Uses DOTNET_ environment variables and command line args
    });

    builder.ConfigureAppConfiguration((hostingContext, config) =>
    {
        // JSON files, User secrets, environment variables and command line arguments
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        // Adds loggers for console, debug, event source, and EventLog (Windows only)
    })
    .UseDefaultServiceProvider((context, options) =>
    {
        // Configures DI provider validation
    });

    return builder;
}

In short, this method 2.X version differences are as follows:

  • Hosting configured to use the DOTNET_prefix environment variables
  • Hosting configuration using the command-line arguments
  • Increased EventSourceLoggerand EventLogLoggerlog provider
  • ServiceProvider can choose to use verification
  • No special configuration is about Web Hosting

The first place is interesting is how the Host configuration settings. For Web Host, by default to use ASPNETCORE_the prefix as an environment variable configuration. So to set ASPNETCORE_ENVIRONMENTenvironment variables will be set Environmentvalue configuration. For general Host, this prefix is now DOTNET_, and pass arbitrary command line arguments when running the application.

The host configuration like the role is to decide what your application is running in the host environment, host configuration and your application configuration (for use with IOptions interface configuration) is isolated from the.

Configure your app set of methods ConfigureAppConfiguration()and 2.X is no change compared to, so it still uses appsettings.json file, appsetting.ENV.json style files, users' confidential data , environment variables, and command-line parameters.

General Host Log section has been expanded in 3.0. It still come through your app configuration configuration log level filter , and add the console and Debug logs providers. However, it is also added to the event source log providers, and the event log is used as the source on Windows ETW , on Linux LTTng system logs such interaction. In addition, add an Event the Log Provider , only the log information is written in the windows will be Windows Event Log.

Finally, when your app running in the development environment, common Host configuration dependency injection container object it verifies range (, Scopes), and this operation 2.X same. This is intended instance dependency relationships crawl captured in these examples, you will be a range (Scopes) service injected into the singleton service. In 3.0, the generic Host GM can start ValidateOnBuildthe function, I will be talked about in the next blog post.

A key point is that he is versatile Host universal, and it Asp Net core or Http workload has no association. You can like the classic Asp Net core applications, like the Universal Host as the cornerstone of your console app or other long-running services. In 3.0, you only need to add one at the top of your Asp net core layer ConfigureWebHostDefaults()can handle that.
***

Asp Net core function recovery with ConfigureWebHostDefaults

This blog is a long time, so here I do not want to dig too much detail, but, for adding "layers" Asp Net core to Host General terms above, ConfigureWebHostDefaults extension method is very useful. It is a simple level, calling this method will Kestrel web host services are added to the top, but there also exist a number of other changes . Here is this method provides a Glance , (includes GenericWebHostBuilder features provided)

  • Host configuration for the addition of ASPNETCORE_the prefix environment variables (except DOTNET_the prefix variables and command-line arguments)
  • An increase of GenericWebHostServicethis is to achieve a IHostedService, which usually run on Asp Net core service, which is a major feature, this feature is Asp Net core complex offers the possibility to use common Host.
  • It adds an extra app configuration source, in RazorUI class library, the StaticWebAssetsLoaderstatic files (css / js) function together.
  • Kestrel using the default configuration (with the same 2.X)
  • Increase HostFilteringStartupFilter(and the same as 2.0)
  • Increases ForwardedHeadersStartupFilter, if the ForwardedHeaders_Enabledconfiguration value is true, for example, if the ASPNETCORE_FORWARDEDHEADERS_ENABLEDvalue of the environment variable is true.
  • IIS on windows start integration
  • Increasing a endpoint routing the vessel DI

Most of what is and Asp net core2.x same, different places are: app infrastructure as a IHostService running routes and end points ForwardedHeadersStartupFilter, which is 3.0 endpoint routing globally enabled (no longer limited to MVC 2.X / Razor the page)

ForwardedHeadersStartupFilterIn 1.0 there have been, when your app runs behind a proxy will use it, in order to ensure that your application can handle SSL- load and generate the correct URL. The purpose of such a design is that you can simply by setting an environment variable to configure a use X-Forwarded-Forand X-Forwarded-Protorequest middleware head.
***

to sum up

In this blog I dig deep from Asp Net Core2.X to 3.0 in only two files - files and change csproj Program.cs file. On the surface, those are just a few minor changes, all from 2.X to 3.0 transplant should not be too difficult. It is naive to cover up the tremendous changes which - significant changes shared framework, as well as Asp Net core has been rebuilt on a common Host.

I think the biggest problem we encountered was the difference Nuget package - some of the app will have to be removed Asp Net Core program references the package, while explicitly refer to other packages. Although the solution to this problem is not too difficult, but it will not change the user's familiar with the confusing, so it should be the first time to review these changes

Guess you like

Origin www.cnblogs.com/blue-tian/p/11456489.html