The .NET Core Web Api published to Linux (CentOS 7 64)

The .NET Core (2.1) Web Api published to Linux (CentOS 7 64)

   In recent linux learn something related, then happened to want to try to release core applications to Linux, after all, cross-platform. try it. Here are some of collating information I found online to do.

   What You Need: Xshell, WinSPC, VM (we are here temporarily adopt a CentOS 7 VM installed the system as a test case)

 

 1. First create a .net core of web api application. Here Select .net core version 2.1 is selected, after the establishment of good, will automatically have a controller ValuesController, as shown:

 

 

 

2.NuGet introduced Swagger, [Startup] code is as follows:

 

 

 1 public class Startup
 2     {
 3         public Startup(IConfiguration configuration)
 4         {
 5             Configuration = configuration;
 6         }
 7 
 8         public IConfiguration Configuration { get; }
 9 
10         // This method gets called by the runtime. Use this method to add services to the container.
11         public void ConfigureServices(IServiceCollection services)
12         {
13             services.AddMvc () SetCompatibilityVersion (CompatibilityVersion.Version_2_1);.
 14              // configuration Swagger
 15              // Register Swagger generator, defining a document Swagger 
16              services.AddSwaggerGen (C =>
 . 17              {
 18 is                  c.SwaggerDoc ( " V1 " , new new info
 19                  {
 20                      Version = " v1 " ,
 21                      the Title = " interface documentation " ,
 22                      the Description = "RESTful API"
23                 });
24                 // 为 Swagger 设置xml文档注释路径
25                 var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
26                 var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
27                 c.IncludeXmlComments(xmlPath);
28             });
29         }
30 
31         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32         public voidThe Configure (App IApplicationBuilder, IHostingEnvironment the env)
 33 is          {
 34 is              IF (env.IsDevelopment ())
 35              {
 36                  app.UseDeveloperExceptionPage ();
 37 [              }
 38 is              // Enable middleware services generated Swagger 
39              app.UseSwagger ();
 40              // Enable generating middleware services Swagger, Swagger JSON specified endpoint 
41 is              app.UseSwaggerUI (C =>
 42 is              {
 43 is                  c.SwaggerEndpoint ( " /swagger/v1/swagger.json " , " the Web Vl the App " );
44 is                  c.RoutePrefix = String .Empty; // set the root access 
45              });
 46 is              app.UseMvc ();
 47          }
 48      }

 

 Generation run at the following results:

 

 

 

 

2. The project release, issued the following configuration:

 

 

 

 

3. A loading system with a CentOS 7 64 VM (image files here, too, it did not pass, the Internet to find a self). Bahrain set up after the system-related documents, account password. And then start the virtual machine.

 

 

After successful login via account password, execute command ifconfig, get the IP of the virtual machine. Then you can use Xshell, WinSPC tools to connect the virtual machine.

 

 

 

4. tops .net core Linux operating environment, if it is .net core 2.1 need to execute two commands (address: https://dotnet.microsoft.com/download/linux-package-manager/centos7/sdk-2.1.802 )

 

Sudo rpm Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

  

sudo yum install dotnet-sdk-2.1

  

 

 Two or more commands are executed, if there is encountered y / n selected directly hit enter to enter y. After the installation is complete. Enter the command dotnet --version If you get the following result, the proof of your .net core environment has been set up is completed:

 

 

5. Use tools WinSPC, you need to enter the virtual machine address and account password, connection later built a folder in the root directory, there are two ways to build.

 

1 (through linux command to create, create a file named web folder)

mkdir web

2 (direct manual right in winSPC new folder.

 

 

 

 

 

Publish good .net core package manually paste directly into the web directory. By entering the command cd web directory, and then enter the command ls current file. Figure

 

 

 

The next step was to start the copied service, and with the linux command dotnet * .dll, where * is the representative of your executable file. As shown in the figure, then I, my complete command would be:

 

 

 

Appears above tips, then prove that the server has started successfully, and through this address, we try directly in the machine, can access.

 

 

 

 

 

        These results appear, then the proof of our core services have been successfully launched successful on Linux.

 

Guess you like

Origin www.cnblogs.com/SuperPander/p/11843067.html