已发布的 .Net 7 应用在 Ubuntu 上无法运行。

huangapple go评论61阅读模式
英文:

Published .Net 7 app not running on ubuntu

问题

我有一个包含2个独立项目的.NET 7解决方案。
我正在使用Ubuntu 23.04。

/Project1
  - Project1.csproject
  - appsettings.json

/Project2
  - Project2.csproject
  - appsettings.json

MySolution.sln

我运行了以下命令:

 dotnet publish 'MySolution.sln' --self-contained false -r linux-x64 -c Release -o publish

 MSBuild版本为.NET的17.4.3+7e646be43
 确定要还原的项目...
 所有项目都是最新的,无需还原。
 Project1 -> /home/myuser/mysolution/project1/bin/Release/net7.0/linux-x64/Project1.dll
 Project1 -> /home/myuser/mysolution/publish/
 Project2 -> /home/myuser/mysolution/project1/bin/Release/net7.0/linux-x64/Project2.dll
 Project2 -> /home/myuser/mysolution/publish/

它会生成一个包含正确代码的/publish目录。

不幸的是,我无法使这些应用程序运行。

我的Appsettings文件包含Kestrel部分,以使每个项目在不同的端口上打开:

Project1/appsettings.json :

"AllowedHosts": "*",
"Kestrel": {
  "Endpoints": {
    "Http": {
	  "Url": "http://localhost:5001"
    },
    "Https": {
	  "Url": "https://localhost:5001"
    }
  }
}

Project2/appsettings.json :

"AllowedHosts": "*",
"Kestrel": {
  "Endpoints": {
    "Http": {
	  "Url": "http://localhost:5002"
    },
    "Https": {
	  "Url": "https://localhost:5002"
    }
  }
}

即使我直接转到Publish/Project文件夹并运行

 dotnet Project1.dll --verbosity=diagnostic

我也没有任何输出。

我的dotnet已安装:

.NET SDK:
 Version:   7.0.105
 Commit:    e1bc5e001c

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  23.04
 OS Platform: Linux
 RID:         ubuntu.23.04-x64
 Base Path:   /usr/lib/dotnet/sdk/7.0.105/

Host:
  Version:      7.0.5
  Architecture: x64
  Commit:       8042d61b17

已安装.NET SDKs:
  7.0.105 [/usr/lib/dotnet/sdk]

已安装.NET运行时:
  Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]

我也尝试过使用systemd自动启动项目,但它会在少于一秒钟后启动然后停止。

cd /etc/systemd/system/
sudo nano project1.service

[Unit]
Description=Project1

[Service]
Type=simple
WorkingDirectory=/home/myuser/
Restart=on-failure
RestartSec=10
ExecStart=/usr/bin/dotnet /home/myuser/mysolution/project1/bin/Release/net7.0/linux-x64/Project1.dll
KillSignal=SIGINT
SyslogIdentifier=dotnet-project1
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

感谢任何帮助!我该如何解决这个问题?
我希望有人能发现我犯的错误!

英文:

I have a .Net 7 solution that contains 2 separate projects.
I am using ubuntu 23.04.

/Project1
  - Project1.csproject
  - appsettings.json

/Project2
  - Project2.csproject
  - appsettings.json

MySolution.sln

I have ran the command:

 dotnet publish 'MySolution.sln' --self-contained false -r linux-x64 -c Release -o publish
 
 MSBuild version 17.4.3+7e646be43 for .NET
 Determining projects to restore...
 All projects are up-to-date for restore.
 Project1 -> /home/myuser/mysolution/project1/bin/Release/net7.0/linux-x64/Project1.dll
 Project1 -> /home/myuser/mysolution/publish/
 Project2 -> /home/myuser/mysolution/project1/bin/Release/net7.0/linux-x64/Project2.dll
 Project2 -> /home/myuser/mysolution/publish/

It produces a /publish directory containing the correct code.

Unfortunately, I can not get these apps to run.

My Appsettings files contain Kestrel sections to have each of the projects open on different ports:

Project1/appsettings.json :

"AllowedHosts": "*",
"Kestrel": {
  "Endpoints": {
    "Http": {
	  "Url": "http://localhost:5001"
    },
    "Https": {
	  "Url": "https://localhost:5001"
    }
  }
}

Project2/appsettings.json :

"AllowedHosts": "*",
"Kestrel": {
  "Endpoints": {
    "Http": {
	  "Url": "http://localhost:5002"
    },
    "Https": {
	  "Url": "https://localhost:5002"
    }
  }
}

Even when I navigate directly to the Publish/Project folders and run

 dotnet Project1.dll --verbosity=diagnostic

I get no output.

My dotnet is installed:

.NET SDK:
 Version:   7.0.105
 Commit:    e1bc5e001c

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  23.04
 OS Platform: Linux
 RID:         ubuntu.23.04-x64
 Base Path:   /usr/lib/dotnet/sdk/7.0.105/

Host:
  Version:      7.0.5
  Architecture: x64
  Commit:       8042d61b17

.NET SDKs installed:
  7.0.105 [/usr/lib/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.5 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]

I have also tried using systemd to boot the project automatically but it starts then deactivates after less than a second.

cd /etc/systemd/system/
sudo nano project1.service

[Unit]
Description=Project1

[Service]
Type=simple
WorkingDirectory=/home/myuser/
Restart=on-failure
RestartSec=10
ExecStart=/usr/bin/dotnet /home/myuser/mysolution/project1/bin/Release/net7.0/linux-x64/Project1.dll
KillSignal=SIGINT
SyslogIdentifier=dotnet-project1
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Any help is appreciated! How do I go about troubleshooting this?
I'm hoping someone can spot a mistake I have made!

答案1

得分: 0

我的设置问题与我使用的路径有关。我将已发布的文件从已发布的目录移动到 /var/myapp。

sudo cp -a /home/myuser/mysolution/ /var/myapp/

然后,我更新了 systemd 文件中的这些详细信息。

[Unit]
Description=Project1

[Service]
Type=simple
WorkingDirectory=/var/myapp
Restart=on-failure
RestartSec=10
ExecStart=/usr/bin/dotnet /var/myapp/project1/Project1.dll
KillSignal=SIGINT
SyslogIdentifier=dotnet-project1
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
英文:

The problem with my set up was to do with the paths I was using. I moved the published files from the published directory to /var/myapp

sudo cp -a /home/myuser/mysolution/ /var/myapp/

I then updated the systemd file with these details

[Unit]
Description=Project1

[Service]
Type=simple
WorkingDirectory=/var/myapp
Restart=on-failure
RestartSec=10
ExecStart=/usr/bin/dotnet /var/myapp/project1/Project1.dll
KillSignal=SIGINT
SyslogIdentifier=dotnet-project1
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

huangapple
  • 本文由 发表于 2023年6月11日 19:58:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定