我尝试在 CentOS 上部署 asp.net web.api 应用程序。当我使用 dotnet/var/www/html/CoreApiTest.dll 手动运行它时,它工作正常(使用 apache 作为代理)。当我使用服务来运行它时,出现错误。这是服务代码:

[Unit]
Description=Example .NET Web API Application running on CentOS 7

[Service]
WorkingDirectory=/var/www/html/CoreApiTest
ExecStart=/usr/bin/dotnet /var/www/html/CoreApiTest.dll
Restart=always
# Restart service after 10 seconds if dotnet service crashes
RestartSec=10
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

我使用 systemctl enable kestrel-CoreApiTest.service 安装它。然后启动它并检查状态:systemctl status kestrel-CoreApiTest.service 并收到此错误:
kestrel-CoreApiTest.service - Example .NET Web API Application running on CentOS 7
   Loaded: loaded (/etc/systemd/system/kestrel-CoreApiTest.service; enabled; vendor preset: disabled)
   Active: activating (auto-restart) (Result: exit-code) since Thu 2017-11-02 21:27:35 MSK; 5s ago
  Process: 2093 ExecStart=/usr/bin/dotnet /var/www/html/CoreApiTest.dll (code=exited, status=1/FAILURE)
 Main PID: 2093 (code=exited, status=1/FAILURE)

Nov 02 21:27:35 aryumin.fvds.ru systemd[1]: kestrel-CoreApiTest.service: main process exited, code=exited, status=1/FAILURE
Nov 02 21:27:35 aryumin.fvds.ru systemd[1]: Unit kestrel-CoreApiTest.service entered failed state.
Nov 02 21:27:35 aryumin.fvds.ru systemd[1]: kestrel-CoreApiTest.service failed.

在日志中(journalctl -u kestrel-CoreApiTest)我看到了这个:
ov 02 20:59:47 aryumin.fvds.ru systemd[1]: Started Example .NET Web API Application running on CentOS 7.
Nov 02 20:59:47 aryumin.fvds.ru systemd[1]: Starting Example .NET Web API Application running on CentOS 7...
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: Welcome to .NET Core!
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: ---------------------
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available command
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: Telemetry
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: --------------
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: The .NET Core tools collect usage data in order to improve your experience. The data is anonymous a
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 usi
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: System.UnauthorizedAccessException: Access to the path '/usr/share/httpd/.dotnet/2.0.0.dotnetFirstU
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: --- End of inner exception stack trace ---
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 e
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at System.IO.File.Create(String path)
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.Extensions.EnvironmentAbstractions.FileWrapper.CreateEmptyFile(String path)
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.DotNet.Configurer.FirstTimeUseNoticeSentinel.CreateIfNotExists()
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.PrintFirstTimeUseNotice()
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.Configure()
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentin
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
Nov 02 20:59:54 aryumin.fvds.ru dotnet-example[476]: at Microsoft.DotNet.Cli.Program.Main(String[] args)

我究竟做错了什么?

最佳答案

dotnet CLI 正在尝试将文件写入用户配置文件以记录它已显示首次运行消息的事实。这将失败,因为服务用户没有可写的配置文件目录。

您可以将以下内容添加到 [Service] 部分以防止 dotnet 显示此消息并创建此文件:

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

关于c# - asp.net core web.api 应用程序无法启动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47082358/

10-11 16:59