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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activities
{
using System.Activities;
using System.Activities.Statements;
using System.Activities.Validation;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Windows.Markup;
using System.Xaml;
using System.Xml.Linq;
// Used to send the reply in a request/reply messaging pattern. Must be paired with
// a corresponding Receive activity.
[ContentProperty("Content")]
public sealed class SendReply : Activity
{
Collection<CorrelationInitializer> correlationInitializers;
ToReply responseFormatter;
InternalSendMessage internalSend;
public SendReply()
: base()
{
base.Implementation = () =>
{
// if CacheMetadata isn't called, bail early
if (this.internalSend == null)
{
return null;
}
if (this.responseFormatter == null) // untyped message
{
return this.internalSend;
}
else
{
Variable<Message> response = new Variable<Message> { Name = "ResponseMessage" };
this.responseFormatter.Message = new OutArgument<Message>(response);
this.internalSend.Message = new InArgument<Message>(response);
// This is used to clear out the response variable
this.internalSend.MessageOut = new OutArgument<Message>(response);
return new Sequence
{
Variables = { response },
Activities =
{
this.responseFormatter,
this.internalSend
}
};
}
};
}
// the content to send (either message or parameters-based) declared by the user
[DefaultValue(null)]
public SendContent Content
{
get;
set;
}
// Internally, we should always use InternalContent since this property may have default content that we added
internal SendContent InternalContent
{
get
{
return this.Content ?? SendContent.DefaultSendContent;
}
}
// Reference to the Receive activity that is responsible for receiving the Request part of the
// request/reply pattern. This cannot be null.
[DefaultValue(null)]
public Receive Request
{
get;
set;
}
[DefaultValue(null)]
public string Action
{
get;
set;
}
// Additional correlations allow situations where a "session" involves multiple
// messages between two workflow instances.
[DefaultValue(null)]
public Collection<CorrelationInitializer> CorrelationInitializers
{
get
{
if (this.correlationInitializers == null)
{
this.correlationInitializers = new Collection<CorrelationInitializer>();
}
return this.correlationInitializers;
}
}
// used to ensure that the other side gets ALO behavior
[DefaultValue(false)]
public bool PersistBeforeSend
{
get;
set;
}
protected override void CacheMetadata(ActivityMetadata metadata)
{
if (this.Request == null)
{
metadata.AddValidationError(SR.SendReplyRequestCannotBeNull(this.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.internalSend = CreateInternalSend();
this.InternalContent.ConfigureInternalSendReply(this.internalSend, out this.responseFormatter);
InArgument<CorrelationHandle> requestReplyHandleFromReceive = GetReplyHandleFromReceive();
if (requestReplyHandleFromReceive != null)
{
InArgument<CorrelationHandle> internalSendCorrelatesWith = MessagingActivityHelper.CreateReplyCorrelatesWith(requestReplyHandleFromReceive);
RuntimeArgument internalSendCorrelatesWithArgument = new RuntimeArgument("InternalSendCorrelatesWith", Constants.CorrelationHandleType, ArgumentDirection.In);
metadata.Bind(internalSendCorrelatesWith, internalSendCorrelatesWithArgument);
metadata.AddArgument(internalSendCorrelatesWithArgument);
this.internalSend.CorrelatesWith = (InArgument<CorrelationHandle>)InArgument.CreateReference(internalSendCorrelatesWith, "InternalSendCorrelatesWith");
if (this.responseFormatter != null)
{
InArgument<CorrelationHandle> responseFormatterCorrelatesWith = MessagingActivityHelper.CreateReplyCorrelatesWith(requestReplyHandleFromReceive);
RuntimeArgument responseFormatterCorrelatesWithArgument = new RuntimeArgument("ResponseFormatterCorrelatesWith", Constants.CorrelationHandleType, ArgumentDirection.In);
metadata.Bind(responseFormatterCorrelatesWith, responseFormatterCorrelatesWithArgument);
metadata.AddArgument(responseFormatterCorrelatesWithArgument);
responseFormatter.CorrelatesWith = (InArgument<CorrelationHandle>)InArgument.CreateReference(responseFormatterCorrelatesWith, "ResponseFormatterCorrelatesWith");
}
}
}
else
{
this.internalSend = null;
this.responseFormatter = null;
}
// We don't have any imported children despite referencing the Request
metadata.SetImportedChildrenCollection(new Collection<Activity>());
}
// responseFormatter is null if we have an untyped message situation
InternalSendMessage CreateInternalSend()
{
InternalSendMessage result = new InternalSendMessage
{
IsSendReply = true, //indicates that we are sending a reply(server-side)
ShouldPersistBeforeSend = this.PersistBeforeSend,
OperationName = this.Request.OperationName, //need this for displaying error messages
OwnerDisplayName = this.DisplayName
};
if (this.correlationInitializers != null)
{
foreach (CorrelationInitializer correlation in this.correlationInitializers)
{
result.CorrelationInitializers.Add(correlation.Clone());
}
}
return result;
}
internal void SetFormatter(IDispatchMessageFormatter formatter)
{
if (this.responseFormatter != null)
{
this.responseFormatter.Formatter = formatter;
}
}
internal void SetFaultFormatter(IDispatchFaultFormatter faultFormatter, bool includeExceptionDetailInFaults)
{
Fx.Assert(this.responseFormatter != null, "ToReply cannot be null!");
this.responseFormatter.FaultFormatter = faultFormatter;
this.responseFormatter.IncludeExceptionDetailInFaults = includeExceptionDetailInFaults;
}
internal void SetContractName(XName contractName)
{
Fx.Assert(this.internalSend != null, "InternalSend cannot be null!");
this.internalSend.ServiceContractName = contractName;
}
InArgument<CorrelationHandle> GetReplyHandleFromReceive()
{
if (this.Request != null)
{
//if the 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;
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters,
Justification = "This is the design for this public interface, we want to distinguish between SendReply for faults and SendReply for Return parameter therefore need the second output")]
public static SendReply FromOperationDescription(OperationDescription operation, out IEnumerable<SendReply> faultReplies)
{
if (operation == null)
{
throw FxTrace.Exception.ArgumentNull("operation", "OperationDescription cannot be null");
}
bool contentIsParameter = false;
bool contentIsMessage = false;
bool isSendContentEmpty = false;
MessageDescription message;
faultReplies = null;
List<SendReply> faultRepliesList = new List<SendReply>();
SendReply reply = null;
if (operation.IsOneWay)
{
return null;
}
if (operation.Messages.Count > 1)
{
reply = new SendReply();
reply.Action = operation.Messages[1].Action;
reply.DisplayName = operation.Name + "SendReply";
message = operation.Messages[1];
contentIsParameter = false;
if (message.MessageType == null)
{
if (message.Body.ReturnValue != null && message.Body.ReturnValue.Type != typeof(void))
{
if (!message.Body.ReturnValue.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
{
contentIsParameter = true;
}
isSendContentEmpty = true;
}
}
if (message.MessageType == null)
{
if (message.Body.Parts != null)
{
if (message.Body.Parts.Count > 0)
{
MessagePartDescriptionCollection parts = message.Body.Parts;
foreach (MessagePartDescription messagePart in parts)
{
if (messagePart.Index >= 0)
{
contentIsParameter = true;
break;
}
if (!messagePart.Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
{
contentIsParameter = true;
}
}
isSendContentEmpty = true;
}
}
}
if (isSendContentEmpty)
{
if (contentIsParameter)
{
SendParametersContent content = new SendParametersContent();
if (message.Direction == MessageDirection.Output
&& message.Body.ReturnValue != null
&& message.Body.ReturnValue.Type != typeof(void))
{
Argument returnArgument = InArgument.Create(message.Body.ReturnValue.Type, ArgumentDirection.In);
content.Parameters.Add(message.Body.ReturnValue.Name, (InArgument)returnArgument);
}
if (message.Direction == MessageDirection.Output && message.Body.Parts != null)
{
foreach (MessagePartDescription messagePart in message.Body.Parts)
{
Argument inArgument = InArgument.Create(messagePart.Type, ArgumentDirection.In);
content.Parameters.Add(messagePart.Name, (InArgument)(inArgument));
}
}
contentIsMessage = false;
reply.Content = content;
}
else
{
// We must have an untyped message contract
//
SendMessageContent content = new SendMessageContent();
if (message.Direction == MessageDirection.Output)
{
content.DeclaredMessageType = message.Body.ReturnValue.Type;
Argument inArgument = InArgument.Create(content.DeclaredMessageType, ArgumentDirection.In);
content.Message = (InArgument)(inArgument);
}
contentIsMessage = true;
reply.Content = content;
}
}
else
{
if (message.MessageType != null && message.MessageType.IsDefined(typeof(MessageContractAttribute), false))
{
SendMessageContent sendMessageContent;
sendMessageContent = new SendMessageContent();
sendMessageContent.DeclaredMessageType = message.MessageType;
Argument inArgument = InArgument.Create(sendMessageContent.DeclaredMessageType, ArgumentDirection.In);
sendMessageContent.Message = (InArgument)(inArgument);
reply.Content = sendMessageContent;
contentIsMessage = true;
}
else if (operation.Messages[0].MessageType != null)
{
reply.Content = new SendMessageContent();
contentIsMessage = true;
}
else if (operation.Messages[0].Body.Parts != null
&& operation.Messages[0].Body.Parts.Count == 1
&& operation.Messages[0].Body.Parts[0].Type.IsAssignableFrom(typeof(System.ServiceModel.Channels.Message)))
{
reply.Content = new SendMessageContent();
contentIsMessage = true;
}
else
{
reply.Content = new SendParametersContent();
contentIsMessage = false;
}
}
}
if (operation.Faults != null)
{
foreach (FaultDescription faultDescription in operation.Faults)
{
faultRepliesList.Add(BuildFaultReplies(faultDescription, contentIsMessage));
}
}
faultReplies = faultRepliesList;
return reply;
}
static SendReply BuildFaultReplies(FaultDescription faultDescription, bool isMessageContract)
{
Fx.Assert(faultDescription != null, "fault Description cannot be null");
if (faultDescription.DetailType == TypeHelper.VoidType || faultDescription.DetailType == null)
{
throw FxTrace.Exception.ArgumentNullOrEmpty("FaultDescription.DetailType");
}
SendReply faultReply = new SendReply()
{
DisplayName = faultDescription.Name + "SendFaultReply",
Action = faultDescription.Action,
};
Type[] substitute = { faultDescription.DetailType };
Type faultType = typeof(FaultException<>).MakeGenericType(substitute);
if (isMessageContract)
{
faultReply.Content = new SendMessageContent()
{
Message = (InArgument)(InArgument.Create(faultType, ArgumentDirection.In)),
};
}
else
{
InArgument argument = (InArgument)(InArgument.Create(faultType, ArgumentDirection.In));
SendParametersContent faultReplyParameterContent = new SendParametersContent();
faultReplyParameterContent.Parameters.Add(faultDescription.Name, argument);
faultReply.Content = faultReplyParameterContent;
}
return faultReply;
}
}
}
|