web-dev-qa-db-fra.com

Impossible de démarrer Kestrel. Échec de la liaison à l'adresse déjà utilisée

Je souhaite démarrer une application .net principale à partir d'une API que j'ai créée et qui se trouve également dans .Net Core.

J'ai ajouté la fonction UseUrls() au fichier Program.cs afin qu'il utilise un port que je souhaite utiliser. Alors voici ce à quoi il ressemble mon Program.cs de cet autre module.

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();
}

Ainsi, lorsque j'ouvre CMD, où se trouve ce module, et saisissez dotnet run, l'exécution de l'application sur http://localhost:50003 commence, ce qui est correct car c'est le port sur lequel je veux démarrer.

Mais ce que je dois faire, c'est de démarrer cette application à partir de cette API. Et voici le code que j'ai écrit la commande 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);
        }
    }

Mais quand je l’exécute, cette variable output renvoie une erreur qu’elle dit 

Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://127.0.0.1:4221: 
address already in use

Et je ne sais pas pourquoi il essaie de démarrer l'application avec le port 4221 alors qu'il est écrit dans Program.cs pour utiliser le port 50003.

Donc, la même commande dotnet run fonctionne correctement si j'écris manuellement dans CMD et que cela ne fonctionne pas comme il le devrait si je tape sur mon code.

"Utilisation des paramètres de lancement à partir de C:\Utilisateurs\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 Impossible de démarrer Kestrel.\r\nSystem.IO.IOException: échec de la liaison à l'adresse http://127.0.0.1:4221 : address déjà utilisé. ---> Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal.AddressInUseException: Erreur -4091 L'adresse EADDRINUSE est déjà utilisée ---> Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal. Networking.UvException: erreur -4091 L'adresse EADDRINUSE est déjà utilisée sur Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.ThrowError (Int32 statusCode)\r\n sur Microsoft.AspNetCore.Server .Kestrel.Transport.Libuv.Internal.Networking.LibuvFunctions.tcp_getsockname (UvTcpHandle handle, SockAddr & addr, Int32 & namelen) à Microsoft.AspNetCore. Server.Kestrel.Transport.Libuv.Internal.Networking.UvTcpHandle.GetSockIPEndPoint ()\r\n à Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.ListenTcp (Boolean useFileHle)\r\n. AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener.CreateListenSocket ()\r\n à Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.Listener. <> C.b__8_0 (écouteur de liste). n sur Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal.LibuvThread.CallbackAdapter1.<>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 --- Fin de la trace de pile à partir de l'emplacement précédent où une exception a été levée - -\r\n à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (tâche)\r\n à Microsoft.AspNetCore.Server.Server. Internal.AddressBinder.d__5.MoveNext ()\r\n --- Fin de la trace de la pile des exceptions internes ---\r\n à Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Addres sBinder.d__5.MoveNext ()\r\n --- Trace de fin de pile de l'emplacement précédent où une exception a été levée ---\r\n sous System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n sous System .Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Tâche)\r\n à Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__6.MoveNext ()\r\n --- Fin de la trace de la pile à partir de l'emplacement précédent. où une exception a été levée:\r\n à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (tâche à effectuer)\r\n à Microsoft.AspNetCore. Server.Kestrel.Core.Internal.AddressBinder.d__7.MoveNext ()\r\n --- Trace de fin de pile de l'emplacement précédent où une exception a été générée ---\r\n à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n sur System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (tâche)\r\n sur Microsoft.AspNetCore. Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.d__2.MoveNext ()\r\n --- trace de fin de pile de l'emplacement précédent où une exception a été générée ---\r\n à System.Runtime.ExceptionServices.ExceptionDispatchInfo .Throw ()\r\n à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Tâche)\r\n à Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.d__0.MoveNext (\ r\n). --- Trace de fin de pile de l'emplacement précédent où une exception a été levée ---\r\n à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()\r\n à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (tâche )\r\n à Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.d__23`1.MoveNext ()\r\n "

launchSettings.json contient:

{
  "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/"
    }
  }
}
4
Ertan Hasani

Est-ce que quelque chose comme ça marche? C'est ainsi que mon API a été configurée il y a un moment ...

  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
GPW