/// <summary>
        /// Gets the results of a submit.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies a submit.</param>
        /// <returns>The results returned by the submit.</returns>
        protected sealed override SubmitCompletedResult EndSubmitCore(IAsyncResult asyncResult)
        {
            WebDomainClientAsyncResult <TContract> wcfAsyncResult = this.EndAsyncResult(asyncResult, AsyncOperationType.Submit, /* cancel */ false);
            MethodInfo      endSubmitMethod = wcfAsyncResult.EndOperationMethod;
            EntityChangeSet changeSet       = wcfAsyncResult.EntityChangeSet;

            IEnumerable <ChangeSetEntry> returnValue;

            try
            {
                try
                {
                    returnValue = (IEnumerable <ChangeSetEntry>)endSubmitMethod.Invoke(wcfAsyncResult.Channel, new object[] { wcfAsyncResult.InnerAsyncResult });
                }
                catch (TargetInvocationException tie)
                {
                    if (tie.InnerException != null)
                    {
                        throw tie.InnerException;
                    }

                    throw;
                }
                finally
                {
                    ((IChannel)wcfAsyncResult.Channel).Close();
                }
            }
            catch (FaultException <DomainServiceFault> fe)
            {
                throw WebDomainClient <TContract> .GetExceptionFromServiceFault(fe.Detail);
            }

            return(new SubmitCompletedResult(changeSet, returnValue ?? Enumerable.Empty <ChangeSetEntry>()));
        }
        /// <summary>
        /// Gets the results of a query.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies a query.</param>
        /// <returns>The results returned by the query.</returns>
        protected sealed override QueryCompletedResult EndQueryCore(IAsyncResult asyncResult)
        {
            WebDomainClientAsyncResult <TContract> wcfAsyncResult = this.EndAsyncResult(asyncResult, AsyncOperationType.Query, /* cancel */ false);
            MethodInfo endQueryMethod = (MethodInfo)wcfAsyncResult.EndOperationMethod;

            IEnumerable <ValidationResult> validationErrors = null;
            QueryResult returnValue = null;

            try
            {
                try
                {
                    returnValue = (QueryResult)endQueryMethod.Invoke(wcfAsyncResult.Channel, new object[] { wcfAsyncResult.InnerAsyncResult });
                }
                catch (TargetInvocationException tie)
                {
                    if (tie.InnerException != null)
                    {
                        throw tie.InnerException;
                    }

                    throw;
                }
                finally
                {
                    ((IChannel)wcfAsyncResult.Channel).Close();
                }
            }
            catch (FaultException <DomainServiceFault> fe)
            {
                if (fe.Detail.OperationErrors != null)
                {
                    validationErrors = fe.Detail.GetValidationErrors();
                }
                else
                {
                    throw WebDomainClient <TContract> .GetExceptionFromServiceFault(fe.Detail);
                }
            }

            if (returnValue != null)
            {
                return(new QueryCompletedResult(
                           returnValue.GetRootResults().Cast <Entity>(),
                           returnValue.GetIncludedResults().Cast <Entity>(),
                           returnValue.TotalCount,
                           Enumerable.Empty <ValidationResult>()));
            }
            else
            {
                return(new QueryCompletedResult(
                           new Entity[0],
                           new Entity[0],
                           /* totalCount */ 0,
                           validationErrors ?? Enumerable.Empty <ValidationResult>()));
            }
        }
        /// <summary>
        /// Transitions an <see cref="IAsyncResult"/> instance to a completed state.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies an invocation.</param>
        /// <param name="operationType">The expected operation type.</param>
        /// <param name="cancel">Boolean indicating whether or not the operation has been canceled.</param>
        /// <returns>A <see cref="WebDomainClientAsyncResult&lt;TContract&gt;"/> reference.</returns>
        /// <exception cref="ArgumentNullException"> if <paramref name="asyncResult"/> is null.</exception>
        /// <exception cref="ArgumentException"> if <paramref name="asyncResult"/> is not of type <cref name="TAsyncResult"/>.</exception>
        /// <exception cref="InvalidOperationException"> if <paramref name="asyncResult"/> has been canceled.</exception>
        /// <exception cref="InvalidOperationException"> if <paramref name="asyncResult"/>'s End* method has already been invoked.</exception>
        /// <exception cref="InvalidOperationException"> if <paramref name="asyncResult"/> has not completed.</exception>
        private WebDomainClientAsyncResult <TContract> EndAsyncResult(IAsyncResult asyncResult, AsyncOperationType operationType, bool cancel)
        {
            WebDomainClientAsyncResult <TContract> wcfClientResult = asyncResult as WebDomainClientAsyncResult <TContract>;

            if ((wcfClientResult != null) && (!object.ReferenceEquals(this, wcfClientResult.DomainClient) || wcfClientResult.AsyncOperationType != operationType))
            {
                throw new ArgumentException(Resources.WrongAsyncResult, "asyncResult");
            }

            return(AsyncResultBase.EndAsyncOperation <WebDomainClientAsyncResult <TContract> >(asyncResult, cancel));
        }
        /// <summary>
        /// Invokes an operation asynchronously.
        /// </summary>
        /// <param name="invokeArgs">The arguments to the Invoke operation.</param>
        /// <param name="callback">The callback to invoke when the invocation has been completed.</param>
        /// <param name="userState">Optional user state that will be passed through on the <see cref="InvokeCompletedResult"/>.</param>
        /// <returns>An asynchronous result that identifies this invocation.</returns>
        /// <exception cref="InvalidOperationException">The specified query does not exist.</exception>
        protected sealed override IAsyncResult BeginInvokeCore(InvokeArgs invokeArgs, AsyncCallback callback, object userState)
        {
            MethodInfo beginInvokeMethod = WebDomainClient <TContract> .ResolveBeginMethod(invokeArgs.OperationName);

            MethodInfo endInvokeMethod = WebDomainClient <TContract> .ResolveEndMethod(invokeArgs.OperationName);

            // Pass operation parameters.
            ParameterInfo[] parameterInfos  = beginInvokeMethod.GetParameters();
            object[]        realParameters  = new object[parameterInfos.Length];
            int             parametersCount = (invokeArgs.Parameters == null) ? 0 : invokeArgs.Parameters.Count;

            for (int i = 0; i < parametersCount; i++)
            {
                realParameters[i] = invokeArgs.Parameters[parameterInfos[i].Name];
            }

            TContract channel = this.ChannelFactory.CreateChannel();
            WebDomainClientAsyncResult <TContract> wcfAsyncResult = WebDomainClientAsyncResult <TContract> .CreateInvokeResult(this, channel, endInvokeMethod, invokeArgs, callback, userState);

            // Pass async operation related parameters.
            realParameters[parameterInfos.Length - 2] = new AsyncCallback(delegate(IAsyncResult asyncResponseResult)
            {
                wcfAsyncResult.InnerAsyncResult = asyncResponseResult;
                wcfAsyncResult.Complete();
            });
            realParameters[parameterInfos.Length - 1] = userState;

            IAsyncResult asyncResult;

            try
            {
                asyncResult = (IAsyncResult)beginInvokeMethod.Invoke(channel, realParameters);
            }
            catch (TargetInvocationException tie)
            {
                if (tie.InnerException != null)
                {
                    throw tie.InnerException;
                }

                throw;
            }

            if (!asyncResult.CompletedSynchronously)
            {
                wcfAsyncResult.InnerAsyncResult = asyncResult;
            }
            return(wcfAsyncResult);
        }
        /// <summary>
        /// Submit the specified <see cref="EntityChangeSet"/> to the DomainService, with the results of the operation
        /// being returned on the SubmitCompleted event args.
        /// </summary>
        /// <param name="changeSet">The changeset to submit. If the changeset is empty, an <see cref="InvalidOperationException"/> will
        /// be thrown.</param>
        /// <param name="callback">The callback to invoke when the submit has been executed.</param>
        /// <param name="userState">Optional state that will flow through to the SubmitCompleted event</param>
        /// <returns>An asynchronous result that identifies this submit.</returns>
        /// <exception cref="InvalidOperationException">The changeset is empty.</exception>
        /// <exception cref="InvalidOperationException">The specified query does not exist.</exception>
        protected sealed override IAsyncResult BeginSubmitCore(EntityChangeSet changeSet, AsyncCallback callback, object userState)
        {
            MethodInfo beginSubmitMethod = WebDomainClient <TContract> .ResolveBeginMethod("SubmitChanges");

            MethodInfo endSubmitMethod = WebDomainClient <TContract> .ResolveEndMethod("SubmitChanges");

            IEnumerable <ChangeSetEntry> submitOperations = changeSet.GetChangeSetEntries();

            TContract channel = this.ChannelFactory.CreateChannel();
            WebDomainClientAsyncResult <TContract> wcfAsyncResult = WebDomainClientAsyncResult <TContract> .CreateSubmitResult(this, channel, endSubmitMethod, changeSet, submitOperations.ToList(), callback, userState);

            object[] parameters =
            {
                submitOperations,
                new AsyncCallback(delegate(IAsyncResult asyncResponseResult)
                {
                    wcfAsyncResult.InnerAsyncResult = asyncResponseResult;
                    wcfAsyncResult.Complete();
                }),
                userState
            };

            IAsyncResult asyncResult;

            try
            {
                asyncResult = (IAsyncResult)beginSubmitMethod.Invoke(channel, parameters);
            }
            catch (TargetInvocationException tie)
            {
                if (tie.InnerException != null)
                {
                    throw tie.InnerException;
                }

                throw;
            }

            if (!asyncResult.CompletedSynchronously)
            {
                wcfAsyncResult.InnerAsyncResult = asyncResult;
            }
            return(wcfAsyncResult);
        }
        /// <summary>
        /// Gets the results of an invocation.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies an invocation.</param>
        /// <returns>The results returned by the invocation.</returns>
        protected sealed override InvokeCompletedResult EndInvokeCore(IAsyncResult asyncResult)
        {
            WebDomainClientAsyncResult <TContract> wcfAsyncResult = this.EndAsyncResult(asyncResult, AsyncOperationType.Invoke, /* cancel */ false);
            MethodInfo endInvokeMethod = (MethodInfo)wcfAsyncResult.EndOperationMethod;

            IEnumerable <ValidationResult> validationErrors = null;
            object returnValue = null;

            try
            {
                try
                {
                    returnValue = endInvokeMethod.Invoke(wcfAsyncResult.Channel, new object[] { wcfAsyncResult.InnerAsyncResult });
                }
                catch (TargetInvocationException tie)
                {
                    if (tie.InnerException != null)
                    {
                        throw tie.InnerException;
                    }

                    throw;
                }
                finally
                {
                    ((IChannel)wcfAsyncResult.Channel).Close();
                }
            }
            catch (FaultException <DomainServiceFault> fe)
            {
                if (fe.Detail.OperationErrors != null)
                {
                    validationErrors = fe.Detail.GetValidationErrors();
                }
                else
                {
                    throw WebDomainClient <TContract> .GetExceptionFromServiceFault(fe.Detail);
                }
            }

            return(new InvokeCompletedResult(returnValue, validationErrors ?? Enumerable.Empty <ValidationResult>()));
        }
        /// <summary>
        /// Attempts to cancel the invocation request specified by the <paramref name="asyncResult"/>.
        /// </summary>
        /// <param name="asyncResult">An <see cref="IAsyncResult"/> specifying what invocation operation to cancel.</param>
        protected sealed override void CancelInvokeCore(IAsyncResult asyncResult)
        {
            WebDomainClientAsyncResult <TContract> wcfAsyncResult = this.EndAsyncResult(asyncResult, AsyncOperationType.Invoke, /* cancel */ true);

            ((IChannel)wcfAsyncResult.Channel).Abort();
        }
        /// <summary>
        /// Method called by the framework to begin an asynchronous query operation
        /// </summary>
        /// <param name="query">The query to invoke.</param>
        /// <param name="callback">The callback to invoke when the query has been executed.</param>
        /// <param name="userState">Optional state associated with this operation.</param>
        /// <returns>An asynchronous result that identifies this query.</returns>
        /// <exception cref="InvalidOperationException">The specified query does not exist.</exception>
        protected sealed override IAsyncResult BeginQueryCore(EntityQuery query, AsyncCallback callback, object userState)
        {
            MethodInfo beginQueryMethod = WebDomainClient <TContract> .ResolveBeginMethod(query.QueryName);

            MethodInfo endQueryMethod = WebDomainClient <TContract> .ResolveEndMethod(query.QueryName);

            // Pass query parameters.
            ParameterInfo[] parameterInfos  = beginQueryMethod.GetParameters();
            object[]        realParameters  = new object[parameterInfos.Length];
            int             parametersCount = (query.Parameters == null) ? 0 : query.Parameters.Count;

            for (int i = 0; i < parametersCount; i++)
            {
                realParameters[i] = query.Parameters[parameterInfos[i].Name];
            }

            TContract channel = this.ChannelFactory.CreateChannel();

            WebDomainClientAsyncResult <TContract> wcfAsyncResult = WebDomainClientAsyncResult <TContract> .CreateQueryResult(this, channel, endQueryMethod, callback, userState);

            // Pass async operation related parameters.
            realParameters[parameterInfos.Length - 2] = new AsyncCallback(delegate(IAsyncResult asyncResponseResult)
            {
                wcfAsyncResult.InnerAsyncResult = asyncResponseResult;
                wcfAsyncResult.Complete();
            });
            realParameters[parameterInfos.Length - 1] = userState;

            IAsyncResult asyncResult;

            try
            {
                // Pass the query as a message property.
                using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
                {
                    if (query.Query != null)
                    {
                        OperationContext.Current.OutgoingMessageProperties.Add(WebDomainClient <object> .QueryPropertyName, query.Query);
                    }
                    if (query.IncludeTotalCount)
                    {
                        OperationContext.Current.OutgoingMessageProperties.Add(WebDomainClient <object> .IncludeTotalCountPropertyName, true);
                    }

                    asyncResult = (IAsyncResult)beginQueryMethod.Invoke(channel, realParameters);
                }
            }
            catch (TargetInvocationException tie)
            {
                if (tie.InnerException != null)
                {
                    throw tie.InnerException;
                }

                throw;
            }

            if (!asyncResult.CompletedSynchronously)
            {
                wcfAsyncResult.InnerAsyncResult = asyncResult;
            }

            return(wcfAsyncResult);
        }