【发布时间】:2018-06-29 13:29:57
【问题描述】:
我想从我创建的 API 启动一个 .net 核心应用程序,该 API 也在 .Net Core 中。
我在 Program.cs 文件中添加了UseUrls() 函数,因此它将使用我希望它使用的端口。所以这就是我的另一个模块的 Program.cs 的样子。
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://*:50003")
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
}
)
.UseDefaultServiceProvider(options =>
options.ValidateScopes = false)
.Build();
}
因此,当我打开此模块所在的 CMD 并键入 dotnet run 时,它将开始在 http://localhost:50003 上运行应用程序,这很好,因为那是我要启动的端口。
但我需要做的是从那个 API 启动这个应用程序。这是我编写命令dotnet run的代码:
public IActionResult RunPackage(int id)
{
try
{
var workingDirectory = 'here goes the path of the directory of this module that i want to start running';
var processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "dotnet";
processStartInfo.Arguments = "run";
processStartInfo.WorkingDirectory = workingDirectory;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = false;
processStartInfo.CreateNoWindow = true;
var process = new Process();
process.StartInfo = processStartInfo;
process.Start();
var reader = process.StandardOutput;
var output = reader.ReadToEnd();
return Ok();
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
但是当我运行它时,output 变量返回一个错误,它说
Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://127.0.0.1:4221:
address already in use
我不知道为什么它会尝试使用4221 端口启动应用程序,而它是用 Program.cs 编写的以使用端口50003。
因此,如果我在 CMD 中手动编写,相同的命令 dotnet run 可以正常工作,而如果我在我的代码上键入它,则它不能正常工作。
“使用 C:\Users\StarTech\Desktop\Actibook Actibook\actibook-backend\ServerCore\TimeFrame.Actibook.WebService\wwwroot\Packages\2018-6-Friday031203SampleConverter\Properties\launchSettings.json 的启动设置... \r\ncrit: Microsoft.AspNetCore.Server.Kestrel[0]\r\n 无法启动 Kestrel。\r\nSystem.IO.IOException: 无法绑定到地址 http://127.0.0.1:4221: 地址已在使用中。--- > Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal.AddressInUseException:错误-4091 EADDRINUSE 地址已在使用中---> Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.UvException:错误-4091 EADDRINUSE 地址已在 Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.ThrowError(Int32 statusCode)\r\n 在 Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv 处使用。 Internal.Networking.LibuvFunctions.tcp_getsockname(UvTcpHandle 句柄、SockAddr& addr、Int32& namelen)\r\n 在 Microsoft.AspNetCore.Server.Kestre l.Transport.Libuv.Internal.Networking.UvTcpHandle.GetSockIPEndPoint()\r\n 在 Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.ListenTcp(Boolean useFileHandle)\r\n 在 Microsoft.AspNetCore。 Server.Kestrel.Transport.Libuv.Internal.Listener.CreateListenSocket()\r\n at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.c.b__8_0(Listener listener)\r\n at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.LibuvThread.CallbackAdapter
1.<>c.<.cctor>b__3_1(Object callback, Object state)\r\n at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.LibuvThread.DoPostWork()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.ListenerPrimary.<StartAsync>d__15.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.LibuvTransport.<BindAsync>d__20.MoveNext()\r\n --- End of inner exception stack trace ---\r\n at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.LibuvTransport.<BindAsync>d__20.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.<>c__DisplayClass23_01.d.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Microsoft.AspNetCore.Server.Kestrel。 Core.Internal.AddressBinder.d__5.MoveNext()\r\n --- 内部异常堆栈跟踪结束 ---\r\n 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__5.M oveNext()\r\n--- 从先前引发异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices .TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__6.MoveNext()\r\n--- 堆栈跟踪从先前引发异常的位置结束---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Microsoft.AspNetCore.Server.Kestrel。 Core.Internal.AddressBinder.d__7.MoveNext()\r\n--- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 处从先前引发异常的位置结束堆栈跟踪 ---\r\n\r \n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Microsoft.AspNetCore.Server.Kestrel .Core.Internal.AddressBinder.AddressesStrategy.d__2.MoveNext()\r\n--- 从先前引发异常的位置结束堆栈跟踪---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw( )\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r\n 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__0.MoveNext()\r\n---从先前引发异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\r \n 在 Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.d__23`1.MoveNext()\r\n"
launchSettings.json 包含:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60924/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"SampleConverter": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:60925/"
}
}
}
【问题讨论】:
-
您可能会看到与此文件有关的输出 - “C:\Users\StarTech\Desktop\...\Properties\launchSettings.json”。它的内容是什么?
-
dotnet run对部署无效(因为它仅用于开发)。您应该通过dotnet publish发布二进制文件并使用dotnet直接启动入口程序集。 -
@KirylZ 刚刚在此处添加了该文件的代码。但是只有一些关于启动的基本配置。但问题是,即使这里显示 url:
localhost:60925和 60924,我在 Program.cs 中添加的代码也应该覆盖它。附:当我从代码开始时,它甚至没有绑定这里写的这些端口。 -
@LexLi 我试着写
dotnet publish,然后去发布目录并使用dotnet ApplicationName.dll,但还是一样。 -
@ErtanHasani 您是否尝试过缩小监听 4221 的进程?喜欢通过 netstat?
标签: c# asp.net .net asp.net-core process