我正在尝试在 ubuntu 上部署我的 ASP.NET Core 项目。我已在虚拟机上复制了发布文件,安装了运行时并创建了一个服务文件。但是,当我启动程序时,我收到错误
Unhandled exception. System.IO.FileNotFoundException: The configuration file 'appsettings.Production.json' was not found and is not optional. The expected physical path was '/appsettings.Production.json'.
运行
dotnet publish --configuration Release
会输出一个 Release 文件夹,其中包含一个 net8.0
文件夹,其中包含项目的所有文件。
net8.0的内容
ubuntu@server-1:~$ ls Release/net8.0
API.deps.json Microsoft.IdentityModel.JsonWebTokens.dll
API.dll Microsoft.IdentityModel.Logging.dll
API.exe Microsoft.IdentityModel.Protocols.OpenIdConnect.dll
API.pdb Microsoft.IdentityModel.Protocols.dll
API.runtimeconfig.json Microsoft.IdentityModel.Tokens.dll
Asp.Versioning.Abstractions.dll Microsoft.OpenApi.dll
Asp.Versioning.Http.dll MySqlConnector.dll
Asp.Versioning.Mvc.ApiExplorer.dll Newtonsoft.Json.dll
Asp.Versioning.Mvc.dll Pomelo.EntityFrameworkCore.MySql.dll
Castle.Core.dll Swashbuckle.AspNetCore.Annotations.dll
ClassLibrary.dll Swashbuckle.AspNetCore.Swagger.dll
ClassLibrary.pdb Swashbuckle.AspNetCore.SwaggerGen.dll
Discord.Net.Core.dll Swashbuckle.AspNetCore.SwaggerUI.dll
Discord.Net.Rest.dll System.IdentityModel.Tokens.Jwt.dll
Microsoft.AspNetCore.Authentication.JwtBearer.dll System.Interactive.Async.dll
Microsoft.Bcl.AsyncInterfaces.dll System.Linq.Async.dll
Microsoft.EntityFrameworkCore.Abstractions.dll appsettings.Development.json
Microsoft.EntityFrameworkCore.Proxies.dll appsettings.Production.json
Microsoft.EntityFrameworkCore.Relational.dll appsettings.json
Microsoft.EntityFrameworkCore.dll publish
Microsoft.IdentityModel.Abstractions.dll
如您所见,appsettings.Production.json 文件在那里,但仍未加载。
这是我的服务文件
[Unit]
Description=ASP.NET Core Website
After=network.target
[Service]
ExecStart=/usr/bin/dotnet /home/ubuntu/Release/net8.0/API.dll
Restart-on=failure
User=ubuntu
Type=simple
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
在 Program.cs 中我有
builder.Configuration.AddJsonFile("appsettings.Production.json", false);
它可以在我的 PC 上本地运行,但不能在 ubuntu 虚拟机上运行。我做错了什么?
我没有更改任何其他配置,ContentRootPath 是默认配置 (/)。
除非 ContentRootPath
/
指向 ubuntu 的 /
目录,否则我很困惑为什么会发生这种情况。
我通过将
WorkingDirectory
设置为服务文件的 Service 部分中的 net8.0
目录解决了这个问题。