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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activities
{
using System.Activities;
using System.Activities.Validation;
using System.Collections;
using System.Collections.Generic;
using System.Runtime;
using System.Runtime.Collections;
using System.ServiceModel.Description;
using System.Windows.Markup;
[ContentProperty("Parameters")]
public sealed class SendParametersContent : SendContent
{
string[] argumentNames;
Type[] argumentTypes;
public SendParametersContent()
: base()
{
this.Parameters = new OrderedDictionary<string, InArgument>();
}
public SendParametersContent(IDictionary<string, InArgument> parameters)
: base()
{
if (parameters == null)
{
throw FxTrace.Exception.ArgumentNull("parameters");
}
this.Parameters = new OrderedDictionary<string, InArgument>(parameters);
}
public IDictionary<string, InArgument> Parameters
{
get;
private set;
}
internal string[] ArgumentNames
{
get
{
if (this.argumentNames == null)
{
ShredParameters();
}
return this.argumentNames;
}
}
internal Type[] ArgumentTypes
{
get
{
if (this.argumentTypes == null)
{
ShredParameters();
}
return this.argumentTypes;
}
}
internal override bool IsFault
{
get
{
if (this.ArgumentTypes.Length == 1)
{
return ContractInferenceHelper.ExceptionType.IsAssignableFrom(this.ArgumentTypes[0]);
}
else
{
return false;
}
}
}
void ShredParameters()
{
// Turn Dictionary into ordered Argument arrays
int argumentCount = this.Parameters.Count;
this.argumentNames = new string[argumentCount];
this.argumentTypes = new Type[argumentCount];
int index = 0;
foreach (KeyValuePair<string, InArgument> pair in this.Parameters)
{
this.argumentNames[index] = pair.Key;
if (pair.Value != null)
{
this.argumentTypes[index] = pair.Value.ArgumentType;
}
index++;
}
}
internal override void CacheMetadata(ActivityMetadata metadata, Activity owner, string operationName)
{
// force a shred for every CacheMetadata call
ShredParameters();
int index = 0;
foreach (Type argumentType in this.argumentTypes)
{
if (argumentType == null || argumentType == TypeHelper.VoidType)
{
metadata.AddValidationError(SR.ArgumentCannotHaveNullOrVoidType(owner.DisplayName, argumentNames[index]));
}
if (argumentType == MessageDescription.TypeOfUntypedMessage || MessageBuilder.IsMessageContract(argumentType))
{
metadata.AddValidationError(SR.SendParametersContentDoesNotSupportMessage(owner.DisplayName, argumentNames[index]));
}
index++;
}
if (!metadata.HasViolations)
{
foreach (KeyValuePair<string, InArgument> pair in this.Parameters)
{
RuntimeArgument newRuntimeArgument = new RuntimeArgument(pair.Key, pair.Value.ArgumentType, ArgumentDirection.In);
metadata.Bind(pair.Value, newRuntimeArgument);
metadata.AddArgument(newRuntimeArgument);
}
}
}
internal override void ConfigureInternalSend(InternalSendMessage internalSendMessage, out ToRequest requestFormatter)
{
//Zero or more arguments
requestFormatter = new ToRequest();
foreach (KeyValuePair<string, InArgument> parameter in this.Parameters)
{
requestFormatter.Parameters.Add(InArgument.CreateReference(parameter.Value, parameter.Key));
}
}
internal override void ConfigureInternalSendReply(InternalSendMessage internalSendMessage, out ToReply responseFormatter)
{
responseFormatter = new ToReply();
foreach (KeyValuePair<string, InArgument> parameter in this.Parameters)
{
responseFormatter.Parameters.Add(InArgument.CreateReference(parameter.Value, parameter.Key));
}
}
internal override void InferMessageDescription(OperationDescription operation, object owner, MessageDirection direction)
{
ContractInferenceHelper.CheckForDisposableParameters(operation, this.ArgumentTypes);
string overridingAction = owner is Send ? ((Send)owner).Action : ((SendReply)owner).Action;
if (direction == MessageDirection.Input)
{
ContractInferenceHelper.AddInputMessage(operation, overridingAction, this.ArgumentNames, this.ArgumentTypes);
}
else
{
ContractInferenceHelper.AddOutputMessage(operation, overridingAction, this.ArgumentNames, this.ArgumentTypes);
}
}
internal override void ValidateContract(NativeActivityContext context, OperationDescription targetOperation, object owner, MessageDirection direction)
{
MessageDescription targetMessage;
string overridingAction;
bool isResponse;
if (direction == MessageDirection.Input)
{
Fx.Assert(targetOperation.Messages.Count >= 1, "There must be at least one MessageDescription in an OperationDescription!");
targetMessage = targetOperation.Messages[0];
Fx.Assert(owner is Send, "The parent of a SendParametersContent with in-message can only be Send!");
overridingAction = ((Send)owner).Action;
isResponse = false;
}
else
{
Fx.Assert(targetOperation.Messages.Count == 2, "There must be exactly two MessageDescription objects for a two-way operation!");
targetMessage = targetOperation.Messages[1];
Fx.Assert(owner is SendReply, "The parent of a SendParametersContent with out-message can only be SendReply!");
overridingAction = ((SendReply)owner).Action;
isResponse = true;
}
if (!this.IsFault)
{
ContractValidationHelper.ValidateAction(context, targetMessage, overridingAction, targetOperation, isResponse);
if (ContractValidationHelper.IsSendParameterContent(targetOperation))
{
ContractValidationHelper.ValidateParametersContent(context, targetMessage, (IDictionary)this.Parameters, targetOperation, isResponse);
}
else
{
Constraint.AddValidationError(context, new ValidationError(SR.MisuseOfParameterContent(targetOperation.Name, targetOperation.DeclaringContract.Name)));
}
}
else
{
Fx.Assert(this.argumentTypes != null && this.argumentTypes.Length == 1, "Exception should be the only parameter in SendFault!");
Type argumentType = this.argumentTypes[0];
if (argumentType.IsGenericType && argumentType.GetGenericTypeDefinition() == ContractInferenceHelper.FaultExceptionType)
{
Type faultType = argumentType.GetGenericArguments()[0];
ContractValidationHelper.ValidateFault(context, targetOperation, overridingAction, faultType);
}
}
}
}
}
|