title: Unable to start Kestrel. Failed to bind to address address already in use
body: <p>I want to start a .net core application from an API that I created which is also in .Net Core too.</p>
  
  <p>I added <code>UseUrls()</code> function to Program.cs file so it will use a port that i want it to use. So heres what it looks my Program.cs of that other module.</p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          BuildWebHost(args).Run();
      }
  
      public static IWebHost BuildWebHost(string[] args) =&gt;
          WebHost.CreateDefaultBuilder(args)
              .UseStartup&lt;Startup&gt;()
              .UseUrls("http://*:50003")
              .UseKestrel(options =&gt;
              {
                  options.Limits.MaxRequestBodySize = null;
              }
              )
              .UseDefaultServiceProvider(options =&gt;
                  options.ValidateScopes = false)
              .Build();
  }
  </code></pre>
  
  <p>So when I open CMD where this module where is located and type <code>dotnet run</code> it will start running application on <code>http://localhost:50003</code>, which is fine because thats the port where I want to start.</p>
  
  <p>But what I need to do, is to start this app from that API. And heres the code that I wrote the command <code>dotnet run</code>:</p>
  
  <pre><code>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);
          }
      }
  </code></pre>
  
  <p>But when I'm running it, that <code>output</code> variable returns an error which it says </p>
  
  <pre><code>Unable to start Kestrel.
  System.IO.IOException: Failed to bind to address http://127.0.0.1:4221: 
  address already in use
  </code></pre>
  
  <p>And i dont know why it tries to start the app with <code>4221</code> port while it is written in Program.cs to use port <code>50003</code>.</p>
  
  <p>So the same command <code>dotnet run</code> is working fine if i write manually in CMD and its not working the way it should if i type on my code.</p>
  
  <blockquote>
    <p>"Using launch settings from 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      Unable to start Kestrel.\r\nSystem.IO.IOException: Failed to bind to address <a href="http://127.0.0.1:4221" rel="noreferrer">http://127.0.0.1:4221</a>: address already in use. ---> Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal.AddressInUseException: Error -4091 EADDRINUSE address already in use ---> Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.UvException: Error -4091 EADDRINUSE address already in use\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.ThrowError(Int32 statusCode)\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.tcp_getsockname(UvTcpHandle handle, SockAddr&amp; addr, Int32&amp; namelen)\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.UvTcpHandle.GetSockIPEndPoint()\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.ListenTcp(Boolean useFileHandle)\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.CreateListenSocket()\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.&lt;>c.b__8_0(Listener listener)\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.LibuvThread.CallbackAdapter<code>1.&lt;&gt;c.&lt;.cctor&gt;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.&lt;StartAsync&gt;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.&lt;BindAsync&gt;d__20.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.LibuvTransport.&lt;BindAsync&gt;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.&lt;&gt;c__DisplayClass23_0</code>1.&lt;g__OnBind|0>d.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.Internal.AddressBinder.d__5.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__5.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.Internal.AddressBinder.d__6.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.Internal.AddressBinder.d__7.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.Internal.AddressBinder.AddressesStrategy.d__2.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.Internal.AddressBinder.d__0.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.d__23`1.MoveNext()\r\n"</p>
  </blockquote>
  
  <p>launchSettings.json contanins:</p>
  
  <pre><code>{
    "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/"
      }
    }
  }
  </code></pre>