Esempio n. 1
0
 public IDisposable Start(IServerInformation serverInformation, Func<IFeatureCollection, Task> application)
 {
     var disposables = new List<IDisposable>();
     var information = (ServerInformation)serverInformation;
     var engine = new KestrelEngine(_libraryManager, _appShutdownService);
     engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
     foreach (var address in information.Addresses)
     {
         disposables.Add(engine.CreateServer(
             address.Scheme,
             address.Host,
             address.Port,
             async frame =>
             {
                 var request = new ServerRequest(frame);
                 await application.Invoke(request.Features).ConfigureAwait(false);
             }));
     }
     disposables.Add(engine);
     return new Disposable(() =>
     {
         foreach (var disposable in disposables)
         {
             disposable.Dispose();
         }
     });
 }
        public IDisposable Start(IFeatureCollection serverFeatures, Func <IFeatureCollection, Task> application)
        {
            var disposables = new Stack <IDisposable>();
            var disposer    = new Disposable(() =>
            {
                foreach (var disposable in disposables)
                {
                    disposable.Dispose();
                }
            });

            try
            {
                var information            = (KestrelServerInformation)serverFeatures.Get <IKestrelServerInformation>();
                var dateHeaderValueManager = new DateHeaderValueManager();
                var engine = new KestrelEngine(_libraryManager, new ServiceContext
                {
                    AppShutdown            = _appShutdownService,
                    Log                    = new KestrelTrace(_logger),
                    DateHeaderValueManager = dateHeaderValueManager
                });

                disposables.Push(engine);
                disposables.Push(dateHeaderValueManager);

                if (information.ThreadCount < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(information.ThreadCount),
                                                          information.ThreadCount,
                                                          "ThreadCount cannot be negative");
                }

                engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
                bool atLeastOneListener = false;

                foreach (var address in information.Addresses)
                {
                    var parsedAddress = ServerAddress.FromUrl(address);
                    if (parsedAddress == null)
                    {
                        throw new FormatException("Unrecognized listening address: " + address);
                    }
                    else
                    {
                        atLeastOneListener = true;
                        disposables.Push(engine.CreateServer(
                                             parsedAddress.Scheme,
                                             parsedAddress.Host,
                                             parsedAddress.Port,
                                             async frame =>
                        {
                            var request = new ServerRequest(frame);
                            await application.Invoke(request.Features).ConfigureAwait(false);
                        }));
                    }
                }

                if (!atLeastOneListener)
                {
                    throw new InvalidOperationException("No recognized listening addresses were configured.");
                }

                return(disposer);
            }
            catch
            {
                disposer.Dispose();
                throw;
            }
        }
        public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> application)
        {
            var disposables = new Stack<IDisposable>();
            var disposer = new Disposable(() =>
            {
                foreach (var disposable in disposables)
                {
                    disposable.Dispose();
                }
            });

            try
            {
                var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>();
                var dateHeaderValueManager = new DateHeaderValueManager();
                var engine = new KestrelEngine(_libraryManager, new ServiceContext
                {
                    AppShutdown = _appShutdownService,
                    Log = new KestrelTrace(_logger),
                    DateHeaderValueManager = dateHeaderValueManager
                });

                disposables.Push(engine);
                disposables.Push(dateHeaderValueManager);

                if (information.ThreadCount < 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(information.ThreadCount),
                        information.ThreadCount,
                        "ThreadCount cannot be negative");
                }

                engine.Start(information.ThreadCount == 0 ? 1 : information.ThreadCount);
                bool atLeastOneListener = false;

                foreach (var address in information.Addresses)
                {
                    var parsedAddress = ServerAddress.FromUrl(address);
                    if (parsedAddress == null)
                    {
                        throw new FormatException("Unrecognized listening address: " + address);
                    }
                    else
                    {
                        atLeastOneListener = true;
                        disposables.Push(engine.CreateServer(
                            parsedAddress.Scheme,
                            parsedAddress.Host,
                            parsedAddress.Port,
                            async frame =>
                            {
                                var request = new ServerRequest(frame);
                                await application.Invoke(request.Features).ConfigureAwait(false);
                            }));
                    }
                }

                if (!atLeastOneListener)
                {
                    throw new InvalidOperationException("No recognized listening addresses were configured.");
                }

                return disposer;
            }
            catch
            {
                disposer.Dispose();
                throw;
            }
        }