Exemplo n.º 1
0
        async Task ProcessContext()
        {
            QueueNextRequestPending();
            HttpListenerContext nativeContext;

            try
            {
                nativeContext = await _listener.GetContextAsync();
            }
            catch (HttpListenerException)
            {
                return;
            }
            var ambientContext = new AmbientContext();
            var context        = new HttpListenerCommunicationContext(this, nativeContext);

            try
            {
                using (new ContextScope(ambientContext))
                {
                    var incomingRequestReceivedEventArgs = new IncomingRequestReceivedEventArgs(context);
                    IncomingRequestReceived(this, incomingRequestReceivedEventArgs);
                    await incomingRequestReceivedEventArgs.RunTask;
                }
            }
            finally
            {
                using (new ContextScope(ambientContext))
                {
                    IncomingRequestProcessed(this, new IncomingRequestProcessedEventArgs(context));
                }
            }
        }
        async Task ProcessContext(HttpListenerContext nativeContext)
        {
            var ambientContext = new AmbientContext();
            var context        = new HttpListenerCommunicationContext(this, nativeContext, Resolver.Resolve <ILogger>());

            try
            {
                Interlocked.Increment(ref _pendingRequestCount);
                _zeroPendingRequests.Reset();

                try
                {
                    using (new ContextScope(ambientContext))
                    {
                        Resolver.AddDependencyInstance(typeof(HttpListenerContext), nativeContext);
                        var incomingRequestReceivedEventArgs = new IncomingRequestReceivedEventArgs(context);
                        IncomingRequestReceived(this, incomingRequestReceivedEventArgs);
                        await incomingRequestReceivedEventArgs.RunTask;
                    }
                }
                finally
                {
                    using (new ContextScope(ambientContext))
                    {
                        IncomingRequestProcessed(this, new IncomingRequestProcessedEventArgs(context));
                    }
                }
            }
            finally
            {
                if (Interlocked.Decrement(ref _pendingRequestCount) == 0)
                {
                    _zeroPendingRequests.Set();
                }

                nativeContext.Response.Close();
            }
        }