示例#1
0
        private static ServiceContext CreateServiceContext(IOptions <KestrelServerOptions> options, ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            var serverOptions     = options.Value ?? new KestrelServerOptions();
            var logger            = loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel");
            var trace             = new KestrelTrace(logger);
            var connectionManager = new FrameConnectionManager(
                trace,
                serverOptions.Limits.MaxConcurrentConnections,
                serverOptions.Limits.MaxConcurrentUpgradedConnections);

            var systemClock            = new SystemClock();
            var dateHeaderValueManager = new DateHeaderValueManager(systemClock);

            // TODO: This logic will eventually move into the IConnectionHandler<T> and off
            // the service context once we get to https://github.com/aspnet/KestrelHttpServer/issues/1662
            IThreadPool threadPool = null;

            switch (serverOptions.ApplicationSchedulingMode)
            {
            case SchedulingMode.Default:
            case SchedulingMode.ThreadPool:
                threadPool = new LoggingThreadPool(trace);
                break;

            case SchedulingMode.Inline:
                threadPool = new InlineLoggingThreadPool(trace);
                break;

            default:
                throw new NotSupportedException(CoreStrings.FormatUnknownTransportMode(serverOptions.ApplicationSchedulingMode));
            }

            return(new ServiceContext
            {
                Log = trace,
                HttpParserFactory = frameParser => new HttpParser <FrameAdapter>(frameParser.Frame.ServiceContext.Log.IsEnabled(LogLevel.Information)),
                ThreadPool = threadPool,
                SystemClock = systemClock,
                DateHeaderValueManager = dateHeaderValueManager,
                ConnectionManager = connectionManager,
                ServerOptions = serverOptions
            });
        }
示例#2
0
 public TestServiceContext(ILoggerFactory loggerFactory, IKestrelTrace kestrelTrace)
 {
     LoggerFactory          = loggerFactory;
     Log                    = kestrelTrace;
     ThreadPool             = new LoggingThreadPool(Log);
     SystemClock            = new MockSystemClock();
     DateHeaderValueManager = new DateHeaderValueManager(SystemClock);
     ConnectionManager      = new FrameConnectionManager(Log, ResourceCounter.Unlimited, ResourceCounter.Unlimited);
     HttpParserFactory      = frameAdapter => new HttpParser <FrameAdapter>(frameAdapter.Frame.ServiceContext.Log.IsEnabled(LogLevel.Information));
     ServerOptions          = new KestrelServerOptions
     {
         AddServerHeader = false
     };
 }
示例#3
0
        public void UnrootedConnectionsGetRemovedFromHeartbeat()
        {
            var connectionId           = "0";
            var trace                  = new Mock <IKestrelTrace>();
            var frameConnectionManager = new FrameConnectionManager(trace.Object, ResourceCounter.Unlimited, ResourceCounter.Unlimited);

            // Create FrameConnection in inner scope so it doesn't get rooted by the current frame.
            UnrootedConnectionsGetRemovedFromHeartbeatInnerScope(connectionId, frameConnectionManager, trace);

            GC.Collect();
            GC.WaitForPendingFinalizers();

            var connectionCount = 0;

            frameConnectionManager.Walk(_ => connectionCount++);

            Assert.Equal(0, connectionCount);
            trace.Verify(t => t.ApplicationNeverCompleted(connectionId), Times.Once());
        }
示例#4
0
        private void UnrootedConnectionsGetRemovedFromHeartbeatInnerScope(
            string connectionId,
            FrameConnectionManager frameConnectionManager,
            Mock <IKestrelTrace> trace)
        {
            var frameConnection = new FrameConnection(new FrameConnectionContext
            {
                ServiceContext = new TestServiceContext(),
                ConnectionId   = connectionId
            });

            frameConnectionManager.AddConnection(0, frameConnection);

            var connectionCount = 0;

            frameConnectionManager.Walk(_ => connectionCount++);

            Assert.Equal(1, connectionCount);
            trace.Verify(t => t.ApplicationNeverCompleted(connectionId), Times.Never());

            // Ensure frameConnection doesn't get GC'd before this point.
            GC.KeepAlive(frameConnection);
        }