Exemplo n.º 1
0
        public Task Invoke(IDictionary <string, object> env)
        {
            RequestQueue queue;

            if (IsQueueInitialized)
            {
                queue = _queue;
            }
            else
            {
                queue = LazyInitializer.EnsureInitialized(
                    ref _queue,
                    ref _queueInitialized,
                    ref _queueLock,
                    () =>
                {
                    // Start called once on first request
                    var newQueue = new RequestQueue(_options);
                    newQueue.Start();
                    object value;
                    CancellationToken onAppDisposing = env.TryGetValue("server.OnAppDisposing", out value) ? (CancellationToken)value : CancellationToken.None;
                    if (onAppDisposing != CancellationToken.None)
                    {
                        // Stop called once on app disposing
                        // will drain queue by rejecting
                        onAppDisposing.Register(newQueue.Stop);
                    }
                    return(newQueue);
                });
            }

            var             requestInstance = new RequestInstance(env, _next);
            RequestInstance executeInstance = queue.GetInstanceToExecute(requestInstance);

            if (executeInstance != null)
            {
                executeInstance.Execute();
            }
            return(requestInstance.Task);
        }
Exemplo n.º 2
0
        public Task Invoke(IDictionary<string, object> env)
        {
            RequestQueue queue;
            if (IsQueueInitialized)
            {
                queue = _queue;
            }
            else
            {
                queue = LazyInitializer.EnsureInitialized(
                    ref _queue,
                    ref _queueInitialized,
                    ref _queueLock,
                    () =>
                    {
                        // Start called once on first request
                        var newQueue = new RequestQueue(_options);
                        newQueue.Start();
                        object value;
                        CancellationToken onAppDisposing = env.TryGetValue("server.OnAppDisposing", out value) ? (CancellationToken)value : CancellationToken.None;
                        if (onAppDisposing != CancellationToken.None)
                        {
                            // Stop called once on app disposing
                            // will drain queue by rejecting
                            onAppDisposing.Register(newQueue.Stop);
                        }
                        return newQueue;
                    });
            }

            var requestInstance = new RequestInstance(env, _next);
            RequestInstance executeInstance = queue.GetInstanceToExecute(requestInstance);
            if (executeInstance != null)
            {
                executeInstance.Execute();
            }
            return requestInstance.Task;
        }