.NET Core开发部署

前言

本文将介绍在Windows 10操作系统上,用Visual Studio 2019开发一个基于MySQL数据库的WebApi项目,并发布到银河麒麟高级服务器操作系统V10上的详细过程。


一、创建.NET Core 3.1 WebApi项目

1. 创建项目,选择ASP.NET Core Web应用程序

.NET Core开发部署_第1张图片

2. 确认项目名称后,选择创建

.NET Core开发部署_第2张图片

3. 选择.NET Core框架,ASP.NET Core 3.1版本,选择API项目类型,点击创建

.NET Core开发部署_第3张图片

4. 按下图,打开NuGet管理界面

.NET Core开发部署_第4张图片

5. 切换到浏览标签

.NET Core开发部署_第5张图片

6. 安装Microsoft.EntityFrameworkCore.Tools 3.1.9

.NET Core开发部署_第6张图片

.NET Core开发部署_第7张图片

7. 参考上述方法,依次完成安装:

Microsoft.EntityFrameworkCore 3.1.9

MySql.Data.EntityFrameworkCore 8.0.22

 

8. 更新NuGet数据库

按照下图程序包管理打开控制台

.NET Core开发部署_第8张图片

输入Update-Database

.NET Core开发部署_第9张图片

二、反向工程,生成EntityFramework相关类

1. 本地已准备好MySQL8数据库sbtest,其中表结果及数据如下

  1. DROP TABLE IF EXISTS `sbtest1`;

  2. CREATE TABLE `sbtest1` (

  3. `id` int NOT NULL AUTO_INCREMENT,

  4. `k` int NOT NULL DEFAULT '0',

  5. `c` char(120) NOT NULL DEFAULT '',

  6. `pad` char(60) NOT NULL DEFAULT '',

  7. PRIMARY KEY (`id`),

  8. KEY `k_1` (`k`)

  9. ) ENGINE=InnoDB AUTO_INCREMENT=1000003 DEFAULT CHARSET=utf8mb4;

  10.  
  11.  
  12. LOCK TABLES `sbtest1` WRITE;

  13. INSERT INTO `sbtest1` VALUES (1000001,1,'1','1'),(1000002,2,'2','2');

  14. UNLOCK TABLES;

2. 打开cmd,切换到当前项目目录下。检查dotnet版本信息,参考如下

  1. C:\Users\yidam>cd "C:\Users\yidam\source\repos\WebApplication1\WebApplication1"

  2.  
  3. C:\Users\yidam\source\repos\WebApplication1\WebApplication1>dotnet --info

  4. .NET SDK (反映任何 global.json):

  5. Version: 5.0.100

  6. Commit: 5044b93829

  7.  
  8. 运行时环境:

  9. OS Name: Windows

  10. OS Version: 10.0.14393

  11. OS Platform: Windows

  12. RID: win10-x64

  13. Base Path: C:\Program Files\dotnet\sdk\5.0.100\

  14.  
  15. Host (useful for support):

  16. Version: 5.0.0

  17. Commit: cf258a14b7

  18.  
  19. .NET SDKs installed:

  20. 2.1.518 [C:\Program Files\dotnet\sdk]

  21. 3.1.403 [C:\Program Files\dotnet\sdk]

  22. 5.0.100 [C:\Program Files\dotnet\sdk]

  23.  
  24. .NET runtimes installed:

  25. Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]

  26. Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]

  27. Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]

  28. Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]

  29. Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]

  30. Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]

  31. Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

  32. Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

  33. Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

  34. Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

  35. Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  36. Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  37.  
  38. To install additional .NET runtimes or SDKs:

  39. https://aka.ms/dotnet-download

  40.  
  41. C:\Users\yidam\source\repos\WebApplication1\WebApplication1>

3. 执行反向工程指令

dotnet ef dbcontext scaffold "server=localhost;port=3306;database=sbtest;uid=root;pwd=abcDEF123___;CharSet=utf8mb4" Pomelo.EntityFrameworkCore.Mysql -f -o Models

结果

  1. C:\Users\yidam\source\repos\WebApplication1\WebApplication1>dotnet ef dbcontext scaffold "server=localhost;port=3306;database=sbtest;uid=root;pwd=abcDEF123___;CharSet=utf8mb4" Pomelo.EntityFrameworkCore.Mysql -f -o Models

  2. Build started...

  3. Build succeeded.

此时项目自动生成了一个Models目录,结构如下

.NET Core开发部署_第10张图片

其中

Efmigrationshistory.cs

  1. using System;

  2. using System.Collections.Generic;

  3.  
  4. namespace WebApplication1.Models

  5. {

  6. public partial class Efmigrationshistory

  7. {

  8. public string MigrationId { get; set; }

  9. public string ProductVersion { get; set; }

  10. }

  11. }

Sbtest1.cs

  1. using System;

  2. using System.Collections.Generic;

  3.  
  4. namespace WebApplication1.Models

  5. {

  6. public partial class Sbtest1

  7. {

  8. public int Id { get; set; }

  9. public int K { get; set; }

  10. public string C { get; set; }

  11. public string Pad { get; set; }

  12. }

  13. }

sbtestContext.cs

  1. using System;

  2. using Microsoft.EntityFrameworkCore;

  3. using Microsoft.EntityFrameworkCore.Metadata;

  4.  
  5. namespace WebApplication1.Models

  6. {

  7. public partial class sbtestContext : DbContext

  8. {

  9. public sbtestContext()

  10. {

  11. }

  12.  
  13. public sbtestContext(DbContextOptions options)

  14. : base(options)

  15. {

  16. }

  17.  
  18. public virtual DbSet Efmigrationshistory { get; set; }

  19. public virtual DbSet Sbtest1 { get; set; }

  20.  
  21. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

  22. {

  23. if (!optionsBuilder.IsConfigured)

  24. {

  25. #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.

  26. optionsBuilder.UseMySql("server=localhost;port=3306;database=sbtest;uid=root;pwd=abcDEF123___;charset=utf8mb4", x => x.ServerVersion("8.0.22-mysql"));

  27. }

  28. }

  29.  
  30. protected override void OnModelCreating(ModelBuilder modelBuilder)

  31. {

  32. modelBuilder.Entity(entity =>

  33. {

  34. entity.HasKey(e => e.MigrationId)

  35. .HasName("PRIMARY");

  36.  
  37. entity.ToTable("__efmigrationshistory");

  38.  
  39. entity.Property(e => e.MigrationId)

  40. .HasColumnType("varchar(150)")

  41. .HasCharSet("utf8mb4")

  42. .HasCollation("utf8mb4_bin");

  43.  
  44. entity.Property(e => e.ProductVersion)

  45. .IsRequired()

  46. .HasColumnType("varchar(32)")

  47. .HasCharSet("utf8mb4")

  48. .HasCollation("utf8mb4_bin");

  49. });

  50.  
  51. modelBuilder.Entity(entity =>

  52. {

  53. entity.ToTable("sbtest1");

  54.  
  55. entity.HasIndex(e => e.K)

  56. .HasName("k_1");

  57.  
  58. entity.Property(e => e.Id).HasColumnName("id");

  59.  
  60. entity.Property(e => e.C)

  61. .IsRequired()

  62. .HasColumnName("c")

  63. .HasColumnType("char(120)")

  64. .HasDefaultValueSql("''")

  65. .HasCharSet("utf8mb4")

  66. .HasCollation("utf8mb4_0900_ai_ci");

  67.  
  68. entity.Property(e => e.K).HasColumnName("k");

  69.  
  70. entity.Property(e => e.Pad)

  71. .IsRequired()

  72. .HasColumnName("pad")

  73. .HasColumnType("char(60)")

  74. .HasDefaultValueSql("''")

  75. .HasCharSet("utf8mb4")

  76. .HasCollation("utf8mb4_0900_ai_ci");

  77. });

  78.  
  79. OnModelCreatingPartial(modelBuilder);

  80. }

  81.  
  82. partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

  83. }

  84. }

三、添加控制器

1. 右键Controllers目录,添加一个控制器

.NET Core开发部署_第11张图片

2. 选择“MVC 控制器 - 空”

.NET Core开发部署_第12张图片

选择“API 控制器 - 空”

.NET Core开发部署_第13张图片

3. 编辑ValuesController.cs内容如下

  1. using Microsoft.AspNetCore.Http;

  2. using Microsoft.AspNetCore.Mvc;

  3. using System;

  4. using System.Collections.Generic;

  5. using System.Linq;

  6. using System.Threading.Tasks;

  7. using WebApplication1.Models;

  8.  
  9. namespace WebApplication1.Controllers

  10. {

  11. [Route("api/[controller]")]

  12. [ApiController]

  13. public class ValuesController : ControllerBase

  14. {

  15.  
  16. private readonly sbtestContext context = new sbtestContext();

  17.  
  18. // GET api/values

  19. [HttpGet]

  20. public ActionResult> Get()

  21. {

  22. return context.Sbtest1.ToList();

  23. }

  24.  
  25. // GET api/values/5

  26. [HttpGet("{id}")]

  27. public ActionResult Get(int id)

  28. {

  29. var item = context.Sbtest1.Where(c => c.Id == id).SingleOrDefault();

  30. return item;

  31. }

  32.  
  33. // POST api/values

  34. [HttpPost]

  35. public void Post([FromBody] Sbtest1 value)

  36. {

  37. context.Sbtest1.Add(value);

  38. }

  39.  
  40. // PUT api/values/5

  41. [HttpPut("{id}")]

  42. public void Put(int id, [FromBody] Sbtest1 value)

  43. {

  44. if (value.Id != id)

  45. {

  46. throw new Exception("id not equeal");

  47. }

  48. context.Sbtest1.Update(value);

  49. context.SaveChanges();

  50. }

  51.  
  52. // DELETE api/values/5

  53. [HttpDelete("{id}")]

  54. public void Delete(int id)

  55. {

  56. var item = context.Sbtest1.FirstOrDefault(c => c.Id == id);

  57. context.Sbtest1.Remove(item);

  58. context.SaveChanges();

  59. }

  60. }

  61.  
  62. }

四、运行测试

1. VS启动项目

.NET Core开发部署_第14张图片

2. 访问/api/values/

.NET Core开发部署_第15张图片

五、发布到银河麒麟高级服务器操作系统V10上

1. VS2019发布项目

.NET Core开发部署_第16张图片

.NET Core开发部署_第17张图片

.NET Core开发部署_第18张图片

.NET Core开发部署_第19张图片

得到发布输出

  1. 已启动生成…

  2. 1>------ 已启动生成: 项目: WebApplication1, 配置: Release Any CPU ------

  3. 1>C:\Users\yidam\source\repos\WebApplication1\WebApplication1\Models\sbtestContext.cs(25,10,25,214): warning CS1030: #警告:“To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.”

  4. 1>WebApplication1 -> C:\Users\yidam\source\repos\WebApplication1\WebApplication1\bin\Release\netcoreapp3.1\WebApplication1.dll

  5. 1>已完成生成项目“WebApplication1.csproj”的操作。

  6. 2>------ 发布已启动: 项目: WebApplication1, 配置: Release Any CPU ------

  7. 正在连接到 C:\Users\yidam\source\repos\WebApplication1\WebApplication1\bin\Release\netcoreapp3.1\publish\...

  8. WebApplication1 -> C:\Users\yidam\source\repos\WebApplication1\WebApplication1\bin\Release\netcoreapp3.1\WebApplication1.dll

  9. WebApplication1 -> C:\Users\yidam\source\repos\WebApplication1\WebApplication1\obj\Release\netcoreapp3.1\PubTmp\Out\

  10. Web 应用已成功发布 file:///C:/Users/yidam/source/repos/WebApplication1/WebApplication1/bin/Release/netcoreapp3.1/publish/

  11.  
  12. ========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========

  13. ========== 发布: 成功 1 个,失败 0 个,跳过 0 个 ==========

2. 通过FileZilla将publish目录上传到服务器/root/下

.NET Core开发部署_第20张图片

3. ssh登陆服务器,进入/root/publish目录,启动web服务

  1. [root@localhost ~]# cd /root/publish/

  2. [root@localhost publish]# dotnet WebApplication1.dll

  3. info: Microsoft.Hosting.Lifetime[0]

  4. Now listening on: http://localhost:5000

  5. info: Microsoft.Hosting.Lifetime[0]

  6. Now listening on: https://localhost:5001

  7. info: Microsoft.Hosting.Lifetime[0]

  8. Application started. Press Ctrl+C to shut down.

  9. info: Microsoft.Hosting.Lifetime[0]

  10. Hosting environment: Production

  11. info: Microsoft.Hosting.Lifetime[0]

  12. Content root path: /root/publish

4. 在服务器本地用curl检测接口

  1. [root@localhost ~]# curl https://localhost:5001/api/values -k

  2. [{"id":1000001,"k":1,"c":"1","pad":"1"},{"id":1000002,"k":2,"c":"2","pad":"2"}]

附:

服务器系统信息

  1. [root@localhost ~]# cat /etc/kylin-release

  2. Kylin Linux Advanced Server release V10 (Azalea)

  3. [root@localhost ~]# dotnet --info

  4. .NET Core SDK (reflecting any global.json):

  5. Version: 3.1.402

  6. Commit: 9b5de826fd

  7.  
  8. Runtime Environment:

  9. OS Name: kylin

  10. OS Version: V10

  11. OS Platform: Linux

  12. RID: linux-arm64

  13. Base Path: /root/dotnet/sdk/3.1.402/

  14.  
  15. Host (useful for support):

  16. Version: 3.1.8

  17. Commit: 9c1330dedd

  18.  
  19. .NET Core SDKs installed:

  20. 3.1.402 [/root/dotnet/sdk]

  21.  
  22. .NET Core runtimes installed:

  23. Microsoft.AspNetCore.App 3.1.8 [/root/dotnet/shared/Microsoft.AspNetCore.App]

  24. Microsoft.NETCore.App 3.1.8 [/root/dotnet/shared/Microsoft.NETCore.App]

  25.  
  26. To install additional .NET Core runtimes or SDKs:

  27. https://aka.ms/dotnet-download


总结

采用.NET Core 框架,可以轻松完成ASP.NET C# Web项目迁移到国产银河麒麟高级服务器操作系统V10上。

 

作者:叶强

你可能感兴趣的:(兼容适配,银河麒麟服务器V10,.net)