public void GetNextAvailablePort()
        {
            var port = _localTcpPortManager.GetNextAvailablePort();

            Assert.AreNotEqual(-1, port);
        }
        protected override void OnStart()
        {
            // Update some basic settings
            _appSettings.AddOrUpdate(CommonAppSettingsKeys.ClientIdSetting.Name, Guid.NewGuid().ToString());
            _appSettings.AddOrUpdate(CommonAppSettingsKeys.ClientNameSetting.Name, "CQRS Service");

            // Start dependent components
            _eventPublisher.Start();
            _logicHandler.Start();

            // Get a TCP port that is available
            var portReservation = _localTcpPortManager.GetNextAvailablePort();

            var hostPort = portReservation.Port;
            var sslPort  = portReservation.Port + 1;

            // Update the local setting to begin hosting with the port
            _appSettings.AddOrUpdate(CommonAppSettingsKeys.HttpHostPortSetting.Name, portReservation.Port.ToString());


            try
            {
                // Expose API endpoint
                if (_webHost.IsNotNull())
                {
                    _webHost.Dispose();
                }

                var builder = WebHost.CreateDefaultBuilder();

                //
                _hostUri = new UriBuilder(
                    "https",
                    "localhost",
                    sslPort
                    ).Uri;

                builder.UseKestrel(options =>
                {
                    options.Listen(IPAddress.Loopback, hostPort);
                    options.Listen(IPAddress.Loopback, sslPort, listenOptions =>
                    {
                        listenOptions.UseHttps(StoreName.My, "localhost");
                    });
                }
                                   );

                builder.ConfigureServices(ConfigureServices);
                builder.Configure(Configure);

                _webHost = builder.Build();

                try
                {
                    _webHost.Start();
                }
                catch (Exception ex)
                {
                    _log.Error(ex, ex.Message);

                    throw;
                }
            }
            finally
            {
                portReservation.Dispose();
            }

            _log.InfoFormat(
                "Listening on: http://localhost:{0}/ and https://localhost:{1}/",
                hostPort,
                sslPort
                );
        }
        protected override void OnStart()
        {
            // Update some basic settings
            _appSettings.AddOrUpdate(CommonAppSettingsKeys.ClientIdSetting.Name, Guid.NewGuid().ToString());
            _appSettings.AddOrUpdate(CommonAppSettingsKeys.ClientNameSetting.Name, "RESTFul Service");

            // Start dependent components

            // Get a TCP port that is available
            var portReservation = _localTcpPortManager.GetNextAvailablePort();

            var hostPort = portReservation.Port;
            var sslPort  = portReservation.Port + 1;

            // Update the local setting to begin hosting with the port
            _appSettings.AddOrUpdate(CommonAppSettingsKeys.HttpHostPortSetting.Name, portReservation.Port.ToString());


            try
            {
                // Expose API endpoint
                if (_webHost.IsNotNull())
                {
                    _webHost.Dispose();
                }

                var builder = WebHost.CreateDefaultBuilder();

                //
                _hostUri = new UriBuilder(
                    "https",
                    "localhost",
                    sslPort
                    ).Uri;

                builder.UseKestrel(options =>
                {
                    options.Limits.MaxConcurrentConnections         = 100;
                    options.Limits.MaxConcurrentUpgradedConnections = 100;
                    options.Limits.MaxRequestBodySize     = 10 * 1024;
                    options.Limits.MinRequestBodyDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
                    options.Limits.MinResponseDataRate    = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));

                    options.Listen(IPAddress.Loopback, hostPort);
                    options.Listen(IPAddress.Loopback, sslPort, listenOptions =>
                    {
                        listenOptions.UseHttps(StoreName.My, "localhost");
                    });
                }
                                   );

                builder.ConfigureServices(ConfigureServices);
                builder.Configure(Configure);

                _webHost = builder.Build();

                try
                {
                    _webHost.Start();
                }
                catch (Exception ex)
                {
                    _log.Error(ex, ex.Message);

                    throw;
                }
            }
            finally
            {
                portReservation.Dispose();
            }

            _log.InfoFormat(
                "Listening on: http://localhost:{0}/ and https://localhost:{1}/",
                hostPort,
                sslPort
                );
        }