protected virtual void EndExecute(IAsyncResult asyncResult)
 {
     AsyncResultWrapper.End(asyncResult, _executeTag);
 }
Beispiel #2
0
        /// <summary>
        /// Begins an asynchronous version of <see cref="GetErrors"/>.
        /// </summary>

        public override IAsyncResult BeginGetErrors(int pageIndex, int pageSize, ICollection<ErrorLogEntry> errorEntryList, AsyncCallback asyncCallback, object asyncState)
        {
            if (pageIndex < 0) throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null);
            if (pageSize < 0) throw new ArgumentOutOfRangeException("pageSize", pageSize, null);

            //
            // Modify the connection string on the fly to support async 
            // processing otherwise the asynchronous methods on the
            // SqlCommand will throw an exception. This ensures the
            // right behavior regardless of whether configured
            // connection string sets the Async option to true or not.
            //

            var csb = new SqlConnectionStringBuilder(ConnectionString)
            {
                AsynchronousProcessing = true
            };
            var connection = new SqlConnection(csb.ConnectionString);

            //
            // Create the command object with input parameters initialized
            // and setup to call the stored procedure.
            //

            var command = Commands.GetErrorsXml(ApplicationName, pageIndex, pageSize);
            command.Connection = connection;

            //
            // Create a closure to handle the ending of the async operation
            // and retrieve results.
            //

            AsyncResultWrapper asyncResult = null;

            Func<IAsyncResult, int> endHandler = delegate
            {
                Debug.Assert(asyncResult != null);

                using (connection)
                using (command)
                {
                    var xml = ReadSingleXmlStringResult(command.EndExecuteReader(asyncResult.InnerResult));
                    ErrorsXmlToList(xml, errorEntryList);
                    int total;
                    Commands.GetErrorsXmlOutputs(command, out total);
                    return total;
                }
            };

            //
            // Open the connenction and execute the command asynchronously,
            // returning an IAsyncResult that wrap the downstream one. This
            // is needed to be able to send our own AsyncState object to
            // the downstream IAsyncResult object. In order to preserve the
            // one sent by caller, we need to maintain and return it from
            // our wrapper.
            //

            try
            {
                connection.Open();

                asyncResult = new AsyncResultWrapper(
                    command.BeginExecuteReader(
                        asyncCallback != null ? /* thunk */ delegate { asyncCallback(asyncResult); } : (AsyncCallback) null, 
                        endHandler), asyncState);

                return asyncResult;
            }
            catch (Exception)
            {
                connection.Dispose();
                throw;
            }
        }
Beispiel #3
0
 protected internal virtual void EndProcessRequest(IAsyncResult asyncResult)
 {
     SecurityUtil.ProcessInApplicationTrust(() => {
         AsyncResultWrapper.End(asyncResult, _processRequestTag);
     });
 }
Beispiel #4
0
        /// <summary>
        /// Begins an asynchronous version of <see cref="GetErrors"/>.
        /// </summary>

        public override IAsyncResult BeginGetErrors(int pageIndex, int pageSize, ICollection <ErrorLogEntry> errorEntryList, AsyncCallback asyncCallback, object asyncState)
        {
            if (pageIndex < 0)
            {
                throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null);
            }
            if (pageSize < 0)
            {
                throw new ArgumentOutOfRangeException("pageSize", pageSize, null);
            }

            //
            // Modify the connection string on the fly to support async
            // processing otherwise the asynchronous methods on the
            // SqlCommand will throw an exception. This ensures the
            // right behavior regardless of whether configured
            // connection string sets the Async option to true or not.
            //

            var csb = new SqlConnectionStringBuilder(ConnectionString)
            {
                AsynchronousProcessing = true
            };
            var connection = new SqlConnection(csb.ConnectionString);

            //
            // Create the command object with input parameters initialized
            // and setup to call the stored procedure.
            //

            var command = Commands.GetErrorsXml(ApplicationName, pageIndex, pageSize);

            command.Connection = connection;

            //
            // Create a closure to handle the ending of the async operation
            // and retrieve results.
            //

            AsyncResultWrapper asyncResult = null;

            Func <IAsyncResult, int> endHandler = delegate
            {
                Debug.Assert(asyncResult != null);

                using (connection)
                    using (command)
                    {
                        var xml = ReadSingleXmlStringResult(command.EndExecuteReader(asyncResult.InnerResult));
                        ErrorsXmlToList(xml, errorEntryList);
                        int total;
                        Commands.GetErrorsXmlOutputs(command, out total);
                        return(total);
                    }
            };

            //
            // Open the connenction and execute the command asynchronously,
            // returning an IAsyncResult that wrap the downstream one. This
            // is needed to be able to send our own AsyncState object to
            // the downstream IAsyncResult object. In order to preserve the
            // one sent by caller, we need to maintain and return it from
            // our wrapper.
            //

            try
            {
                connection.Open();

                asyncResult = new AsyncResultWrapper(
                    command.BeginExecuteReader(
                        asyncCallback != null ? /* thunk */ delegate { asyncCallback(asyncResult); } : (AsyncCallback)null,
                        endHandler), asyncState);

                return(asyncResult);
            }
            catch (Exception)
            {
                connection.Dispose();
                throw;
            }
        }
Beispiel #5
0
 protected internal virtual void EndProcessRequest(IAsyncResult asyncResult)
 {
     AsyncResultWrapper.End(asyncResult, _processRequestTag);
 }
Beispiel #6
0
 public void End_ThrowsIfAsyncResultIsNull()
 {
     // Act & assert
     Assert.ThrowsArgumentNull(
         delegate { AsyncResultWrapper.End(null); }, "asyncResult");
 }
        protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
        {
            IController        controller;
            IControllerFactory factory;

            ProcessRequestInit(httpContext, out controller, out factory);

            IAsyncController asyncController = controller as IAsyncController;

            if (asyncController != null)
            {
                // asynchronous controller

                // Ensure delegates continue to use the C# Compiler static delegate caching optimization.
                BeginInvokeDelegate <ProcessRequestState> beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState, ProcessRequestState innerState)
                {
                    try
                    {
                        return(innerState.AsyncController.BeginExecute(innerState.RequestContext, asyncCallback, asyncState));
                    }
                    catch
                    {
                        innerState.ReleaseController();
                        throw;
                    }
                };

                EndInvokeVoidDelegate <ProcessRequestState> endDelegate = delegate(IAsyncResult asyncResult, ProcessRequestState innerState)
                {
                    try
                    {
                        innerState.AsyncController.EndExecute(asyncResult);
                    }
                    finally
                    {
                        innerState.ReleaseController();
                    }
                };
                ProcessRequestState outerState = new ProcessRequestState()
                {
                    AsyncController = asyncController, Factory = factory, RequestContext = RequestContext
                };

                SynchronizationContext callbackSyncContext = SynchronizationContextUtil.GetSynchronizationContext();
                return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, outerState, _processRequestTag, callbackSyncContext: callbackSyncContext));
            }
            else
            {
                // synchronous controller
                Action action = delegate
                {
                    try
                    {
                        controller.Execute(RequestContext);
                    }
                    finally
                    {
                        factory.ReleaseController(controller);
                    }
                };

                return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag));
            }
        }