public async Task StartAsync()

in src/WebJobs.Extensions.DurableTask/LocalHttpListener.cs [61:111]


        public async Task StartAsync()
        {
            if (this.IsListening == true)
            {
                throw new InvalidOperationException("The local HTTP listener has already started.");
            }

            const int maxAttempts = 10;
            int numAttempts = 1;
            do
            {
                int listeningPort = numAttempts == 1
                    ? DefaultPort
                    : this.GetRandomPort();
                try
                {
                    this.InternalRpcUri = new Uri($"http://127.0.0.1:{listeningPort}/durabletask/");
                    var listenUri = new Uri(this.InternalRpcUri.GetLeftPart(UriPartial.Authority));
                    this.localWebHost = new WebHostBuilder()
                        .UseKestrel()
                        .ConfigureKestrel(o =>
                        {
                            // remove request's Content size limits
                            o.Limits.MaxRequestBodySize = null;
                        })
                        .UseUrls(listenUri.OriginalString)
                        .Configure(a => a.Run(this.HandleRequestAsync))
                        .Build();

                    await this.localWebHost.StartAsync();
                    this.IsListening = true;
                    break;
                }
                catch (IOException)
                {
                    this.traceHelper.ExtensionWarningEvent(
                        this.durableTaskOptions.HubName,
                        functionName: string.Empty,
                        instanceId: string.Empty,
                        message: $"Failed to open local port {listeningPort}. This was attempt #{numAttempts} to open a local port.");
                    this.attemptedPorts.Add(listeningPort);
                    numAttempts++;
                }
            }
            while (numAttempts <= maxAttempts);

            if (!this.IsListening)
            {
                throw new IOException($"Unable to find a port to open an RPC endpoint on after {maxAttempts} attempts");
            }
        }