1. 程式人生 > >在ubuntu16.04中再次體驗.net core 2.0

在ubuntu16.04中再次體驗.net core 2.0

vscode flow art .dll 設置 wan 現在 src color

  在上一篇文章中在ubuntu16.04中初次體驗.net core 2.0 簡單介紹了一下ubuntu中運行.net core 2.0、配置nginx反向代理以及安裝supervisor守護進程……本來安裝supervisor(對他不是很了解,在其他的文章中看到這麽用)目的是讓我們應用程序開機啟動,但是現在win10 Linux子系統用不了systemctl這個命令:https://github.com/Microsoft/WSL/issues/1990 (不知道理解的對不對),之後看到微軟官方文章,不安裝supervisor也可以實現,可以采用XXX.sevice的方式。

  這次筆記沒有在win10 Linux子系統中進行,而是在Ubuntu虛擬機(vmware-ubuntu 16.04)中。安裝.net core 2.0、nginx都很順利,配置也和上一篇沒有區別,這裏就不在截圖了。

  一、配置監視應用程序

  直接創建服務文件,參考文章:使用 Nginx 在 Linux 上為 ASP.NET Core 設置托管環境,並對其進行部署 來張截圖:

技術分享

  1、在etc/systemd/system 中新建kestrel-test1.service ,命令:sudo vi /etc/systemd/system/kestrel-test1.service

  2、修改上述文件的權限(哎,使用vi編輯不習慣,只能用編輯器),命令:sudo chmod 777 /etc/systemd/system/kestrel-test1.service

  3、輸入以下內容:

[Unit]
Description
=Example .NET Web API Application running on Ubuntu [Service] WorkingDirectory=/home/ddz1/Desktop/ddzdotnetcore/test1/bin/release/netcoreapp2.0/publish ExecStart=/usr/bin/dotnet /home/ddz1/Desktop/ddzdotnetcore/test1/bin/release/netcoreapp2.0/publish/test1.dll Restart=always RestartSec=10 # Restart service after 10 seconds if
dotnet service crashes SyslogIdentifier=ddz-dotnet-test1 User=root Environment=ASPNETCORE_ENVIRONMENT=Production [Install] WantedBy=multi-user.target

技術分享  上面標記部分請自行修改!

  4、上述配置的服務開機啟動,命令:sudo systemctl enable kestrel-test1.service

  5、啟用該服務,命令:sudo systemctl start kestrel-test1.service

  6、查看上述服務狀態,命令:sudo systemctl status kestrel-test1.service

  7、查看運行效果:

技術分享

技術分享

  我們註意到80和5000都可以訪問,也就是說nginx已經啟動了,我們查看一下:

技術分享

  說到這裏,上面說到的問題已經結束了,倒是有點少,再說的別的……  

  二、win10上開發,Linux部署

  在上一篇中,我們弄得應用程序是在Linux用dotnet命令創建的。Linux畢竟用不熟,雖然有VSCode……但是沒有VS,所以需要Win10開發然後部署在Linux上。開始在win10上的操作和在Linux上的操作相同,之後將發布之後的文件拷貝過去,然後運行dotnet run命令會有如下錯誤:

Invalid runtimeconfig.json [/home/ddzubuntu/ddzdotnetcore/test3/test3.runtimeconfig.json] [/home/ddzubuntu/ddzdotnetcore/test3/test3.runtimeconfig.dev.json]

技術分享

  這個錯誤,郁悶了好久,試了好幾種辦法:

  1、RuntimeIdentifier

  1.1、https://docs.microsoft.com/zh-cn/dotnet/core/tools/csproj

  1.2、https://docs.microsoft.com/zh-cn/dotnet/core/rid-catalog

  2、dotnet publish -r ubuntu.16.04-x64

  ……

  ……

  也沒有解決,偶爾看到了這篇文章:Invalid runtimeconfig.json error when trying to run netcore app on linux ,暈!!!沒想到是因為權限的問題,於是我修改了以下應用程序上一層目錄的權限:sudo chmod -R 1744 test3 之後在執行 dotnet test3.dll就沒問題了,如下圖:

技術分享

  ubuntu中再次相遇.net core 2.0就到這裏了。謝謝!

在ubuntu16.04中再次體驗.net core 2.0