void SerializeOutputs(ref MessageRpc rpc) { if (!this.IsOneWay && _parent.EnableFaults) { Message reply; if (_serializeReply) { if (TD.DispatchFormatterSerializeReplyStartIsEnabled()) { TD.DispatchFormatterSerializeReplyStart(rpc.EventTraceActivity); } reply = this.Formatter.SerializeReply(rpc.RequestVersion, rpc.OutputParameters, rpc.ReturnParameter); if (TD.DispatchFormatterSerializeReplyStopIsEnabled()) { TD.DispatchFormatterSerializeReplyStop(rpc.EventTraceActivity); } if (reply == null) { string message = SR.Format(SR.SFxNullReplyFromFormatter2, this.Formatter.GetType().ToString(), (_name ?? "")); ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message)); } } else { if ((rpc.ReturnParameter == null) && (rpc.OperationContext.RequestContext != null)) { string message = SR.Format(SR.SFxDispatchRuntimeMessageCannotBeNull, _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.Format(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); } } } if (MessageLogger.LoggingEnabled && null != reply) { MessageLogger.LogMessage(ref reply, MessageLoggingSource.ServiceLevelSendReply | MessageLoggingSource.LastChance); } rpc.Reply = reply; } }
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; } }