protected override void Execute(NativeActivityContext context) { MessageVersion version; SendReceiveExtension sendReceiveExtension = context.GetExtension <SendReceiveExtension>(); if (sendReceiveExtension != null) { HostSettings hostSettings = sendReceiveExtension.HostSettings; this.IncludeExceptionDetailInFaults = hostSettings.IncludeExceptionDetailInFaults; } CorrelationHandle correlatesWith = (this.CorrelatesWith == null) ? null : this.CorrelatesWith.Get(context); if (correlatesWith == null) { correlatesWith = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle; } CorrelationResponseContext responseContext; if (correlatesWith != null) { if (sendReceiveExtension != null) { if (!this.TryGetMessageVersion(correlatesWith.InstanceKey, out version)) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.MessageVersionInformationNotFound)); } } else if (correlatesWith.TryAcquireResponseContext(context, out responseContext)) { //Register the ResponseContext so that InternalSendMessage can access it. if (!correlatesWith.TryRegisterResponseContext(context, responseContext)) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.ResponseContextIsNotNull)); } //Use the same MessageVersion as the incoming message that is retrieved using CorrelatonHandle version = responseContext.MessageVersion; } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CorrelationResponseContextShouldNotBeNull)); } } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CorrelationResponseContextShouldNotBeNull)); } Fx.Assert((this.Formatter == null && this.FaultFormatter != null) || (this.Formatter != null && this.FaultFormatter == null), "OperationFormatter and FaultFormatter cannot be both null or both set!"); if (this.FaultFormatter != null) { Fx.Assert(this.parameters.Count == 1, "Exception should be the only parameter!"); Exception exception = this.parameters[0].Get(context) as Exception; Fx.Assert(exception != null, "InArgument must be an Exception!"); MessageFault messageFault; string action; FaultException faultException = exception as FaultException; if (faultException != null) { // This is an expected fault // Reproduce logic from ErrorBehavior.InitializeFault messageFault = this.FaultFormatter.Serialize(faultException, out action); if (action == null) { action = version.Addressing.DefaultFaultAction; } } else { // This is an unexpected fault // Reproduce logic from ErrorBehavior.ProvideFaultOfLastResort FaultCode code = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespaces.NetDispatch); code = FaultCode.CreateReceiverFaultCode(code); action = FaultCodeConstants.Actions.NetDispatcher; if (this.IncludeExceptionDetailInFaults) { messageFault = MessageFault.CreateFault(code, new FaultReason(new FaultReasonText(exception.Message, CultureInfo.CurrentCulture)), new ExceptionDetail(exception)); } else { messageFault = MessageFault.CreateFault(code, new FaultReason(new FaultReasonText(SR2.InternalServerError, CultureInfo.CurrentCulture))); } } if (messageFault == null) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CannotCreateMessageFault)); } else { Message outMessage = System.ServiceModel.Channels.Message.CreateMessage(version, messageFault, action); this.Message.Set(context, outMessage); } } else { object[] inObjects; if (this.parameters != null) { inObjects = new object[this.parameters.Count]; for (int i = 0; i < this.parameters.Count; i++) { Fx.Assert(this.Parameters[i] != null, "Parameter cannot be null"); inObjects[i] = this.parameters[i].Get(context); } } else { inObjects = Constants.EmptyArray; } object returnValue = null; if (this.Result != null) { returnValue = this.Result.Get(context); } Message outMessage = this.Formatter.SerializeReply(version, inObjects, returnValue); this.Message.Set(context, outMessage); } }
bool TryGetContextCorrelationInstanceKey(HostSettings hostSettings, MessageProperties messageProperties, out InstanceKey correlationContextInstanceKey) { correlationContextInstanceKey = null; ContextMessageProperty contextProperties = null; if (ContextMessageProperty.TryGet(messageProperties, out contextProperties)) { if (contextProperties.Context != null) { string instanceId = null; if (contextProperties.Context.TryGetValue(InstanceIdKey, out instanceId)) { IDictionary<string, string> keyData = new Dictionary<string, string>(1) { { WSContextInstanceIdName, instanceId } }; correlationContextInstanceKey = new CorrelationKey(keyData, hostSettings.ScopeName, null); } } } return correlationContextInstanceKey != null; }