我想从我创建的API(也位于.Net Core中)启动.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
而且我不知道为什么它在Program.cs中编写为使用端口4221
时尝试使用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:错误Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.ThrowError(Int32 statusCode)的-4091 EADDRINUSE地址已在使用中,Microsoft.AspNetCore.Server.Kestrel.Transport的地址。 Libuv.Internal.Networking.LibuvFunctions.tcp_getsockname(UvTcpHandle句柄,SockAddr&addr,Int32&namelen)\ r \ n在Microsoft.AspNetCore.Server.Kestrel.Tran Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.ListenTcp(Boolean useFileHandle)的sport.Libuv.Internal.Networking.UvTcpHandle.GetSockIPEndPoint()\ r \ n(Microsoft.AspNetCore.Server的布尔型useFileHandle)\ r \ n。 Microsoft的Kestrel.Transport.Libuv.Internal.Listener.CreateListenSocket()\ r \ n.Microsoft的Kestrel.Transport.Libuv.Internal.Listener.CreateListenSocket()\ r \ n.Microsoft的<> c.b__8_0(Listener listener)\ r \ n。 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_0
1.d.MoveNext()\ 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__5.MoveNext()\ r \ n ---内部异常堆栈跟踪的结尾--- \ r \ n在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__5.MoveNext( )\ r \ n ---从上一个引发异常的位置开始的堆栈结束跟踪--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(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 ---从上一个引发异常的位置开始的堆栈结束跟踪--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime .CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.AspNetCore.Server.Kestrel.Core.Intern al.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(Task task)\ 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/"
}
}
}
答案 0 :(得分:1)
类似的东西行吗?这是我的API之前设置的方式...
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
options=>
{
options.Listen(IPAddress.Any, 50003);
options.Limits.MaxRequestBodySize = null;
})
)
.UseDefaultServiceProvider(options =>
options.ValidateScopes = false)
.Build();
答案 1 :(得分:0)
我也遇到了像您这样的错误,这是因为我打开了两个终端,并且都“ dotnet watch run”。要解决此问题,只需按Ctrl + C即可停止终端,然后在1个终端中再次“运行dotnet watch”。
答案 2 :(得分:0)