无法启动红est。绑定到已经使用的地址失败

Ert*_*ani 5 .net c# asp.net process asp.net-core

我想从我创建的API(也位于.Net Core中)启动.net Core应用程序。

UseUrls()在Program.cs文件中添加了功能,因此它将使用我希望它使用的端口。因此,这里看起来是我在其他模块中的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();
}
Run Code Online (Sandbox Code Playgroud)

因此,当我打开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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,该output变量返回一个错误,它说

Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://127.0.0.1:4221: 
address already in use
Run Code Online (Sandbox Code Playgroud)

而且我不知道为什么它4221在Program.cs中编写为使用port 时尝试使用port 启动应用程序50003

因此,dotnet run如果我用CMD手动编写该命令,则该命令运行良好,并且如果我在代码中键入该命令,该命令将无法正常工作。

“使用来自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中使用\ r \ n在Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.ThrowError(Int32 statusCode)\ r \ n .Libuv.Internal.Networking.LibuvFunctions.tcp_getsockname(UvTcpHandle句柄,SockAddr&addr,Int32&namelen)\ r \ n在Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.UvTcpHandle.GetSockIPEndPoint()\ r \ n Microsoft处Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.ListenTcp(Boolean useFileHandle)\ r \ n。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. <g__OnBind | 0> 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.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__6。MoveNext()\ 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(Task task)\ r \ n在Microsoft.AspNetCore.Server.Kestrel。 Core.Internal.AddressBinder.AddressesStrategy.d__2.MoveNext()\ r \ n ---在System.Runtime.ExceptionServices处从引发异常的先前位置开始的堆栈结束跟踪--- \ r \ n。ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__0.MoveNext()\ r \ n n ---从上一个引发异常的位置开始的堆栈结束跟踪--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)\ r \ n在Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.d__23`1.MoveNext()\ r \ n“MoveNext()\ 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“MoveNext()\ 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/"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

GPW*_*GPW 3

这样的东西有用吗?这就是我的 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();
Run Code Online (Sandbox Code Playgroud)