示例#1
0
        public async Task StartAsync(HttpServerOptions options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            if (RequestHandler == null)
            {
                throw new InvalidOperationException("RequestHandler is not set.");
            }

            try
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _socketWrapper           = _socketWrapperFactory(options);

                await _socketWrapper.StartAsync();

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                Task.Factory.StartNew(() => AcceptConnectionsAsync(_cancellationTokenSource.Token).ConfigureAwait(false), _cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).ConfigureAwait(false);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            }
            catch (Exception)
            {
                await StopAsync();

                throw;
            }
        }
示例#2
0
        public async Task StopAsync()
        {
            if (_socketWrapper != null)
            {
                await _socketWrapper.StopAsync();
            }

            _socketWrapper?.Dispose();
            _socketWrapper = null;
        }