Xav*_*eña 7 c# linux asp.net .net-core kestrel-http-server
简介:它可以作为dotnet run,但不能作为dotnet myappname.dll。
我的Linux技能是有限的,但是我想按照本书学习,所以我不会混淆(遵循Scott Hanselman的本教程):
$ cd /home/myusername/dotnettest
$ dotnet run
Run Code Online (Sandbox Code Playgroud)
然后像这样将其移动到/ var:
$ sudo cp -a /home/myusername/dotnettest/bin/Debug/netcoreapp1.1/publish /var/dotnettest
Run Code Online (Sandbox Code Playgroud)
最后,我测试它是否也可以正常工作:
$ dotnet dotnettest.dll
Run Code Online (Sandbox Code Playgroud)
然后失败:
info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
User profile is available. Using '/home/myusername/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Unhandled Exception: System.AggregateException: One or more errors occurred. (Error -98 EADDRINUSE address already in use) ---> Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvException: Error -98 EADDRINUSE address already in use
at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.Libuv.Check(Int32 statusCode)
at Microsoft.AspNetCore.Server.Kestrel.Internal.Networking.UvTcpHandle.GetSockIPEndPoint()
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.TcpListener.CreateListenSocket()
at Microsoft.AspNetCore.Server.Kestrel.Internal.Http.Listener.<>c.<StartAsync>b__6_0(Object state)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.Kestrel.Internal.KestrelEngine.CreateServer(ServerAddress address)
at Microsoft.AspNetCore.Server.Kestrel.KestrelServer.Start[TContext](IHttpApplication`1 application)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Start()
at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host, CancellationToken token, String shutdownMessage)
at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)
at WebApplication.Program.Main(String[] args) in /home/myusername/dotnettest/Program.cs:line 27
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
我在尝试停止nginx时非常小心。
我检查了是否有任何命令正在监听:5123:
$ lsof -i tcp:5123
Run Code Online (Sandbox Code Playgroud)
而且似乎什么也没发生。
以下命令有助于找到端口并终止进程
Mac上的终端
查找过程号
lsof -i: <port number>
Run Code Online (Sandbox Code Playgroud)
例如lsof -i:5001
然后杀死进程号
kill -9 <process number>
Run Code Online (Sandbox Code Playgroud)
例如-杀死-9 1600
我知道 OP 在 Mac 上,但这个问题作为第一个 Google 结果出现,所以我也添加了一个 Windows 解决方法:
Get-Process -Id (Get-NetTCPConnection -LocalPort 5001).OwningProcess
Run Code Online (Sandbox Code Playgroud)
在 powershell/windows 终端中运行上述命令(替换5001为您的端口号)。然后通过运行以下命令终止该进程:
kill theProcessNumberHere
Run Code Online (Sandbox Code Playgroud)
事实证明,在更改Program.cs配置以监听 :5123 后,它没有正确重建。发布的版本使用 :5000 而不是 :5123。
与此同时,端口 :5000 被另一个dotnet进程使用(我通过它发现sudo netstat -ltp并随后将其杀死)。这就是为什么错误是“地址已在使用中”。杀死进程后,dotnet dotnettest.dll运行正常,但在端口:5000(还不是:5123)。
然后,我确保项目已正确重建,/publish为了以防万一,我删除了该文件夹,然后dotnet publish. 重要提示:我必须hosting.json手动复制到构建文件夹(之后也复制到发布文件夹)。现在它正在收听:5123。
脚步:
sudo netstat -ltp.hosting.json,您必须将其包含在项目中,以便在构建和发布时复制它)。