| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using System.Diagnostics.Contracts; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Dispatcher |
| | | 13 | | { |
| | | 14 | | internal class ProxyOperationRuntime |
| | | 15 | | { |
| | 0 | 16 | | internal static readonly ParameterInfo[] NoParams = Array.Empty<ParameterInfo>(); |
| | 0 | 17 | | internal static readonly object[] EmptyArray = Array.Empty<object>(); |
| | | 18 | | private readonly IClientMessageFormatter _formatter; |
| | | 19 | | private readonly IParameterInspector[] _parameterInspectors; |
| | | 20 | | private readonly MethodInfo _beginMethod; |
| | | 21 | | private readonly MethodInfo _syncMethod; |
| | | 22 | | private readonly MethodInfo _taskMethod; |
| | | 23 | | private readonly ParameterInfo[] _inParams; |
| | | 24 | | private readonly ParameterInfo[] _outParams; |
| | | 25 | | private readonly ParameterInfo[] _endOutParams; |
| | | 26 | | private readonly ParameterInfo _returnParam; |
| | | 27 | | |
| | 2 | 28 | | internal ProxyOperationRuntime(ClientOperation operation, ImmutableClientRuntime parent) |
| | | 29 | | { |
| | 2 | 30 | | if (operation == null) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation)); |
| | | 33 | | } |
| | | 34 | | |
| | 2 | 35 | | Parent = parent ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parent)); |
| | 2 | 36 | | _formatter = operation.Formatter; |
| | 2 | 37 | | IsInitiating = operation.IsInitiating; |
| | 2 | 38 | | IsOneWay = operation.IsOneWay; |
| | 2 | 39 | | IsTerminating = operation.IsTerminating; |
| | 2 | 40 | | IsSessionOpenNotificationEnabled = operation.IsSessionOpenNotificationEnabled; |
| | 2 | 41 | | Name = operation.Name; |
| | 2 | 42 | | _parameterInspectors = EmptyArray<IParameterInspector>.ToArray(operation.ParameterInspectors); |
| | 2 | 43 | | FaultFormatter = operation.FaultFormatter; |
| | 2 | 44 | | SerializeRequest = operation.SerializeRequest; |
| | 2 | 45 | | DeserializeReply = operation.DeserializeReply; |
| | 2 | 46 | | Action = operation.Action; |
| | 2 | 47 | | ReplyAction = operation.ReplyAction; |
| | 2 | 48 | | _beginMethod = operation.BeginMethod; |
| | 2 | 49 | | _syncMethod = operation.SyncMethod; |
| | 2 | 50 | | _taskMethod = operation.TaskMethod; |
| | 2 | 51 | | TaskTResult = operation.TaskTResult; |
| | | 52 | | |
| | 2 | 53 | | if (_beginMethod != null) |
| | | 54 | | { |
| | 0 | 55 | | _inParams = ServiceReflector.GetInputParameters(_beginMethod, true); |
| | 0 | 56 | | if (_syncMethod != null) |
| | | 57 | | { |
| | 0 | 58 | | _outParams = ServiceReflector.GetOutputParameters(_syncMethod, false); |
| | | 59 | | } |
| | | 60 | | else |
| | | 61 | | { |
| | 0 | 62 | | _outParams = NoParams; |
| | | 63 | | } |
| | 0 | 64 | | _endOutParams = ServiceReflector.GetOutputParameters(operation.EndMethod, true); |
| | 0 | 65 | | _returnParam = operation.EndMethod.ReturnParameter; |
| | | 66 | | } |
| | 2 | 67 | | else if (_syncMethod != null) |
| | | 68 | | { |
| | 1 | 69 | | _inParams = ServiceReflector.GetInputParameters(_syncMethod, false); |
| | 1 | 70 | | _outParams = ServiceReflector.GetOutputParameters(_syncMethod, false); |
| | 1 | 71 | | _returnParam = _syncMethod.ReturnParameter; |
| | | 72 | | } |
| | | 73 | | |
| | 2 | 74 | | if (_formatter == null && (SerializeRequest || DeserializeReply)) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Cli |
| | | 77 | | } |
| | 2 | 78 | | } |
| | | 79 | | |
| | 1 | 80 | | internal string Action { get; } |
| | | 81 | | |
| | 0 | 82 | | internal IClientFaultFormatter FaultFormatter { get; } |
| | | 83 | | |
| | 0 | 84 | | internal bool IsInitiating { get; } |
| | | 85 | | |
| | 2 | 86 | | internal bool IsOneWay { get; } |
| | | 87 | | |
| | 1 | 88 | | internal bool IsTerminating { get; } |
| | | 89 | | |
| | 1 | 90 | | internal bool IsSessionOpenNotificationEnabled { get; } |
| | | 91 | | |
| | 0 | 92 | | internal string Name { get; } |
| | | 93 | | |
| | 6 | 94 | | internal ImmutableClientRuntime Parent { get; } |
| | | 95 | | |
| | 2 | 96 | | internal string ReplyAction { get; } |
| | | 97 | | |
| | 2 | 98 | | internal bool DeserializeReply { get; } |
| | | 99 | | |
| | 2 | 100 | | internal bool SerializeRequest { get; } |
| | | 101 | | |
| | | 102 | | internal Type TaskTResult |
| | | 103 | | { |
| | 0 | 104 | | get; |
| | 2 | 105 | | set; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal void AfterReply(ref ProxyRpc rpc) |
| | | 109 | | { |
| | 1 | 110 | | if (!IsOneWay) |
| | | 111 | | { |
| | 1 | 112 | | Message reply = rpc.Reply; |
| | | 113 | | |
| | 1 | 114 | | if (DeserializeReply) |
| | | 115 | | { |
| | | 116 | | //if (TD.ClientFormatterDeserializeReplyStartIsEnabled()) |
| | | 117 | | //{ |
| | | 118 | | // TD.ClientFormatterDeserializeReplyStart(rpc.EventTraceActivity); |
| | | 119 | | //} |
| | | 120 | | |
| | 1 | 121 | | rpc.ReturnValue = _formatter.DeserializeReply(reply, rpc.OutputParameters); |
| | | 122 | | |
| | | 123 | | //if (TD.ClientFormatterDeserializeReplyStopIsEnabled()) |
| | | 124 | | //{ |
| | | 125 | | // TD.ClientFormatterDeserializeReplyStop(rpc.EventTraceActivity); |
| | | 126 | | //} |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | 0 | 130 | | rpc.ReturnValue = reply; |
| | | 131 | | } |
| | | 132 | | |
| | 1 | 133 | | int offset = Parent.ParameterInspectorCorrelationOffset; |
| | | 134 | | try |
| | | 135 | | { |
| | 2 | 136 | | for (int i = _parameterInspectors.Length - 1; i >= 0; i--) |
| | | 137 | | { |
| | 0 | 138 | | _parameterInspectors[i].AfterCall(Name, |
| | 0 | 139 | | rpc.OutputParameters, |
| | 0 | 140 | | rpc.ReturnValue, |
| | 0 | 141 | | rpc.Correlation[offset + i]); |
| | | 142 | | //if (TD.ClientParameterInspectorAfterCallInvokedIsEnabled()) |
| | | 143 | | //{ |
| | | 144 | | // TD.ClientParameterInspectorAfterCallInvoked(rpc.EventTraceActivity, this._parameterInspect |
| | | 145 | | //} |
| | | 146 | | } |
| | 1 | 147 | | } |
| | 0 | 148 | | catch (Exception e) |
| | | 149 | | { |
| | 0 | 150 | | if (Fx.IsFatal(e)) |
| | | 151 | | { |
| | 0 | 152 | | throw; |
| | | 153 | | } |
| | 0 | 154 | | if (ErrorBehavior.ShouldRethrowClientSideExceptionAsIs(e)) |
| | | 155 | | { |
| | 0 | 156 | | throw; |
| | | 157 | | } |
| | 0 | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 159 | | } |
| | | 160 | | |
| | 1 | 161 | | if (Parent.ValidateMustUnderstand) |
| | | 162 | | { |
| | 1 | 163 | | Collection<MessageHeaderInfo> headersNotUnderstood = reply.Headers.GetHeadersNotUnderstood(); |
| | 1 | 164 | | if (headersNotUnderstood != null && headersNotUnderstood.Count > 0) |
| | | 165 | | { |
| | 0 | 166 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.Format(SR.SFx |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | } |
| | 1 | 170 | | } |
| | | 171 | | |
| | | 172 | | internal void BeforeRequest(ref ProxyRpc rpc) |
| | | 173 | | { |
| | 1 | 174 | | int offset = Parent.ParameterInspectorCorrelationOffset; |
| | | 175 | | try |
| | | 176 | | { |
| | 2 | 177 | | for (int i = 0; i < _parameterInspectors.Length; i++) |
| | | 178 | | { |
| | 0 | 179 | | rpc.Correlation[offset + i] = _parameterInspectors[i].BeforeCall(Name, rpc.InputParameters); |
| | | 180 | | //if (TD.ClientParameterInspectorBeforeCallInvokedIsEnabled()) |
| | | 181 | | //{ |
| | | 182 | | // TD.ClientParameterInspectorBeforeCallInvoked(rpc.EventTraceActivity, this._parameterInspectors |
| | | 183 | | //} |
| | | 184 | | } |
| | 1 | 185 | | } |
| | 0 | 186 | | catch (Exception e) |
| | | 187 | | { |
| | 0 | 188 | | if (Fx.IsFatal(e)) |
| | | 189 | | { |
| | 0 | 190 | | throw; |
| | | 191 | | } |
| | 0 | 192 | | if (ErrorBehavior.ShouldRethrowClientSideExceptionAsIs(e)) |
| | | 193 | | { |
| | 0 | 194 | | throw; |
| | | 195 | | } |
| | 0 | 196 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 197 | | } |
| | | 198 | | |
| | 1 | 199 | | if (SerializeRequest) |
| | | 200 | | { |
| | | 201 | | //if (TD.ClientFormatterSerializeRequestStartIsEnabled()) |
| | | 202 | | //{ |
| | | 203 | | // TD.ClientFormatterSerializeRequestStart(rpc.EventTraceActivity); |
| | | 204 | | //} |
| | | 205 | | |
| | 1 | 206 | | rpc.Request = _formatter.SerializeRequest(rpc.MessageVersion, rpc.InputParameters); |
| | | 207 | | |
| | | 208 | | //if (TD.ClientFormatterSerializeRequestStopIsEnabled()) |
| | | 209 | | //{ |
| | | 210 | | // TD.ClientFormatterSerializeRequestStop(rpc.EventTraceActivity); |
| | | 211 | | //} |
| | | 212 | | } |
| | | 213 | | else |
| | | 214 | | { |
| | 0 | 215 | | if (rpc.InputParameters[0] == null) |
| | | 216 | | { |
| | 0 | 217 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | rpc.Request = (Message)rpc.InputParameters[0]; |
| | 0 | 221 | | if (!IsValidAction(rpc.Request, Action)) |
| | | 222 | | { |
| | 0 | 223 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 224 | | } |
| | | 225 | | } |
| | 0 | 226 | | } |
| | | 227 | | |
| | | 228 | | internal static object GetDefaultParameterValue(Type parameterType) |
| | | 229 | | { |
| | 1 | 230 | | return (parameterType.GetTypeInfo().IsValueType && parameterType != typeof(void)) ? Activator.CreateInstance |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | internal bool IsSyncCall(MethodCall methodCall) |
| | | 234 | | { |
| | 1 | 235 | | if (_syncMethod == null) |
| | | 236 | | { |
| | 0 | 237 | | return false; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | Contract.Assert(methodCall != null); |
| | | 241 | | Contract.Assert(methodCall.MethodBase != null); |
| | 1 | 242 | | return methodCall.MethodBase.Equals(_syncMethod); |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | internal bool IsBeginCall(MethodCall methodCall) |
| | | 246 | | { |
| | 0 | 247 | | if (_beginMethod == null) |
| | | 248 | | { |
| | 0 | 249 | | return false; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | Contract.Assert(methodCall != null); |
| | | 253 | | Contract.Assert(methodCall.MethodBase != null); |
| | 0 | 254 | | return methodCall.MethodBase.Equals(_beginMethod); |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | internal bool IsTaskCall(MethodCall methodCall) |
| | | 258 | | { |
| | 1 | 259 | | if (_taskMethod == null) |
| | | 260 | | { |
| | 1 | 261 | | return false; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | Contract.Assert(methodCall != null); |
| | | 265 | | Contract.Assert(methodCall.MethodBase != null); |
| | 0 | 266 | | return methodCall.MethodBase.Equals(_taskMethod); |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | internal object[] MapSyncInputs(MethodCall methodCall, out object[] outs) |
| | | 270 | | { |
| | 1 | 271 | | if (_outParams.Length == 0) |
| | | 272 | | { |
| | 1 | 273 | | outs = Array.Empty<object>(); |
| | | 274 | | } |
| | | 275 | | else |
| | | 276 | | { |
| | 0 | 277 | | outs = new object[_outParams.Length]; |
| | | 278 | | } |
| | 1 | 279 | | if (_inParams.Length == 0) |
| | | 280 | | { |
| | 0 | 281 | | return Array.Empty<object>(); |
| | | 282 | | } |
| | | 283 | | |
| | 1 | 284 | | return methodCall.Args; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | internal object[] MapAsyncBeginInputs(MethodCall methodCall, out AsyncCallback callback, out object asyncState) |
| | | 288 | | { |
| | | 289 | | object[] ins; |
| | 0 | 290 | | if (_inParams.Length == 0) |
| | | 291 | | { |
| | 0 | 292 | | ins = Array.Empty<object>(); |
| | | 293 | | } |
| | | 294 | | else |
| | | 295 | | { |
| | 0 | 296 | | ins = new object[_inParams.Length]; |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | object[] args = methodCall.Args; |
| | 0 | 300 | | for (int i = 0; i < ins.Length; i++) |
| | | 301 | | { |
| | 0 | 302 | | ins[i] = args[_inParams[i].Position]; |
| | | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | callback = args[methodCall.Args.Length - 2] as AsyncCallback; |
| | 0 | 306 | | asyncState = args[methodCall.Args.Length - 1]; |
| | 0 | 307 | | return ins; |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | internal void MapAsyncEndInputs(MethodCall methodCall, out IAsyncResult result, out object[] outs) |
| | | 311 | | { |
| | 0 | 312 | | outs = new object[_endOutParams.Length]; |
| | 0 | 313 | | result = methodCall.Args[methodCall.Args.Length - 1] as IAsyncResult; |
| | 0 | 314 | | } |
| | | 315 | | |
| | | 316 | | internal object[] MapSyncOutputs(MethodCall methodCall, object[] outs, ref object ret) |
| | | 317 | | { |
| | 1 | 318 | | return MapOutputs(_outParams, methodCall, outs, ref ret); |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | internal object[] MapAsyncOutputs(MethodCall methodCall, object[] outs, ref object ret) |
| | | 322 | | { |
| | 0 | 323 | | return MapOutputs(_endOutParams, methodCall, outs, ref ret); |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | private object[] MapOutputs(ParameterInfo[] parameters, MethodCall methodCall, object[] outs, ref object ret) |
| | | 327 | | { |
| | 1 | 328 | | if (ret == null && _returnParam != null) |
| | | 329 | | { |
| | 1 | 330 | | ret = GetDefaultParameterValue(TypeLoader.GetParameterType(_returnParam)); |
| | | 331 | | } |
| | | 332 | | |
| | 1 | 333 | | if (parameters.Length == 0) |
| | | 334 | | { |
| | 1 | 335 | | return null; |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | object[] args = methodCall.Args; |
| | 0 | 339 | | for (int i = 0; i < parameters.Length; i++) |
| | | 340 | | { |
| | 0 | 341 | | if (outs[i] == null) |
| | | 342 | | { |
| | | 343 | | // the RealProxy infrastructure requires a default value for value types |
| | 0 | 344 | | args[parameters[i].Position] = GetDefaultParameterValue(TypeLoader.GetParameterType(parameters[i])); |
| | | 345 | | } |
| | | 346 | | else |
| | | 347 | | { |
| | 0 | 348 | | args[parameters[i].Position] = outs[i]; |
| | | 349 | | } |
| | | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | return args; |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | internal static bool IsValidAction(Message message, string action) |
| | | 356 | | { |
| | 10 | 357 | | if (message == null) |
| | | 358 | | { |
| | 0 | 359 | | return false; |
| | | 360 | | } |
| | | 361 | | |
| | 10 | 362 | | if (message.IsFault) |
| | | 363 | | { |
| | 8 | 364 | | return true; |
| | | 365 | | } |
| | | 366 | | |
| | 2 | 367 | | if (action == MessageHeaders.WildcardAction) |
| | | 368 | | { |
| | 1 | 369 | | return true; |
| | | 370 | | } |
| | | 371 | | |
| | 1 | 372 | | return (string.CompareOrdinal(message.Headers.Action, action) == 0); |
| | | 373 | | } |
| | | 374 | | } |
| | | 375 | | } |