Exemple #1
0
        private void InitializeCallContextCore(ref MessageRpc rpc)
        {
            IClientChannel proxy = rpc.Channel.Proxy as IClientChannel;
            int            callContextCorrelationOffset = this.Parent.CallContextCorrelationOffset;

            try
            {
                for (int i = 0; i < rpc.Operation.CallContextInitializers.Length; i++)
                {
                    rpc.Correlation[callContextCorrelationOffset + i] = this.CallContextInitializers[i].BeforeInvoke(rpc.InstanceContext, proxy, rpc.Request);
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                if (ErrorBehavior.ShouldRethrowExceptionAsIs(exception))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
            }
        }
Exemple #2
0
        internal void AfterReceiveRequestCore(ref MessageRpc rpc)
        {
            int offset = MessageInspectorCorrelationOffset;

            try
            {
                for (int i = 0; i < _messageInspectors.Length; i++)
                {
                    rpc.Correlation[offset + i] = _messageInspectors[i].AfterReceiveRequest(ref rpc.Request, (IClientChannel)rpc.Channel.Proxy, rpc.InstanceContext);
                    if (WcfEventSource.Instance.MessageInspectorAfterReceiveInvokedIsEnabled())
                    {
                        WcfEventSource.Instance.MessageInspectorAfterReceiveInvoked(rpc.EventTraceActivity, _messageInspectors[i].GetType().FullName);
                    }
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                if (ErrorBehavior.ShouldRethrowExceptionAsIs(e))
                {
                    throw;
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
            }
        }
Exemple #3
0
        private void DeserializeInputs(ref MessageRpc rpc)
        {
            bool flag = false;

            try
            {
                try
                {
                    rpc.InputParameters = this.Invoker.AllocateInputs();
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    if (ErrorBehavior.ShouldRethrowExceptionAsIs(exception))
                    {
                        throw;
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
                }
                try
                {
                    if (this.deserializeRequest)
                    {
                        this.Formatter.DeserializeRequest(rpc.Request, rpc.InputParameters);
                    }
                    else
                    {
                        rpc.InputParameters[0] = rpc.Request;
                    }
                    flag = true;
                }
                catch (Exception exception2)
                {
                    if (Fx.IsFatal(exception2))
                    {
                        throw;
                    }
                    if (ErrorBehavior.ShouldRethrowExceptionAsIs(exception2))
                    {
                        throw;
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception2);
                }
            }
            finally
            {
                rpc.DidDeserializeRequestBody = rpc.Request.State != MessageState.Created;
                if (!flag && MessageLogger.LoggingEnabled)
                {
                    MessageLogger.LogMessage(ref rpc.Request, MessageLoggingSource.Malformed);
                }
            }
        }
Exemple #4
0
        internal void BeforeSendReplyCore(ref MessageRpc rpc, ref Exception exception, ref bool thereIsAnUnhandledException)
        {
            int offset = MessageInspectorCorrelationOffset;

            for (int i = 0; i < _messageInspectors.Length; i++)
            {
                try
                {
                    Message originalReply = rpc.Reply;
                    Message reply         = originalReply;

                    _messageInspectors[i].BeforeSendReply(ref reply, rpc.Correlation[offset + i]);
                    if (WcfEventSource.Instance.MessageInspectorBeforeSendInvokedIsEnabled())
                    {
                        WcfEventSource.Instance.MessageInspectorBeforeSendInvoked(rpc.EventTraceActivity, _messageInspectors[i].GetType().FullName);
                    }

                    if ((reply == null) && (originalReply != null))
                    {
                        string message = SR.Format(SR.SFxNullReplyFromExtension2, _messageInspectors[i].GetType().ToString(), (rpc.Operation.Name ?? ""));
                        ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message));
                    }
                    rpc.Reply = reply;
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    if (!ErrorBehavior.ShouldRethrowExceptionAsIs(e))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                    }

                    if (exception == null)
                    {
                        exception = e;
                    }
                    thereIsAnUnhandledException = (!_error.HandleError(e)) || thereIsAnUnhandledException;
                }
            }
        }
Exemple #5
0
 private void SerializeOutputs(ref MessageRpc rpc)
 {
     if (!this.IsOneWay && this.parent.EnableFaults)
     {
         Message returnParameter;
         if (this.serializeReply)
         {
             try
             {
                 returnParameter = this.Formatter.SerializeReply(rpc.RequestVersion, rpc.OutputParameters, rpc.ReturnParameter);
             }
             catch (Exception exception)
             {
                 if (Fx.IsFatal(exception))
                 {
                     throw;
                 }
                 if (ErrorBehavior.ShouldRethrowExceptionAsIs(exception))
                 {
                     throw;
                 }
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(exception);
             }
             if (returnParameter == null)
             {
                 object[] args = new object[] { this.Formatter.GetType().ToString(), this.name ?? "" };
                 ErrorBehavior.ThrowAndCatch(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxNullReplyFromFormatter2", args)));
             }
         }
         else
         {
             if ((rpc.ReturnParameter == null) && (rpc.OperationContext.RequestContext != null))
             {
                 ErrorBehavior.ThrowAndCatch(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxDispatchRuntimeMessageCannotBeNull", new object[] { this.name })));
             }
             returnParameter = (Message)rpc.ReturnParameter;
             if ((returnParameter != null) && !ProxyOperationRuntime.IsValidAction(returnParameter, this.ReplyAction))
             {
                 object[] objArray3 = new object[] { this.Name, returnParameter.Headers.Action ?? "{NULL}", this.ReplyAction };
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("SFxInvalidReplyAction", objArray3)));
             }
         }
         if ((DiagnosticUtility.ShouldUseActivity && (rpc.Activity != null)) && (returnParameter != null))
         {
             TraceUtility.SetActivity(returnParameter, rpc.Activity);
             if (TraceUtility.ShouldPropagateActivity)
             {
                 TraceUtility.AddActivityHeader(returnParameter);
             }
         }
         else if ((TraceUtility.ShouldPropagateActivity && (returnParameter != null)) && (rpc.ResponseActivityId != Guid.Empty))
         {
             new ActivityIdHeader(rpc.ResponseActivityId).AddTo(returnParameter);
         }
         if (TraceUtility.MessageFlowTracingOnly)
         {
             if ((rpc.OperationContext.IncomingMessage != null) && (MessageState.Closed != rpc.OperationContext.IncomingMessage.State))
             {
                 FxTrace.Trace.SetAndTraceTransfer(TraceUtility.GetReceivedActivityId(rpc.OperationContext), true);
             }
             else if (rpc.ResponseActivityId != Guid.Empty)
             {
                 FxTrace.Trace.SetAndTraceTransfer(rpc.ResponseActivityId, true);
             }
         }
         if (MessageLogger.LoggingEnabled && (returnParameter != null))
         {
             MessageLogger.LogMessage(ref returnParameter, MessageLoggingSource.LastChance | MessageLoggingSource.ServiceLevelSendReply);
         }
         rpc.Reply = returnParameter;
     }
 }
        void SerializeOutputs(ref MessageRpc rpc)
        {
            if (!this.IsOneWay && this.parent.EnableFaults)
            {
                Message reply;
                if (this.serializeReply)
                {
                    try
                    {
                        if (TD.DispatchFormatterSerializeReplyStartIsEnabled())
                        {
                            TD.DispatchFormatterSerializeReplyStart(rpc.EventTraceActivity);
                        }

                        bool      outputTiming = DS.MessageFormatterIsEnabled();
                        Stopwatch sw           = null;
                        if (outputTiming)
                        {
                            sw = Stopwatch.StartNew();
                        }

                        reply = this.Formatter.SerializeReply(rpc.RequestVersion, rpc.OutputParameters, rpc.ReturnParameter);

                        if (outputTiming)
                        {
                            DS.DispatchMessageFormatterSerialize(this.Formatter.GetType(), sw.Elapsed);
                        }

                        if (TD.DispatchFormatterSerializeReplyStopIsEnabled())
                        {
                            TD.DispatchFormatterSerializeReplyStop(rpc.EventTraceActivity);
                        }
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        if (ErrorBehavior.ShouldRethrowExceptionAsIs(e))
                        {
                            throw;
                        }
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                    }

                    if (reply == null)
                    {
                        string message = SR.GetString(SR.SFxNullReplyFromFormatter2, this.Formatter.GetType().ToString(), (this.name ?? ""));
                        ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message));
                    }
                }
                else
                {
                    if ((rpc.ReturnParameter == null) && (rpc.OperationContext.RequestContext != null))
                    {
                        string message = SR.GetString(SR.SFxDispatchRuntimeMessageCannotBeNull, this.name);
                        ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message));
                    }

                    reply = (Message)rpc.ReturnParameter;

                    if ((reply != null) && (!ProxyOperationRuntime.IsValidAction(reply, this.ReplyAction)))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidReplyAction, this.Name, reply.Headers.Action ?? "{NULL}", this.ReplyAction)));
                    }
                }

                if (DiagnosticUtility.ShouldUseActivity && rpc.Activity != null && reply != null)
                {
                    TraceUtility.SetActivity(reply, rpc.Activity);
                    if (TraceUtility.ShouldPropagateActivity)
                    {
                        TraceUtility.AddActivityHeader(reply);
                    }
                }
                else if (TraceUtility.ShouldPropagateActivity && reply != null && rpc.ResponseActivityId != Guid.Empty)
                {
                    ActivityIdHeader header = new ActivityIdHeader(rpc.ResponseActivityId);
                    header.AddTo(reply);
                }

                //rely on the property set during the message receive to correlate the trace
                if (TraceUtility.MessageFlowTracingOnly)
                {
                    //Guard against MEX scenarios where the message is closed by now
                    if (null != rpc.OperationContext.IncomingMessage && MessageState.Closed != rpc.OperationContext.IncomingMessage.State)
                    {
                        FxTrace.Trace.SetAndTraceTransfer(TraceUtility.GetReceivedActivityId(rpc.OperationContext), true);
                    }
                    else
                    {
                        if (rpc.ResponseActivityId != Guid.Empty)
                        {
                            FxTrace.Trace.SetAndTraceTransfer(rpc.ResponseActivityId, true);
                        }
                    }
                }

                // Add the ImpersonateOnSerializingReplyMessageProperty on the reply message iff
                // a. reply message is not null.
                // b. Impersonation is enabled on serializing Reply

                if (reply != null && this.parent.IsImpersonationEnabledOnSerializingReply)
                {
                    bool shouldImpersonate = this.parent.SecurityImpersonation != null && this.parent.SecurityImpersonation.IsImpersonationEnabledOnCurrentOperation(ref rpc);
                    if (shouldImpersonate)
                    {
                        reply.Properties.Add(ImpersonateOnSerializingReplyMessageProperty.Name, new ImpersonateOnSerializingReplyMessageProperty(ref rpc));
                        reply = new ImpersonatingMessage(reply);
                    }
                }

                if (MessageLogger.LoggingEnabled && null != reply)
                {
                    MessageLogger.LogMessage(ref reply, MessageLoggingSource.ServiceLevelSendReply | MessageLoggingSource.LastChance);
                }
                rpc.Reply = reply;
            }
        }
        void DeserializeInputs(ref MessageRpc rpc)
        {
            bool success = false;

            try
            {
                try
                {
                    rpc.InputParameters = this.Invoker.AllocateInputs();
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    if (ErrorBehavior.ShouldRethrowExceptionAsIs(e))
                    {
                        throw;
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                }
                try
                {
                    // If the field is true, then this operation is to be invoked at the time the service
                    // channel is opened. The incoming message is created at ChannelHandler level with no
                    // content, so we don't need to deserialize the message.
                    if (!this.isSessionOpenNotificationEnabled)
                    {
                        if (this.deserializeRequest)
                        {
                            if (TD.DispatchFormatterDeserializeRequestStartIsEnabled())
                            {
                                TD.DispatchFormatterDeserializeRequestStart(rpc.EventTraceActivity);
                            }

                            bool      outputTiming = DS.MessageFormatterIsEnabled();
                            Stopwatch sw           = null;
                            if (outputTiming)
                            {
                                sw = Stopwatch.StartNew();
                            }

                            this.Formatter.DeserializeRequest(rpc.Request, rpc.InputParameters);

                            if (outputTiming)
                            {
                                DS.DispatchMessageFormatterDeserialize(this.Formatter.GetType(), sw.Elapsed);
                            }

                            if (TD.DispatchFormatterDeserializeRequestStopIsEnabled())
                            {
                                TD.DispatchFormatterDeserializeRequestStop(rpc.EventTraceActivity);
                            }
                        }
                        else
                        {
                            rpc.InputParameters[0] = rpc.Request;
                        }
                    }

                    success = true;
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    if (ErrorBehavior.ShouldRethrowExceptionAsIs(e))
                    {
                        throw;
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                }
            }
            finally
            {
                rpc.DidDeserializeRequestBody = (rpc.Request.State != MessageState.Created);

                if (!success && MessageLogger.LoggingEnabled)
                {
                    MessageLogger.LogMessage(ref rpc.Request, MessageLoggingSource.Malformed);
                }
            }
        }