1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activities
{
using System.Activities;
using System.Activities.Statements;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Windows.Markup;
// Used to receive the reply in a request/reply messaging pattern. Must be paired with
// a corresponding Send activity.
[ContentProperty("Content")]
public sealed class ReceiveReply : Activity
{
Collection<CorrelationInitializer> correlationInitializers;
FromReply responseFormatter;
InternalReceiveMessage internalReceive;
public ReceiveReply()
: base()
{
base.Implementation = () =>
{
// if CacheMetadata isn't called, bail early
if (this.internalReceive == null)
{
return null;
}
// requestFormatter is null if we have an untyped message situation
if (this.responseFormatter == null)
{
return this.internalReceive;
}
else
{
Variable<Message> response = new Variable<Message> { Name = "ResponseMessage" };
this.internalReceive.Message = new OutArgument<Message>(response);
this.responseFormatter.Message = new InArgument<Message>(response);
return new MessagingNoPersistScope
{
Body = new Sequence
{
Variables = { response },
Activities =
{
this.internalReceive,
this.responseFormatter,
}
}
};
}
};
}
// the content to receive (either message or parameters-based) declared by the user
[DefaultValue(null)]
public ReceiveContent Content
{
get;
set;
}
// Internally, we should always use InternalContent since this property may have default content that we added
internal ReceiveContent InternalContent
{
get
{
return this.Content ?? ReceiveContent.DefaultReceiveContent;
}
}
[DefaultValue(null)]
public string Action
{
get;
set;
}
// Reference to the Send activity that is responsible for sending the Request part of the
// request/reply pattern. This cannot be null.
[DefaultValue(null)]
public Send Request
{
get;
set;
}
// Additional correlations allow situations where a "session" involves multiple
// messages between two workflow instances.
public Collection<CorrelationInitializer> CorrelationInitializers
{
get
{
if (this.correlationInitializers == null)
{
this.correlationInitializers = new Collection<CorrelationInitializer>();
}
return this.correlationInitializers;
}
}
protected override void CacheMetadata(ActivityMetadata metadata)
{
if (this.Request == null)
{
metadata.AddValidationError(SR.ReceiveReplyRequestCannotBeNull(this.DisplayName));
}
else
{
// Need to validate Send.ServiceContractName and Send.OperationName here so that we can proceed with contract inference
if (this.Request.ServiceContractName == null)
{
string errorOperationName = ContractValidationHelper.GetErrorMessageOperationName(this.Request.OperationName);
metadata.AddValidationError(SR.MissingServiceContractName(this.Request.DisplayName, errorOperationName));
}
if (string.IsNullOrEmpty(this.Request.OperationName))
{
metadata.AddValidationError(SR.MissingOperationName(this.Request.DisplayName));
}
}
// validate Correlation Initializers
MessagingActivityHelper.ValidateCorrelationInitializer(metadata, this.correlationInitializers, true, this.DisplayName, (this.Request != null ? this.Request.OperationName : String.Empty));
// Validate Content
string operationName = this.Request != null ? this.Request.OperationName : null;
this.InternalContent.CacheMetadata(metadata, this, operationName);
if (this.correlationInitializers != null)
{
for (int i = 0; i < this.correlationInitializers.Count; i++)
{
CorrelationInitializer initializer = this.correlationInitializers[i];
initializer.ArgumentName = Constants.Parameter + i;
RuntimeArgument initializerArgument = new RuntimeArgument(initializer.ArgumentName, Constants.CorrelationHandleType, ArgumentDirection.In);
metadata.Bind(initializer.CorrelationHandle, initializerArgument);
metadata.AddArgument(initializerArgument);
}
}
if (!metadata.HasViolations)
{
this.internalReceive = CreateInternalReceive();
InArgument<CorrelationHandle> requestReplyHandleFromSend = GetReplyHandleFromSend();
if (requestReplyHandleFromSend != null)
{
InArgument<CorrelationHandle> resultCorrelatesWith = MessagingActivityHelper.CreateReplyCorrelatesWith(requestReplyHandleFromSend);
RuntimeArgument resultCorrelatesWithArgument = new RuntimeArgument("ResultCorrelatesWith", Constants.CorrelationHandleType, ArgumentDirection.In);
metadata.Bind(resultCorrelatesWith, resultCorrelatesWithArgument);
metadata.AddArgument(resultCorrelatesWithArgument);
this.internalReceive.CorrelatesWith = (InArgument<CorrelationHandle>)InArgument.CreateReference(resultCorrelatesWith, "ResultCorrelatesWith");
}
this.InternalContent.ConfigureInternalReceiveReply(this.internalReceive, out this.responseFormatter);
if (this.InternalContent is ReceiveMessageContent
&& MessageBuilder.IsMessageContract(((ReceiveMessageContent)this.InternalContent).InternalDeclaredMessageType))
{
this.Request.OperationUsesMessageContract = true;
}
OperationDescription operation = ContractInferenceHelper.CreateTwoWayOperationDescription(this.Request, this);
this.Request.OperationDescription = operation;
if (this.responseFormatter != null)
{
IClientMessageFormatter formatter = ClientOperationFormatterProvider.GetFormatterFromRuntime(operation);
this.Request.SetFormatter(formatter);
this.responseFormatter.Formatter = formatter;
//
int index = 0;
Type[] faultTypes = new Type[operation.KnownTypes.Count];
foreach (Type type in operation.KnownTypes)
{
faultTypes[index] = type;
index++;
}
this.responseFormatter.FaultFormatter = new FaultFormatter(faultTypes);
}
// Add CorrelationQuery to the Send->ReplyCorrelation, we validate that the same query is not added multiple times
if (this.correlationInitializers != null && this.correlationInitializers.Count > 0)
{
Collection<CorrelationQuery> internalCorrelationQueryCollection = ContractInferenceHelper.CreateClientCorrelationQueries(null, this.correlationInitializers,
this.Action, this.Request.ServiceContractName, this.Request.OperationName, true);
foreach (CorrelationQuery query in internalCorrelationQueryCollection)
{
this.Request.SetReplyCorrelationQuery(query);
}
}
}
else
{
this.internalReceive = null;
this.responseFormatter = null;
}
// We don't have any imported children despite referencing the Request
metadata.SetImportedChildrenCollection(new Collection<Activity>());
}
InternalReceiveMessage CreateInternalReceive()
{
InternalReceiveMessage result = new InternalReceiveMessage
{
IsOneWay = false,
IsReceiveReply = true,
OwnerDisplayName = this.DisplayName
};
if (this.correlationInitializers != null)
{
foreach (CorrelationInitializer correlation in this.correlationInitializers)
{
result.CorrelationInitializers.Add(correlation.Clone());
}
}
// Update the send->IsOneWay
if (this.Request != null)
{
this.Request.SetIsOneWay(false);
}
return result;
}
InArgument<CorrelationHandle> GetReplyHandleFromSend()
{
if (this.Request != null)
{
// If user has set AdditionalCorrelations, then we need to first look for requestReply Handle there
foreach (CorrelationInitializer correlation in this.Request.CorrelationInitializers)
{
RequestReplyCorrelationInitializer requestReplyCorrelation = correlation as RequestReplyCorrelationInitializer;
if (requestReplyCorrelation != null && requestReplyCorrelation.CorrelationHandle != null)
{
return requestReplyCorrelation.CorrelationHandle;
}
}
}
return null;
}
}
}
|