在Ubuntu上托管Net Core应用程序

时间:2018-03-18 00:37:49

标签: ubuntu asp.net-core .net-core asp.net-core-2.0

嘿伙计我在Ubuntu上托管我的.net核心有些麻烦,我可以使用以下命令托管它:

dotnet /home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0/FrutaMargemCore.dll

这可以很好地托管应用程序,我可以通过VM的外部IP访问它,但它似乎将其锁定在某种会话状态,终端只是托管它,我需要终止进程运行任何其他命令。

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using'/home/bourdain/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Production
Content root path: /home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

关闭SSH连接会终止该过程,并且不再托管该应用程序。

我尝试过使用

dotnet run /home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0/FrutaMargemCore.dll

但随机抛出异常

Using launch settings from /home/bourdain/website/FrutaMargemCore/Properties/launchSettings.json...

Unhandled Exception: System.FormatException: Value for switch '/home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0/FrutaMargemCore.dll' is missing.
   at Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationProvider.Load()
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at FrutaMargemCore.Program.BuildWebHost(String[] args) in /home/bourdain/website/FrutaMargemCore/Program.cs:line 21
   at FrutaMargemCore.Program.Main(String[] args) in /home/bourdain/website/FrutaMargemCore/Program.cs:line 17

每当我关闭SSH连接时,如何在我的ubuntu服务器上托管我的netcore核心而不会终止它?

1 个答案:

答案 0 :(得分:2)

您应该首先创建一个这样的服务文件:

sudo nano /etc/systemd/system/kestrel-yourApp.service

服务文件将包含以下内容:

[Unit]
Description=My ASPNET Core APP

[Service]
WorkingDirectory=/var/someFolder/yourApp
ExecStart=/usr/bin/dotnet /var/aspnetcore/yourApp/yourApp.dll
Restart=always
RestartSec=20
SyslogIdentifier=dotnet-example
User=yourUser
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

调整应用程序上下文的参数(如用户,路径等)。用户必须拥有正确的权限。

这将启动服务:

systemctl enable kestrel-yourApp.service

这样做只是为了检查它是否正在运行:

systemctl status kestrel-yourApp.service