基于.NET Core 3.1 网站开发和部署的方法(10)

public class HomeController : Controller { private readonly INewsManager newsManager; private readonly ISuggestionManager suggestionManager; private readonly IRecruitmentManager recruitmentManager; public HomeController( INewsManager newsManager, ISuggestionManager suggestionManager, IRecruitmentManager recruitmentManager) { this.newsManager = newsManager; this.suggestionManager = suggestionManager; this.recruitmentManager = recruitmentManager; } // ... }

5.测试

启动项目也没有什么问题

七、项目发布

1.项目配置

dotnet core 中没有Web.conf文件了。
查看文档,都是通过Startup.cs中配置项目的。
暂时放弃配置。

2.命令行发布项目

CLI 提供了发布项目的相关命令

dotnet publish -c Release --no-self-contained -o /path/to/save/project/

3.另一种方式使用vs发布

很简单,下一步下一步做就好了。

八、通过Nginx部署到Linux服务器

1.在Centos7 上安装运行时

Register Microsoft key and feed

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

Install the ASP.NET Core runtime

sudo yum install dotnet-sdk-3.1

2.安装libgdiplus

因为项目中使用验证码,需要用到这个命名空间:System.Drawing.Common
速度太慢,放弃。

3.将项目目录上传到linux

使用xshell 的ftp 轻松完成。

基于.NET Core 3.1 网站开发和部署的方法

4.测试项目是否运行

cd /var/www/HotelWeb/ dotnet HotelWeb.dll [root@centos7 HotelWeb]# dotnet HotelWebMVC.dll info: Microsoft.Hosting.Lifetime[0] Now listening on: :5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /var/www/HotelWeb

5.安装Nginx并配置

yum install nginx vim /etc/nginx/nginx.conf --------------- # 删除nginx默认的server,添加下面两个 server { listen 80 default_server; return 444; } server { listen 80; server_name *.hotel.com; location / { proxy_pass :5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ----------------

6.启动nginx

systemctl start nginx nginx -t # 配置确认没有问题后,重新载入配置 nginx -s reload

7.浏览器测试

修改win7的host

192.168.30.110

基于.NET Core 3.1 网站开发和部署的方法

8.最后的问题

因为libgdiplus这个库没有安装,所以进入验证码的页面会有报错。

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
---> System.DllNotFoundException: Unable to load shared library 'libgdiplus' or one of its dependencies.

9.修改dotnet监听端口

在Program.cs 文件中修改

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.ConfigureKestrel(serverOptions => { serverOptions.Listen(IPAddress.Any, 5000); }) .UseStartup<Startup>(); }); }

修改后

[root@centos7 HotelWeb]# dotnet HotelWebMVC.dll info: Microsoft.Hosting.Lifetime[0] Now listening on: :5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /var/www/HotelWeb [root@centos7 ~]# ss -tna State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:5000 *:* LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* ESTAB 0 0 192.168.30.110:22 192.168.30.88:1581 ESTAB 0 52 192.168.30.110:22 192.168.30.88:1516 LISTEN 0 128 :::111 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*

参考文档

连接字符串-EF

Entity Framework Core tools reference - .NET CLI

Architectural principles

CentOS 7 Package Manager - Install .NET Core

Add the Mono repository to your system

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wxgdfp.html