With the release of ASP. NET Core 2.0, the ASP. NET Webapi site, which was originally running in Windows IIS, can be run across platforms in Linux. We need to talk about ASP. NET Core first.
The first time that ASP. NET core is a new generation of ASP. ASP. VNext, which was later named as ASP. 5, as it was perfected and matured, it was eventually named ASP., which indicates that it is not an ASP. Instead, it is a redesigned web development framework. And it's a very important change is that it no longer relies on IIS, but rather a standalone self-hosted console application, which is also the root of its cross-platform.
Two keywords:IIS decoupling and standalone self-hosted console application.
Let's compare ASP. NET and ASP. ASP. NET is strongly dependent on Windows IIS because there are many methods in system.web that are called directly by IIS-related APIs, and it resides in the IIS process. However, the ASP. NET Core Runtime is a completely stand-alone console application that has its own Kestrel Server that can provide services directly to the outside. However, the functionality of the existing Kestrel server is relatively simple, so we still need a reverse proxy server to protect the Kestrel server. Therefore, deployed in a Linux environment, you can consider a combination of Nginx+kestrel server, Windows environment, support and direct integration of IIS (Useiisintegration), deployed in IIS.
We went to today's topic and migrated the original ASP. NET Webapi project to ASP. NET Core 2.0.
I. ASP. NET Core Webapi Project
Open VS2017 (version must be greater than 15.3), new project: ASP. NET Core Web application
New Solutions and projects:
Ii. ASP. NET Core webhost Construction and operation
As you can see, with the Program.cs class, with the main function, the console application is ready to run.
Continue to look, construct and start a webhost. The question comes, what is the mechanism of webhost? What if it's constructed?
Webhost.createdefaultbuilder is new in ASP. NET Core 2.0, under the Microsoft.aspnetcore namespace. Let's take a look at the source code of Createdefaultbuilder:
The entire Webhost object is constructed in such a way that:
1. Register the Kestrel middleware, specifying the server (HTTP server) to be used by the current webhost.
2. Set the Content root directory to use the root directory of the current project as the Contentroot directory.
3. Read the Appsettinggs.json configuration file, the Usersecrets in the development environment, and the environment variables and command line parameters.
4. Read the Logging node in the configuration file and configure the log system.
5. Add the Iisintegration middleware to run in IIS.
6. Under Set development environment, ServiceProvider Validatescopes is true to avoid obtaining the Scope instance directly in the Configure method.
7. Specify the Startup class to create the Webhost object through the build method.
In the main function, Buildwebhost (args). Run (); Run the created webhost.
Iii. Commissioning and operation
The action implementation in Valuescontroller:
Iv. ASP. NET WEBAPI Migration Assessment
Before we actually migrate the ASP. NET Webapi project, we want to evaluate the specific migration tasks:
- References to NuGet and DLL migrations
- Web. config file migration
- Custom WEBAPI Routing Migrations
- Controller migration
- HttpModule Extended Migration
- Various custom attribute migrations
Our service Gateway WEBAPI mainly involved in the above 6 parts, we can add and delete according to the actual situation.
Five or one-step migration of ASP. NET Core WebApi
1. Before migrating, first create an empty ASP. Core2.0 Webapi Project:
2. Migrating controller and model
The existing controller and model are re-carved in the new project, for example:
3. Migrate various attribute, attribute tags, for example: Custom exception handling tags in this example
4. migrating Web. config
For better compatibility with. NET legacy configuration files and configuration systems. Although NuGet was provided:
System.Configuration.ConfigurationManager
However, in fact, Web. config is designed for IIS, and it is recommended to use the Appsettings.json replacement transformation in the new ASP. NET Core. That is, modify the XML configuration file to be a JSON configuration file.
The core of the NameSpace:Microsoft.Extensions.Configuration
For details, refer to Microsoft's official profile:
Https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration
Configuration Read Example:
5. Webapi Custom Route Rewriting
In many WEBAPI projects, custom routing is adopted, such as our Commoncontroller, how is custom routing supported in ASP. NET Core2.0?
In the startup class, provide the Configure method:
Here, we add a custom routing configuration that corresponds to the commoncontroller we just made:
6. IHttpModule extended Migration ASP. Core2.0
The concept of middleware is provided in ASP. Core2.0, which supports chained extensions between requests and responses:
Specifically, you can refer to Microsoft Official information:
Https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware
Https://docs.microsoft.com/en-us/aspnet/core/migration/http-modules
Here, for example, we write our own custom middleware:
This is a clever way to extend the interface Iapplicationbuilder by extending the method,
Also load the extended custom middleware in the Startup Configure method:
These are the bits and pieces of technology we've been using to migrate WEBAPI sites to ASP. NET Core2.0, and share them with you.
Zhou
2017/9/28
Step by step to get you to do webapi migration ASP Core2.0