internal void SetCurrent(ref MessageRpc rpc)
        {
            Transaction current = rpc.Transaction.Current;

            if (current == null)
            {
                DiagnosticUtility.FailFast("we should never get here with a requestTransaction null");
            }
            lock (this.mutex)
            {
                if (this.current == null)
                {
                    this.current = current;
                }
                else if (this.current != current)
                {
                    this.waiting = current;
                    this.paused  = rpc.Pause();
                }
                else
                {
                    rpc.Transaction.Current = this.current;
                }
            }
        }
        // ........................................................................................................

        internal void SetCurrent(ref MessageRpc rpc)
        {
            Transaction requestTransaction = rpc.Transaction.Current;

            if (!(requestTransaction != null))
            {
                // tx processing requires failfast when state is inconsistent
                DiagnosticUtility.FailFast("we should never get here with a requestTransaction null");
            }

            lock (this.mutex)
            {
                if (this.current == null)
                {
                    this.current = requestTransaction;
                }
                else if (this.current != requestTransaction)
                {
                    this.waiting = requestTransaction;
                    this.paused  = rpc.Pause();
                }
                else
                {
                    rpc.Transaction.Current = this.current; //rpc.Transaction.Current should get the dependent clone
                }
            }
        }
        private void SynchronizationContextEndCallback(object state)
        {
            IResumeMessageRpc resume = (IResumeMessageRpc)state;

            this.ResumeProcessing(resume);
            this.GetSyncContext(resume.GetMessageInstanceContext()).OperationCompleted();
        }
            public AcknowledgementCompleteAsyncResult(
                ReceiveContext receiveContext,
                TimeSpan timeout,
                ref MessageRpc rpc,
                Transaction transaction,
                AsyncCallback callback,
                object state) : base(callback, state)
            {
                this.receiveContext     = receiveContext;
                this.currentTransaction = transaction;
                this.channelHandler     = rpc.channelHandler;
                this.resumableRPC       = rpc.Pause();

                bool completeThrew = true;

                try
                {
                    bool completed = this.Complete(timeout);
                    completeThrew = false;

                    if (completed)
                    {
                        this.resumableRPC = null;
                        rpc.UnPause();
                        this.Complete(true);
                    }
                }
                finally
                {
                    if (completeThrew)
                    {
                        rpc.UnPause();
                    }
                }
            }
 private void ResumeProcessing(IResumeMessageRpc resume)
 {
     bool flag;
     resume.Resume(out flag);
     if (flag)
     {
         string message = System.ServiceModel.SR.GetString("SFxMultipleCallbackFromSynchronizationContext", new object[] { this.context.GetType().ToString() });
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(message));
     }
 }
Example #6
0
        //        static void OnFinalizeCorrelationCompletedCallback(IAsyncResult result)
        //        {
        //            if (result.CompletedSynchronously)
        //            {
        //                return;
        //            }

        //            IResumeMessageRpc resume = result.AsyncState as IResumeMessageRpc;

        //            if (resume == null)
        //            {
        //                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SFxInvalidAsyncResultState0));
        //            }

        //            resume.Resume(result);
        //        }

        static void OnReplyCompletedCallback(Task result, object state)
        {
            IResumeMessageRpc resume = state as IResumeMessageRpc;

            if (resume == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SFxInvalidAsyncResultState0);
            }

            resume.Resume(result);
        }
        private void ResumeProcessing(IResumeMessageRpc resume)
        {
            bool flag;

            resume.Resume(out flag);
            if (flag)
            {
                string message = System.ServiceModel.SR.GetString("SFxMultipleCallbackFromSynchronizationContext", new object[] { this.context.GetType().ToString() });
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(message));
            }
        }
        private void SynchronizationContextEndCallback(object state)
        {
            IResumeMessageRpc resume = (IResumeMessageRpc)state;

            ResumeProcessing(resume);

            SynchronizationContext syncContext = GetSyncContext(resume.GetMessageInstanceContext());

            Fx.Assert(syncContext != null, "syncContext is null !?");
            syncContext.OperationCompleted();
        }
        private void ResumeProcessing(IResumeMessageRpc resume)
        {
            bool alreadyResumedNoLock;

            resume.Resume(out alreadyResumedNoLock);

            if (alreadyResumedNoLock)
            {
                string text = SR.Format(SR.SFxMultipleCallbackFromSynchronizationContext, _context.GetType().ToString());
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
            }
        }
Example #10
0
 private static void InvokeCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         IResumeMessageRpc asyncState = result.AsyncState as IResumeMessageRpc;
         if (asyncState == null)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("SFxInvalidAsyncResultState0"));
         }
         asyncState.SignalConditionalResume(result);
     }
 }
Example #11
0
        static void InvokeCallback(IAsyncResult result)
        {
            if (result.CompletedSynchronously)
            {
                return;
            }

            IResumeMessageRpc resume = result.AsyncState as IResumeMessageRpc;

            if (resume == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SFxInvalidAsyncResultState0);
            }

            resume.SignalConditionalResume(result);
        }
Example #12
0
        private static void OnReplyCompletedCallback(IAsyncResult result)
        {
            if (result.CompletedSynchronously)
            {
                return;
            }

            IResumeMessageRpc resume = result.AsyncState as IResumeMessageRpc;

            if (resume == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SRServiceModel.SFxInvalidAsyncResultState0);
            }

            resume.Resume(result);
        }
        private void BeginReply(ref MessageRpc rpc)
        {
            bool success = false;

            try
            {
                IResumeMessageRpc resume = rpc.Pause();

                rpc.AsyncResult = rpc.RequestContext.BeginReply(rpc.Reply, rpc.ReplyTimeoutHelper.RemainingTime(),
                                                                s_onReplyCompleted, resume);
                success = true;

                if (rpc.AsyncResult.CompletedSynchronously)
                {
                    rpc.UnPause();
                }
            }
            catch (CommunicationException e)
            {
                _error.HandleError(e);
            }
            catch (TimeoutException e)
            {
                _error.HandleError(e);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                if (!_error.HandleError(e))
                {
                    rpc.RequestContextThrewOnReply = true;
                    rpc.CanSendReply = false;
                }
            }
            finally
            {
                if (!success)
                {
                    rpc.UnPause();
                }
            }
        }
Example #14
0
        internal void InvokeBegin(ref MessageRpc rpc)
        {
            if (rpc.Error == null)
            {
                object target = rpc.Instance;
                this.DeserializeInputs(ref rpc);
                this.InspectInputs(ref rpc);

                ValidateMustUnderstand(ref rpc);

                IAsyncResult result;
                bool         isBeginSuccessful = false;

                IResumeMessageRpc resumeRpc = rpc.Pause();
                try
                {
                    result            = Invoker.InvokeBegin(target, rpc.InputParameters, invokeCallback, resumeRpc);
                    isBeginSuccessful = true;
                }
                finally
                {
                    if (!isBeginSuccessful)
                    {
                        rpc.UnPause();
                    }
                }

                if (result == null)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("IOperationInvoker.BeginDispatch"),
                                                        rpc.Request);
                }

                if (result.CompletedSynchronously)
                {
                    // if the async call completed synchronously, then the responsibility to call
                    // ProcessMessage{6,7,Cleanup} still remains on this thread
                    rpc.UnPause();
                    rpc.AsyncResult = result;
                }
            }
        }
        private void BindCore(ref MessageRpc rpc, bool startOperation)
        {
            SynchronizationContext syncContext = GetSyncContext(rpc.InstanceContext);

            if (syncContext != null)
            {
                IResumeMessageRpc resume = rpc.Pause();
                if (startOperation)
                {
                    syncContext.OperationStarted();
                    syncContext.Post(ThreadAffinityStartCallbackDelegate, resume);
                }
                else
                {
                    syncContext.Post(ThreadAffinityEndCallbackDelegate, resume);
                }
            }
            else if (rpc.SwitchedThreads)
            {
                IResumeMessageRpc resume = rpc.Pause();
                ActionItem.Schedule(ThreadBehavior.CleanThreadCallbackDelegate, resume);
            }
        }
        private void ResumeProcessing(IResumeMessageRpc resume)
        {
            bool alreadyResumedNoLock;
            resume.Resume(out alreadyResumedNoLock);

            if (alreadyResumedNoLock)
            {
                string text = SR.Format(SR.SFxMultipleCallbackFromSynchronizationContext, _context.GetType().ToString());
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
            }
        }
Example #17
0
 internal void InvokeBegin(ref MessageRpc rpc)
 {
     if (rpc.Error == null)
     {
         try
         {
             this.InitializeCallContext(ref rpc);
             object instance = rpc.Instance;
             this.DeserializeInputs(ref rpc);
             this.InspectInputs(ref rpc);
             this.ValidateMustUnderstand(ref rpc);
             IAsyncResult result = null;
             IDisposable  impersonationContext = null;
             IPrincipal   originalPrincipal    = null;
             bool         isThreadPrincipalSet = false;
             bool         flag2 = this.Parent.IsConcurrent(ref rpc);
             try
             {
                 if (this.parent.SecurityImpersonation != null)
                 {
                     this.parent.SecurityImpersonation.StartImpersonation(ref rpc, out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
                 }
                 IManualConcurrencyOperationInvoker invoker = this.Invoker as IManualConcurrencyOperationInvoker;
                 if (this.isSynchronous)
                 {
                     if ((invoker != null) && flag2)
                     {
                         if (this.bufferedReceiveEnabled)
                         {
                             rpc.OperationContext.IncomingMessageProperties.Add(BufferedReceiveMessageProperty.Name, new BufferedReceiveMessageProperty(ref rpc));
                         }
                         rpc.ReturnParameter = invoker.Invoke(instance, rpc.InputParameters, rpc.InvokeNotification, out rpc.OutputParameters);
                     }
                     else
                     {
                         rpc.ReturnParameter = this.Invoker.Invoke(instance, rpc.InputParameters, out rpc.OutputParameters);
                     }
                 }
                 else
                 {
                     bool flag3 = false;
                     if (((invoker != null) && flag2) && this.bufferedReceiveEnabled)
                     {
                         rpc.OperationContext.IncomingMessageProperties.Add(BufferedReceiveMessageProperty.Name, new BufferedReceiveMessageProperty(ref rpc));
                     }
                     IResumeMessageRpc state = rpc.Pause();
                     try
                     {
                         if ((invoker != null) && flag2)
                         {
                             result = invoker.InvokeBegin(instance, rpc.InputParameters, rpc.InvokeNotification, invokeCallback, state);
                         }
                         else
                         {
                             result = this.Invoker.InvokeBegin(instance, rpc.InputParameters, invokeCallback, state);
                         }
                         flag3 = true;
                     }
                     finally
                     {
                         if (!flag3)
                         {
                             rpc.UnPause();
                         }
                     }
                 }
             }
             finally
             {
                 try
                 {
                     if (this.parent.SecurityImpersonation != null)
                     {
                         this.parent.SecurityImpersonation.StopImpersonation(ref rpc, impersonationContext, originalPrincipal, isThreadPrincipalSet);
                     }
                 }
                 catch
                 {
                     string message = null;
                     try
                     {
                         message = System.ServiceModel.SR.GetString("SFxRevertImpersonationFailed0");
                     }
                     finally
                     {
                         DiagnosticUtility.FailFast(message);
                     }
                 }
             }
             if (this.isSynchronous)
             {
                 this.InspectOutputs(ref rpc);
                 this.SerializeOutputs(ref rpc);
             }
             else
             {
                 if (result == null)
                 {
                     throw TraceUtility.ThrowHelperError(new ArgumentNullException("IOperationInvoker.BeginDispatch"), rpc.Request);
                 }
                 if (result.CompletedSynchronously)
                 {
                     rpc.UnPause();
                     rpc.AsyncResult = result;
                 }
             }
         }
         catch
         {
             throw;
         }
         finally
         {
             this.UninitializeCallContext(ref rpc);
         }
     }
 }
Example #18
0
 internal MessageRpcWaiter(IResumeMessageRpc resume)
 {
     this.resume = resume;
 }
            public AcknowledgementCompleteAsyncResult(
                ReceiveContext receiveContext,
                TimeSpan timeout,
                ref MessageRpc rpc,
                Transaction transaction,
                AsyncCallback callback,
                object state) : base(callback, state)
            {
                this.receiveContext = receiveContext;
                this.currentTransaction = transaction;
                this.channelHandler = rpc.channelHandler;
                this.resumableRPC = rpc.Pause();

                bool completeThrew = true;
                try
                {
                    bool completed = this.Complete(timeout);
                    completeThrew = false;

                    if (completed)
                    {
                        this.resumableRPC = null;
                        rpc.UnPause();
                        this.Complete(true);
                    }
                }
                finally
                {
                    if (completeThrew)
                    {
                        rpc.UnPause();
                    }
                }
            }
        // ........................................................................................................

        internal void SetCurrent(ref MessageRpc rpc)
        {
            Transaction requestTransaction = rpc.Transaction.Current;

            if (!(requestTransaction != null))
            {
                // tx processing requires failfast when state is inconsistent
                DiagnosticUtility.FailFast("we should never get here with a requestTransaction null");
            }

            lock (this.mutex)
            {
                if (this.current == null)
                {
                    this.current = requestTransaction;
                }
                else if (this.current != requestTransaction)
                {
                    this.waiting = requestTransaction;
                    this.paused = rpc.Pause();
                }
                else
                {
                    rpc.Transaction.Current = this.current; //rpc.Transaction.Current should get the dependent clone
                }
            }
        }
        internal void InvokeBegin(ref MessageRpc rpc)
        {
            if (rpc.Error == null)
            {
                try
                {
                    this.InitializeCallContext(ref rpc);
                    object target = rpc.Instance;
                    this.DeserializeInputs(ref rpc);
                    this.InspectInputs(ref rpc);

                    ValidateMustUnderstand(ref rpc);

                    IAsyncResult result = null;
                    IDisposable  impersonationContext = null;
                    IPrincipal   originalPrincipal    = null;
                    bool         isThreadPrincipalSet = false;
                    bool         isConcurrent         = this.Parent.IsConcurrent(ref rpc);

                    try
                    {
                        if (this.parent.RequireClaimsPrincipalOnOperationContext)
                        {
                            SetClaimsPrincipalToOperationContext(rpc);
                        }

                        if (this.parent.SecurityImpersonation != null)
                        {
                            this.parent.SecurityImpersonation.StartImpersonation(ref rpc, out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
                        }
                        IManualConcurrencyOperationInvoker manualInvoker = this.Invoker as IManualConcurrencyOperationInvoker;

                        if (DS.OperationInvokerIsEnabled())
                        {
                            DS.InvokeOperationStart(this.Invoker.GetType(), Stopwatch.GetTimestamp());
                        }

                        if (this.isSynchronous)
                        {
                            if (manualInvoker != null && isConcurrent)
                            {
                                if (this.bufferedReceiveEnabled)
                                {
                                    rpc.OperationContext.IncomingMessageProperties.Add(
                                        BufferedReceiveMessageProperty.Name, new BufferedReceiveMessageProperty(ref rpc));
                                }
                                rpc.ReturnParameter = manualInvoker.Invoke(target, rpc.InputParameters, rpc.InvokeNotification, out rpc.OutputParameters);
                            }
                            else
                            {
                                rpc.ReturnParameter = this.Invoker.Invoke(target, rpc.InputParameters, out rpc.OutputParameters);
                            }
                        }
                        else
                        {
                            bool isBeginSuccessful = false;

                            if (manualInvoker != null && isConcurrent && this.bufferedReceiveEnabled)
                            {
                                // This will modify the rpc, it has to be done before rpc.Pause
                                // since IResumeMessageRpc implementation keeps reference of rpc.
                                // This is to ensure consistent rpc whether or not InvokeBegin completed
                                // synchronously or asynchronously.
                                rpc.OperationContext.IncomingMessageProperties.Add(
                                    BufferedReceiveMessageProperty.Name, new BufferedReceiveMessageProperty(ref rpc));
                            }

                            IResumeMessageRpc resumeRpc = rpc.Pause();
                            try
                            {
                                if (manualInvoker != null && isConcurrent)
                                {
                                    result = manualInvoker.InvokeBegin(target, rpc.InputParameters, rpc.InvokeNotification, invokeCallback, resumeRpc);
                                }
                                else
                                {
                                    result = this.Invoker.InvokeBegin(target, rpc.InputParameters, invokeCallback, resumeRpc);
                                }

                                isBeginSuccessful = true;
                                // if the call above actually went async, then responsibility to call
                                // ProcessMessage{6,7,Cleanup} has been transferred to InvokeCallback
                            }
                            finally
                            {
                                if (!isBeginSuccessful)
                                {
                                    rpc.UnPause();
                                }
                            }
                        }
                    }
                    finally
                    {
                        try
                        {
                            if (this.parent.SecurityImpersonation != null)
                            {
                                this.parent.SecurityImpersonation.StopImpersonation(ref rpc, impersonationContext, originalPrincipal, isThreadPrincipalSet);
                            }
                        }
#pragma warning suppress 56500 // covered by FxCOP
                        catch
                        {
                            string message = null;
                            try
                            {
                                message = SR.GetString(SR.SFxRevertImpersonationFailed0);
                            }
                            finally
                            {
                                DiagnosticUtility.FailFast(message);
                            }
                        }

                        if (this.isSynchronous && DS.OperationInvokerIsEnabled())
                        {
                            DS.InvokeOperationStop(Stopwatch.GetTimestamp());
                        }
                    }

                    if (this.isSynchronous)
                    {
                        this.InspectOutputs(ref rpc);

                        this.SerializeOutputs(ref rpc);
                    }
                    else
                    {
                        if (result == null)
                        {
                            throw TraceUtility.ThrowHelperError(new ArgumentNullException("IOperationInvoker.BeginDispatch"), rpc.Request);
                        }

                        if (result.CompletedSynchronously)
                        {
                            // if the async call completed synchronously, then the responsibility to call
                            // ProcessMessage{6,7,Cleanup} still remains on this thread
                            rpc.UnPause();
                            rpc.AsyncResult = result;
                        }
                    }
                }
#pragma warning suppress 56500 // covered by FxCOP
                catch { throw; } // Make sure user Exception filters are not impersonated.
                finally
                {
                    this.UninitializeCallContext(ref rpc);
                }
            }
        }
 internal void SetCurrent(ref MessageRpc rpc)
 {
     Transaction current = rpc.Transaction.Current;
     if (current == null)
     {
         DiagnosticUtility.FailFast("we should never get here with a requestTransaction null");
     }
     lock (this.mutex)
     {
         if (this.current == null)
         {
             this.current = current;
         }
         else if (this.current != current)
         {
             this.waiting = current;
             this.paused = rpc.Pause();
         }
         else
         {
             rpc.Transaction.Current = this.current;
         }
     }
 }
Example #23
0
 internal MessageRpcWaiter(IResumeMessageRpc resume)
 {
     _resume = resume;
 }