0
点赞
收藏
分享

微信扫一扫

ASP.NET Core发布

一 ASP.NET Core Windows启动的几种方式

ASP.NET Core 在Windows上有两种发布方式:1用命令行启用控制台发布;2 使用IIS发布

  • 1 控制台直接启动:
    运行命令 dotnet APIServer.dll

这种方式发布只能在本机访问,需要修改Program.cs文件,以允许其他主机访问
手动指定启动的Url为:http://*:5000

 public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:5000");
}

http://*:5000 可以兼容 http://localhost:5000,http://127.0.0.1:5000,http://所在机器ip:5000

PS E:\发布\ServerCore> dotnet ApiServer.dll
Hosting environment: Production
Content root path: E:\发布\ServerCore
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
Application is shutting down...
PS E:\发布\ServerCore>

还可以将发布的IP设置在配置文件中,便于修改
在配置文件appsettings.json添加一项:
"Server.urls": "http://*:5000;http://*:5005;"
修改启动类Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var hostConfiguration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
//.UseUrls("http://*:5000;http://*:5005;");
.UseUrls(hostConfiguration["Server.urls"]);
}

在项目的发布文件下:运行命令 dotnet APIServer.dll

PS E:\GIT\ApiServer\bin\Debug\netcoreapp2.1> dotnet ApiServer.dll
Hosting environment: Production
Content root path: E:\GIT\ApiServer\bin\Debug\netcoreapp2.1
Now listening on: http://[::]:5000
Now listening on: http://[::]:5005
Application started. Press Ctrl+C to shut down.

再次监听了两个端口,通过这两个端口都可以访问

上述方式是在代码或者配置文件指定端口号,当然这里你还可以在运行的时候指定端口号
dotnet + 启动项目的dll文件+ urls=http://IP:端口号
如:

PS E:\GIT\ApiServer\bin\Debug\netcoreapp2.1> dotnet ZDZN.IntegratedControl.ApiServer.dll urls=http://10.17.128.34:8099
Hosting environment: Production
Content root path: E:\.ApiServer\bin\Debug\netcoreapp2.1
Now listening on: http://10.17.128.34:8099
Application started. Press Ctrl+C to shut down.

常见问题:

  • IIS部署ASP.Net Core 502.5错误解决

查看windows错误日志查找报错原因


修改发布文件的web.config文件


修改processPath="dotnet"为完整的路径processPath="C:\Program Files\dotnet\dotnet.exe"


二 ASP.NET Core Linux发布方式

以Linux Ubuntu 18.04 x64系统为例
注册Microsoft密钥和订阅源
在安装.NET之前,您需要注册Microsoft密钥,注册产品存储库并安装所需的依赖项。这只需要每台机器完成一次。

打开命令提示符并运行以下命令:

wget -q https://packages.microsoft.com/config/ubuntu/18.04/
packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

安装.NET运行时
更新可用于安装的产品,然后安装.NET Runtime。

在命令提示符中,运行以下命令:

sudo add-apt-repository universe 
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install aspnetcore-runtime-2.2

定位到发布文件,执行dotnet ApiServer.dll命令

fcj@ubuntu:~/Desktop/ServerCore$ dotnet ApiServer.dll
Hosting environment: Production
Content root path: /home/fcj/Desktop/ServerCore
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.


在ubuntu发布成功

ubuntu + nginx 发布
安装:
sudo apt install nginx
启动:
sudo service nginx start
查看版本
sudo nginx -v
查看配置是否正确:
sudo nginx -t
修改配置文件nginx/sites-available/default
添加一个server

server {
listen 8011;
server_name fcjqwq.com *.fcj.com;
location / {
proxy_pass http://localhost: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;
}
}

nginx监听8011,把8011的请求转发到端口5000,由此需要把Asp.net core的程序发布到http://localhost:5000;

fcj@ubuntu:~/Desktop/ServerCore$ dotnet ZDZN.IntegratedControl.ApiServer.dll
Hosting environment: Production
Content root path: /home/fcj/Desktop/MCLServerCore
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
^CApplication is shutting down...

三 ASP.NET Core 使用docker发布

请参照我之前的文章Ubuntu 上对 ASP.NET Core Mvc进行 Docker 部署

举报

相关推荐

0 条评论