| | | 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.Generic; |
| | | 6 | | using System.Collections.Specialized; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Diagnostics; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using CoreWCF.Telemetry; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Dispatcher |
| | | 15 | | { |
| | | 16 | | internal class ImmutableDispatchRuntime |
| | | 17 | | { |
| | | 18 | | private readonly AuthenticationBehavior _authenticationBehavior; |
| | | 19 | | private readonly AuthorizationBehavior _authorizationBehavior; |
| | | 20 | | private readonly ConcurrencyBehavior _concurrency; |
| | | 21 | | private readonly IDemuxer _demuxer; |
| | | 22 | | private readonly IInputSessionShutdown[] _inputSessionShutdownHandlers; |
| | | 23 | | private readonly bool _isOnServer; |
| | | 24 | | private readonly IDispatchMessageInspector[] _messageInspectors; |
| | | 25 | | private readonly TerminatingOperationBehavior _terminate; |
| | | 26 | | private readonly ThreadBehavior _thread; |
| | | 27 | | private readonly MessageRpcErrorHandler _processMessageNonCleanupError; |
| | | 28 | | private readonly MessageRpcErrorHandler _processMessageCleanupError; |
| | | 29 | | |
| | | 30 | | private Activity? _activity; |
| | | 31 | | |
| | 664 | 32 | | internal ImmutableDispatchRuntime(DispatchRuntime dispatch) |
| | | 33 | | { |
| | 664 | 34 | | _authenticationBehavior = AuthenticationBehavior.TryCreate(dispatch); |
| | 664 | 35 | | _authorizationBehavior = AuthorizationBehavior.TryCreate(dispatch); |
| | 661 | 36 | | _concurrency = new ConcurrencyBehavior(dispatch); |
| | 661 | 37 | | ErrorBehavior = new ErrorBehavior(dispatch.ChannelDispatcher); |
| | 661 | 38 | | EnableFaults = dispatch.EnableFaults; |
| | 661 | 39 | | _inputSessionShutdownHandlers = EmptyArray<IInputSessionShutdown>.ToArray(dispatch.InputSessionShutdownHandl |
| | 661 | 40 | | InstanceBehavior = new InstanceBehavior(dispatch, this); |
| | 661 | 41 | | _isOnServer = dispatch.IsOnServer; |
| | 661 | 42 | | ManualAddressing = dispatch.ManualAddressing; |
| | 661 | 43 | | _messageInspectors = EmptyArray<IDispatchMessageInspector>.ToArray(dispatch.MessageInspectors); |
| | 661 | 44 | | SecurityImpersonation = SecurityImpersonationBehavior.CreateIfNecessary(dispatch); |
| | 661 | 45 | | RequireClaimsPrincipalOnOperationContext = dispatch.RequireClaimsPrincipalOnOperationContext; |
| | 661 | 46 | | SupportsAuthorizationData = dispatch.SupportsAuthorizationData; |
| | 661 | 47 | | IsImpersonationEnabledOnSerializingReply = dispatch.ImpersonateOnSerializingReply; |
| | 661 | 48 | | _terminate = TerminatingOperationBehavior.CreateIfNecessary(dispatch); |
| | 661 | 49 | | _thread = new ThreadBehavior(dispatch); |
| | 661 | 50 | | ValidateMustUnderstand = dispatch.ValidateMustUnderstand; |
| | 661 | 51 | | ParameterInspectorCorrelationOffset = (dispatch.MessageInspectors.Count + |
| | 661 | 52 | | dispatch.MaxCallContextInitializers); |
| | 661 | 53 | | CorrelationCount = ParameterInspectorCorrelationOffset + dispatch.MaxParameterInspectors; |
| | | 54 | | |
| | 661 | 55 | | DispatchOperationRuntime unhandled = new DispatchOperationRuntime(dispatch.UnhandledDispatchOperation, this) |
| | | 56 | | |
| | 661 | 57 | | if (dispatch.OperationSelector == null) |
| | | 58 | | { |
| | 623 | 59 | | ActionDemuxer demuxer = new ActionDemuxer(); |
| | 6934 | 60 | | for (int i = 0; i < dispatch.Operations.Count; i++) |
| | | 61 | | { |
| | 2844 | 62 | | DispatchOperation operation = dispatch.Operations[i]; |
| | 2844 | 63 | | DispatchOperationRuntime operationRuntime = new DispatchOperationRuntime(operation, this); |
| | 2844 | 64 | | demuxer.Add(operation.Action, operationRuntime); |
| | | 65 | | } |
| | | 66 | | |
| | 623 | 67 | | demuxer.SetUnhandled(unhandled); |
| | 623 | 68 | | _demuxer = demuxer; |
| | | 69 | | } |
| | | 70 | | else |
| | | 71 | | { |
| | 38 | 72 | | CustomDemuxer demuxer = new CustomDemuxer(dispatch.OperationSelector); |
| | 414 | 73 | | for (int i = 0; i < dispatch.Operations.Count; i++) |
| | | 74 | | { |
| | 169 | 75 | | DispatchOperation operation = dispatch.Operations[i]; |
| | 169 | 76 | | DispatchOperationRuntime operationRuntime = new DispatchOperationRuntime(operation, this); |
| | 169 | 77 | | demuxer.Add(operation.Name, operationRuntime); |
| | | 78 | | } |
| | | 79 | | |
| | 38 | 80 | | demuxer.SetUnhandled(unhandled); |
| | 38 | 81 | | _demuxer = demuxer; |
| | | 82 | | } |
| | | 83 | | |
| | 661 | 84 | | _processMessageNonCleanupError = new MessageRpcErrorHandler(ProcessMessageNonCleanupError); |
| | 661 | 85 | | _processMessageCleanupError = new MessageRpcErrorHandler(ProcessMessageCleanupError); |
| | 661 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal int CallContextCorrelationOffset |
| | | 89 | | { |
| | 24 | 90 | | get { return _messageInspectors.Length; } |
| | | 91 | | } |
| | | 92 | | |
| | 2538 | 93 | | internal int CorrelationCount { get; } |
| | | 94 | | |
| | 618 | 95 | | internal bool EnableFaults { get; } |
| | | 96 | | |
| | 8123 | 97 | | internal InstanceBehavior InstanceBehavior { get; } |
| | | 98 | | |
| | 618 | 99 | | internal bool IsImpersonationEnabledOnSerializingReply { get; } |
| | | 100 | | |
| | 5033 | 101 | | internal bool RequireClaimsPrincipalOnOperationContext { get; } |
| | | 102 | | |
| | 142 | 103 | | internal bool SupportsAuthorizationData { get; } |
| | | 104 | | |
| | 2184 | 105 | | internal bool ManualAddressing { get; } |
| | | 106 | | |
| | 661 | 107 | | internal int ParameterInspectorCorrelationOffset { get; } |
| | | 108 | | |
| | | 109 | | // internal IRequestReplyCorrelator RequestReplyCorrelator |
| | | 110 | | // { |
| | | 111 | | // get { return this.requestReplyCorrelator; } |
| | | 112 | | // } |
| | | 113 | | |
| | 4984 | 114 | | internal SecurityImpersonationBehavior SecurityImpersonation { get; } |
| | | 115 | | |
| | 2504 | 116 | | internal bool ValidateMustUnderstand { get; } |
| | | 117 | | |
| | 7614 | 118 | | internal ErrorBehavior ErrorBehavior { get; } |
| | | 119 | | |
| | | 120 | | private Task AcquireDynamicInstanceContextAsync(MessageRpc rpc) |
| | | 121 | | { |
| | 2536 | 122 | | if (rpc.InstanceContext.QuotaThrottle != null) |
| | | 123 | | { |
| | 0 | 124 | | return AcquireDynamicInstanceContextCoreAsync(rpc); |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 2536 | 128 | | return Task.CompletedTask; |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | private Task AcquireDynamicInstanceContextCoreAsync(MessageRpc rpc) |
| | | 133 | | { |
| | 0 | 134 | | return rpc.InstanceContext.QuotaThrottle.AcquireAsync(); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | internal void AfterReceiveRequest(ref MessageRpc rpc) |
| | | 138 | | { |
| | 2536 | 139 | | if (_messageInspectors.Length > 0) |
| | | 140 | | { |
| | 3 | 141 | | AfterReceiveRequestCore(ref rpc); |
| | | 142 | | } |
| | 2536 | 143 | | } |
| | | 144 | | |
| | | 145 | | internal void AfterReceiveRequestCore(ref MessageRpc rpc) |
| | | 146 | | { |
| | | 147 | | try |
| | | 148 | | { |
| | 12 | 149 | | for (int i = 0; i < _messageInspectors.Length; i++) |
| | | 150 | | { |
| | 3 | 151 | | rpc.Correlation[i] = _messageInspectors[i].AfterReceiveRequest(ref rpc.Request, (IClientChannel)rpc. |
| | | 152 | | //if (TD.MessageInspectorAfterReceiveInvokedIsEnabled()) |
| | | 153 | | //{ |
| | | 154 | | // TD.MessageInspectorAfterReceiveInvoked(rpc.EventTraceActivity, this.messageInspectors[i].GetTy |
| | | 155 | | //} |
| | | 156 | | } |
| | 3 | 157 | | } |
| | 0 | 158 | | catch (Exception e) |
| | | 159 | | { |
| | 0 | 160 | | if (Fx.IsFatal(e)) |
| | | 161 | | { |
| | 0 | 162 | | throw; |
| | | 163 | | } |
| | 0 | 164 | | if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) |
| | | 165 | | { |
| | 0 | 166 | | throw; |
| | | 167 | | } |
| | 0 | 168 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 169 | | } |
| | 3 | 170 | | } |
| | | 171 | | |
| | | 172 | | private void BeforeSendReply(MessageRpc rpc, ref Exception exception, ref bool thereIsAnUnhandledException) |
| | | 173 | | { |
| | 2538 | 174 | | if (_messageInspectors.Length > 0) |
| | | 175 | | { |
| | 3 | 176 | | BeforeSendReplyCore(rpc, ref exception, ref thereIsAnUnhandledException); |
| | | 177 | | } |
| | 2538 | 178 | | } |
| | | 179 | | |
| | | 180 | | internal void BeforeSendReplyCore(MessageRpc rpc, ref Exception exception, ref bool thereIsAnUnhandledException) |
| | | 181 | | { |
| | 12 | 182 | | for (int i = 0; i < _messageInspectors.Length; i++) |
| | | 183 | | { |
| | | 184 | | try |
| | | 185 | | { |
| | 3 | 186 | | Message originalReply = rpc.Reply; |
| | 3 | 187 | | Message reply = originalReply; |
| | | 188 | | |
| | 3 | 189 | | _messageInspectors[i].BeforeSendReply(ref reply, rpc.Correlation[i]); |
| | | 190 | | //if (TD.MessageInspectorBeforeSendInvokedIsEnabled()) |
| | | 191 | | //{ |
| | | 192 | | // TD.MessageInspectorBeforeSendInvoked(rpc.EventTraceActivity, this.messageInspectors[i].GetType |
| | | 193 | | //} |
| | | 194 | | |
| | 3 | 195 | | if ((reply == null) && (originalReply != null)) |
| | | 196 | | { |
| | 0 | 197 | | string message = SR.Format(SR.SFxNullReplyFromExtension2, _messageInspectors[i].GetType().ToStri |
| | 0 | 198 | | ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message)); |
| | | 199 | | } |
| | 3 | 200 | | rpc.Reply = reply; |
| | 3 | 201 | | } |
| | 0 | 202 | | catch (Exception e) |
| | | 203 | | { |
| | 0 | 204 | | if (Fx.IsFatal(e)) |
| | | 205 | | { |
| | 0 | 206 | | throw; |
| | | 207 | | } |
| | 0 | 208 | | if (!ErrorBehavior.ShouldRethrowExceptionAsIs(e)) |
| | | 209 | | { |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | if (exception == null) |
| | | 214 | | { |
| | 0 | 215 | | exception = e; |
| | | 216 | | } |
| | 0 | 217 | | thereIsAnUnhandledException = (!ErrorBehavior.HandleError(e)) || thereIsAnUnhandledException; |
| | 0 | 218 | | } |
| | | 219 | | } |
| | 3 | 220 | | } |
| | | 221 | | |
| | | 222 | | private async Task<MessageRpc> ReplyAsync(MessageRpc rpc) |
| | | 223 | | { |
| | 728 | 224 | | rpc.RequestContextThrewOnReply = true; |
| | 728 | 225 | | rpc.SuccessfullySendReply = false; |
| | | 226 | | |
| | | 227 | | try |
| | | 228 | | { |
| | 728 | 229 | | await rpc.RequestContext.ReplyAsync(rpc.Reply, rpc.ReplyTimeoutHelper.GetCancellationToken()); |
| | 728 | 230 | | rpc.RequestContextThrewOnReply = false; |
| | 728 | 231 | | rpc.SuccessfullySendReply = true; |
| | | 232 | | |
| | | 233 | | //if (TD.DispatchMessageStopIsEnabled()) |
| | | 234 | | //{ |
| | | 235 | | // TD.DispatchMessageStop(rpc.EventTraceActivity); |
| | | 236 | | //} |
| | 728 | 237 | | } |
| | 0 | 238 | | catch (CommunicationException e) |
| | | 239 | | { |
| | 0 | 240 | | ErrorBehavior.HandleError(e); |
| | 0 | 241 | | } |
| | 0 | 242 | | catch (TimeoutException e) |
| | | 243 | | { |
| | 0 | 244 | | ErrorBehavior.HandleError(e); |
| | 0 | 245 | | } |
| | 0 | 246 | | catch (Exception e) |
| | | 247 | | { |
| | 0 | 248 | | if (Fx.IsFatal(e)) |
| | | 249 | | { |
| | 0 | 250 | | throw; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | //if (DiagnosticUtility.ShouldTraceError) |
| | | 254 | | //{ |
| | | 255 | | // TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.ServiceOperationExceptionOnReply, |
| | | 256 | | // SR.Format(SR.TraceCodeServiceOperationExceptionOnReply), |
| | | 257 | | // this, e); |
| | | 258 | | //} |
| | | 259 | | |
| | 0 | 260 | | if (!ErrorBehavior.HandleError(e)) |
| | | 261 | | { |
| | 0 | 262 | | rpc.RequestContextThrewOnReply = true; |
| | 0 | 263 | | rpc.CanSendReply = false; |
| | | 264 | | } |
| | 0 | 265 | | } |
| | | 266 | | |
| | 728 | 267 | | return rpc; |
| | 728 | 268 | | } |
| | | 269 | | |
| | | 270 | | internal Task<MessageRpc> DispatchAsync(MessageRpc rpc, bool isOperationContextSet) |
| | | 271 | | { |
| | 2538 | 272 | | rpc.ErrorProcessor = ProcessError; |
| | 2538 | 273 | | rpc.AsyncProcessor = ProcessMessageAsync; |
| | 2538 | 274 | | Task<MessageRpc> task = rpc.ProcessAsync(isOperationContextSet); |
| | 2538 | 275 | | rpc._processCallReturned = true; |
| | 2538 | 276 | | return task; |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | internal void InputSessionDoneReceiving(ServiceChannel channel) |
| | | 280 | | { |
| | 78 | 281 | | if (_inputSessionShutdownHandlers.Length > 0) |
| | | 282 | | { |
| | 0 | 283 | | InputSessionDoneReceivingCore(channel); |
| | | 284 | | } |
| | 78 | 285 | | } |
| | | 286 | | |
| | | 287 | | private void InputSessionDoneReceivingCore(ServiceChannel channel) |
| | | 288 | | { |
| | 0 | 289 | | if (channel.Proxy is IDuplexContextChannel proxy) |
| | | 290 | | { |
| | 0 | 291 | | IInputSessionShutdown[] handlers = _inputSessionShutdownHandlers; |
| | | 292 | | try |
| | | 293 | | { |
| | 0 | 294 | | for (int i = 0; i < handlers.Length; i++) |
| | | 295 | | { |
| | 0 | 296 | | handlers[i].DoneReceiving(proxy); |
| | | 297 | | } |
| | 0 | 298 | | } |
| | 0 | 299 | | catch (Exception e) |
| | | 300 | | { |
| | 0 | 301 | | if (Fx.IsFatal(e)) |
| | | 302 | | { |
| | 0 | 303 | | throw; |
| | | 304 | | } |
| | 0 | 305 | | if (!ErrorBehavior.HandleError(e)) |
| | | 306 | | { |
| | 0 | 307 | | proxy.Abort(); |
| | | 308 | | } |
| | 0 | 309 | | } |
| | | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | internal bool IsConcurrent(MessageRpc rpc) |
| | | 314 | | { |
| | 0 | 315 | | return _concurrency.IsConcurrent(rpc); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | internal void InputSessionFaulted(ServiceChannel channel) |
| | | 319 | | { |
| | 0 | 320 | | if (_inputSessionShutdownHandlers.Length > 0) |
| | | 321 | | { |
| | 0 | 322 | | InputSessionFaultedCore(channel); |
| | | 323 | | } |
| | 0 | 324 | | } |
| | | 325 | | |
| | | 326 | | private void InputSessionFaultedCore(ServiceChannel channel) |
| | | 327 | | { |
| | 0 | 328 | | if (channel.Proxy is IDuplexContextChannel proxy) |
| | | 329 | | { |
| | 0 | 330 | | IInputSessionShutdown[] handlers = _inputSessionShutdownHandlers; |
| | | 331 | | try |
| | | 332 | | { |
| | 0 | 333 | | for (int i = 0; i < handlers.Length; i++) |
| | | 334 | | { |
| | 0 | 335 | | handlers[i].ChannelFaulted(proxy); |
| | | 336 | | } |
| | 0 | 337 | | } |
| | 0 | 338 | | catch (Exception e) |
| | | 339 | | { |
| | 0 | 340 | | if (Fx.IsFatal(e)) |
| | | 341 | | { |
| | 0 | 342 | | throw; |
| | | 343 | | } |
| | 0 | 344 | | if (!ErrorBehavior.HandleError(e)) |
| | | 345 | | { |
| | 0 | 346 | | proxy.Abort(); |
| | | 347 | | } |
| | 0 | 348 | | } |
| | | 349 | | } |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | private void AddMessageProperties(Message message, OperationContext context, ServiceChannel replyChannel) |
| | | 353 | | { |
| | 728 | 354 | | if (context.InternalServiceChannel == replyChannel) |
| | | 355 | | { |
| | 728 | 356 | | if (context.HasOutgoingMessageHeaders) |
| | | 357 | | { |
| | 0 | 358 | | message.Headers.CopyHeadersFrom(context.OutgoingMessageHeaders); |
| | | 359 | | } |
| | | 360 | | |
| | 728 | 361 | | if (context.HasOutgoingMessageProperties) |
| | | 362 | | { |
| | 35 | 363 | | message.Properties.MergeProperties(context.OutgoingMessageProperties); |
| | | 364 | | } |
| | | 365 | | } |
| | 728 | 366 | | } |
| | | 367 | | |
| | | 368 | | private async ValueTask PrepareReplyAsync(MessageRpc rpc) |
| | | 369 | | { |
| | 2538 | 370 | | RequestContext context = rpc.OperationContext.RequestContext; |
| | 2538 | 371 | | Exception exception = null; |
| | 2538 | 372 | | bool thereIsAnUnhandledException = false; |
| | | 373 | | |
| | 2538 | 374 | | if (!rpc.Operation.IsOneWay) |
| | | 375 | | { |
| | | 376 | | //if (DiagnosticUtility.ShouldTraceWarning) |
| | | 377 | | //{ |
| | | 378 | | // // If a service both returns null and sets RequestContext null, that |
| | | 379 | | // // means they handled it (either by calling Close or Reply manually). |
| | | 380 | | // // These traces catch accidents, where you accidentally return null, |
| | | 381 | | // // or you accidentally close the context so we can't return your message. |
| | | 382 | | // if ((rpc.Reply == null) && (context != null)) |
| | | 383 | | // { |
| | | 384 | | // TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Warning, |
| | | 385 | | // TraceCode.ServiceOperationMissingReply, |
| | | 386 | | // SR.Format(SR.TraceCodeServiceOperationMissingReply, rpc.Operation.Name ?? String.Empty), |
| | | 387 | | // null, null); |
| | | 388 | | // } |
| | | 389 | | // else if ((context == null) && (rpc.Reply != null)) |
| | | 390 | | // { |
| | | 391 | | // TraceUtility.TraceEvent(System.Diagnostics.TraceEventType.Warning, |
| | | 392 | | // TraceCode.ServiceOperationMissingReplyContext, |
| | | 393 | | // SR.Format(SR.TraceCodeServiceOperationMissingReplyContext, rpc.Operation.Name ?? String.Em |
| | | 394 | | // null, null); |
| | | 395 | | // } |
| | | 396 | | //} |
| | | 397 | | |
| | 728 | 398 | | if ((context != null) && (rpc.Reply != null)) |
| | | 399 | | { |
| | | 400 | | try |
| | | 401 | | { |
| | 728 | 402 | | rpc.CanSendReply = PrepareAndAddressReply(ref rpc); |
| | 728 | 403 | | } |
| | 0 | 404 | | catch (Exception e) |
| | | 405 | | { |
| | 0 | 406 | | if (Fx.IsFatal(e)) |
| | | 407 | | { |
| | 0 | 408 | | throw; |
| | | 409 | | } |
| | 0 | 410 | | thereIsAnUnhandledException = (!ErrorBehavior.HandleError(e)) || thereIsAnUnhandledException; |
| | 0 | 411 | | exception = e; |
| | 0 | 412 | | } |
| | | 413 | | } |
| | | 414 | | } |
| | | 415 | | |
| | 2538 | 416 | | if (_activity != null) |
| | | 417 | | { |
| | 2 | 418 | | var reply = rpc.Reply; |
| | 2 | 419 | | if (_activity.IsAllDataRequested && reply != null) |
| | | 420 | | { |
| | 2 | 421 | | if (reply.IsFault) |
| | | 422 | | { |
| | 1 | 423 | | _activity.SetStatus(ActivityStatusCode.Error); |
| | | 424 | | } |
| | | 425 | | |
| | 2 | 426 | | _activity.SetTag(WcfInstrumentationConstants.SoapReplyActionTag, reply.Headers.Action); |
| | | 427 | | } |
| | | 428 | | |
| | 2 | 429 | | _activity.Stop(); |
| | | 430 | | } |
| | | 431 | | |
| | 2538 | 432 | | BeforeSendReply(rpc, ref exception, ref thereIsAnUnhandledException); |
| | | 433 | | |
| | 2538 | 434 | | if (rpc.Operation.IsOneWay) |
| | | 435 | | { |
| | 1810 | 436 | | rpc.CanSendReply = false; |
| | | 437 | | } |
| | | 438 | | |
| | 2538 | 439 | | if (!rpc.Operation.IsOneWay && (context != null) && (rpc.Reply != null)) |
| | | 440 | | { |
| | 728 | 441 | | if (exception != null) |
| | | 442 | | { |
| | | 443 | | // We don't call ProvideFault again, since we have already passed the |
| | | 444 | | // point where SFx addresses the reply, and it is reasonable for |
| | | 445 | | // ProvideFault to expect that SFx will address the reply. Instead |
| | | 446 | | // we always just do 'internal server error' processing. |
| | 0 | 447 | | rpc.Error = exception; |
| | 0 | 448 | | ErrorBehavior.ProvideOnlyFaultOfLastResort(ref rpc); |
| | | 449 | | |
| | | 450 | | try |
| | | 451 | | { |
| | 0 | 452 | | rpc.CanSendReply = PrepareAndAddressReply(ref rpc); |
| | 0 | 453 | | } |
| | 0 | 454 | | catch (Exception e) |
| | | 455 | | { |
| | 0 | 456 | | if (Fx.IsFatal(e)) |
| | | 457 | | { |
| | 0 | 458 | | throw; |
| | | 459 | | } |
| | 0 | 460 | | ErrorBehavior.HandleError(e); |
| | 0 | 461 | | } |
| | | 462 | | } |
| | | 463 | | } |
| | 1810 | 464 | | else if ((exception != null) && thereIsAnUnhandledException) |
| | | 465 | | { |
| | 0 | 466 | | await rpc.AbortAsync(); |
| | | 467 | | } |
| | 2538 | 468 | | } |
| | | 469 | | |
| | | 470 | | private bool PrepareAndAddressReply(ref MessageRpc rpc) |
| | | 471 | | { |
| | 728 | 472 | | bool canSendReply = true; |
| | | 473 | | |
| | 728 | 474 | | if (!ManualAddressing) |
| | | 475 | | { |
| | 683 | 476 | | if (!ReferenceEquals(rpc.RequestID, null)) |
| | | 477 | | { |
| | 294 | 478 | | RequestReplyCorrelator.PrepareReply(rpc.Reply, rpc.RequestID); |
| | | 479 | | } |
| | | 480 | | |
| | 683 | 481 | | if (!rpc.Channel.HasSession) |
| | | 482 | | { |
| | 580 | 483 | | canSendReply = RequestReplyCorrelator.AddressReply(rpc.Reply, rpc.ReplyToInfo); |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | |
| | 728 | 487 | | AddMessageProperties(rpc.Reply, rpc.OperationContext, rpc.Channel); |
| | | 488 | | //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled && rpc.EventTraceActivity != null) |
| | | 489 | | //{ |
| | | 490 | | // rpc.Reply.Properties[EventTraceActivity.Name] = rpc.EventTraceActivity; |
| | | 491 | | //} |
| | | 492 | | |
| | 728 | 493 | | return canSendReply; |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | internal DispatchOperationRuntime GetOperation(ref Message message) |
| | | 497 | | { |
| | 2539 | 498 | | return _demuxer.GetOperation(ref message); |
| | | 499 | | } |
| | | 500 | | |
| | | 501 | | private void ReceiveContextRPCFacet_CreatIfRequired_Shim(MessageRpc rpc) |
| | | 502 | | { |
| | 2538 | 503 | | rpc.ReceiveContext = ReceiveContext.TryGet(rpc.Request, out ReceiveContext receiveContext) |
| | 2538 | 504 | | ? receiveContext |
| | 2538 | 505 | | : null; |
| | 2538 | 506 | | } |
| | | 507 | | |
| | | 508 | | internal async Task<MessageRpc> ProcessMessageAsync(MessageRpc rpc) |
| | | 509 | | { |
| | 2538 | 510 | | ReceiveContextRPCFacet_CreatIfRequired_Shim(rpc); |
| | | 511 | | |
| | 2538 | 512 | | if (rpc.Operation.IsOneWay) |
| | | 513 | | { |
| | 1810 | 514 | | await rpc.RequestContext.ReplyAsync(null); |
| | 1810 | 515 | | rpc.OperationContext.RequestContext = null; |
| | | 516 | | } |
| | | 517 | | else |
| | | 518 | | { |
| | 728 | 519 | | if (!rpc.Channel.IsReplyChannel && |
| | 728 | 520 | | ((object)rpc.RequestID == null) && |
| | 728 | 521 | | (rpc.Operation.Action != MessageHeaders.WildcardAction)) |
| | | 522 | | { |
| | 0 | 523 | | CommunicationException error = new CommunicationException(SR.SFxOneWayMessageToTwoWayMethod0); |
| | 0 | 524 | | throw TraceUtility.ThrowHelperError(error, rpc.Request); |
| | | 525 | | } |
| | | 526 | | |
| | 728 | 527 | | if (!ManualAddressing) |
| | | 528 | | { |
| | 683 | 529 | | EndpointAddress replyTo = rpc.ReplyToInfo.ReplyTo; |
| | 683 | 530 | | if (replyTo != null && replyTo.IsNone && rpc.Channel.IsReplyChannel) |
| | | 531 | | { |
| | 0 | 532 | | CommunicationException error = new CommunicationException(SR.SFxRequestReplyNone); |
| | 0 | 533 | | throw TraceUtility.ThrowHelperError(error, rpc.Request); |
| | | 534 | | } |
| | | 535 | | |
| | 683 | 536 | | if (_isOnServer) |
| | | 537 | | { |
| | 683 | 538 | | EndpointAddress remoteAddress = rpc.Channel.RemoteAddress; |
| | 683 | 539 | | if ((remoteAddress != null) && !remoteAddress.IsAnonymous) |
| | | 540 | | { |
| | 0 | 541 | | MessageHeaders headers = rpc.Request.Headers; |
| | 0 | 542 | | Uri remoteUri = remoteAddress.Uri; |
| | | 543 | | |
| | 0 | 544 | | if ((replyTo != null) && !replyTo.IsAnonymous && (remoteUri != replyTo.Uri)) |
| | | 545 | | { |
| | 0 | 546 | | string text = SR.Format(SR.SFxRequestHasInvalidReplyToOnServer, replyTo.Uri, remoteUri); |
| | 0 | 547 | | Exception error = new InvalidOperationException(text); |
| | 0 | 548 | | throw TraceUtility.ThrowHelperError(error, rpc.Request); |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | EndpointAddress faultTo = headers.FaultTo; |
| | 0 | 552 | | if ((faultTo != null) && !faultTo.IsAnonymous && (remoteUri != faultTo.Uri)) |
| | | 553 | | { |
| | 0 | 554 | | string text = SR.Format(SR.SFxRequestHasInvalidFaultToOnServer, faultTo.Uri, remoteUri); |
| | 0 | 555 | | Exception error = new InvalidOperationException(text); |
| | 0 | 556 | | throw TraceUtility.ThrowHelperError(error, rpc.Request); |
| | | 557 | | } |
| | | 558 | | |
| | 0 | 559 | | if (rpc.RequestVersion.Addressing == AddressingVersion.WSAddressingAugust2004) |
| | | 560 | | { |
| | 0 | 561 | | EndpointAddress from = headers.From; |
| | 0 | 562 | | if ((from != null) && !from.IsAnonymous && (remoteUri != from.Uri)) |
| | | 563 | | { |
| | 0 | 564 | | string text = SR.Format(SR.SFxRequestHasInvalidFromOnServer, from.Uri, remoteUri); |
| | 0 | 565 | | Exception error = new InvalidOperationException(text); |
| | 0 | 566 | | throw TraceUtility.ThrowHelperError(error, rpc.Request); |
| | | 567 | | } |
| | | 568 | | } |
| | | 569 | | } |
| | | 570 | | } |
| | | 571 | | } |
| | | 572 | | } |
| | | 573 | | |
| | 2538 | 574 | | if (_concurrency.IsConcurrent(rpc)) |
| | | 575 | | { |
| | 2480 | 576 | | rpc.Channel.IncrementActivity(); |
| | 2480 | 577 | | rpc.SuccessfullyIncrementedActivity = true; |
| | | 578 | | } |
| | | 579 | | |
| | 2538 | 580 | | if (_authenticationBehavior != null) |
| | | 581 | | { |
| | 0 | 582 | | rpc = await _authenticationBehavior.AuthenticateAsync(rpc); |
| | | 583 | | } |
| | | 584 | | |
| | 2538 | 585 | | if (_authorizationBehavior != null && !SupportsAuthorizationData) |
| | | 586 | | { |
| | 17 | 587 | | rpc = await _authorizationBehavior.AuthorizeAsync(rpc); |
| | | 588 | | } |
| | | 589 | | |
| | 2536 | 590 | | await InstanceBehavior.EnsureInstanceContextAsync(rpc); |
| | 2536 | 591 | | TransferChannelFromPendingList(rpc); |
| | 2536 | 592 | | await AcquireDynamicInstanceContextAsync(rpc); |
| | | 593 | | |
| | 2536 | 594 | | _activity = CreateActivity(ref rpc.Request, (IClientChannel)rpc.Channel.Proxy, rpc.InstanceContext); |
| | | 595 | | |
| | 2536 | 596 | | AfterReceiveRequest(ref rpc); |
| | | 597 | | |
| | 2536 | 598 | | await _concurrency.LockInstanceAsync(rpc); |
| | 2536 | 599 | | rpc.SuccessfullyLockedInstance = true; |
| | | 600 | | |
| | | 601 | | try |
| | | 602 | | { |
| | | 603 | | // TaskHelpers has an extension method which enables awaitting a sync context to run continuation on it. |
| | 2536 | 604 | | await _thread.GetSyncContext(rpc); |
| | 2536 | 605 | | } |
| | 0 | 606 | | catch (Exception e) |
| | | 607 | | { |
| | 0 | 608 | | if (Fx.IsFatal(e)) |
| | | 609 | | { |
| | 0 | 610 | | throw; |
| | | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperFatal(e.Message, e); |
| | | 614 | | } |
| | | 615 | | |
| | | 616 | | // This needs to happen after LockInstance--LockInstance guarantees |
| | | 617 | | // in-order delivery, so we can't receive the next message until we |
| | | 618 | | // have acquired the lock. |
| | | 619 | | // |
| | | 620 | | // This also needs to happen after BindThread based on the assumption |
| | | 621 | | // that running on UI thread should guarantee in-order delivery if |
| | | 622 | | // the SynchronizationContext is single threaded. |
| | 2536 | 623 | | if (_concurrency.IsConcurrent(rpc)) |
| | | 624 | | { |
| | 2478 | 625 | | rpc.EnsureReceive(); |
| | 2478 | 626 | | if (!rpc._processCallReturned) |
| | | 627 | | { |
| | | 628 | | // To allow transport receive loop to get next request, the call to dispatch the current message nee |
| | | 629 | | // If all previous await's have completed synchronously, execution needs to be forced to continue on |
| | | 630 | | // This code causes this method to continue on another thread and any calling receive pump (such as |
| | | 631 | | // use this thread to request the next message. It might be better to switch that so this thread con |
| | | 632 | | // thread and the caller has to run on a new thread. |
| | 2404 | 633 | | await Task.Yield(); |
| | | 634 | | } |
| | | 635 | | } |
| | | 636 | | |
| | 2536 | 637 | | InstanceBehavior.EnsureServiceInstance(rpc); |
| | | 638 | | |
| | 2535 | 639 | | if (RequireClaimsPrincipalOnOperationContext) |
| | | 640 | | { |
| | 55 | 641 | | rpc.Operation.SetClaimsPrincipalToOperationContext(rpc); |
| | | 642 | | } |
| | | 643 | | |
| | 2535 | 644 | | if (_authorizationBehavior != null && SupportsAuthorizationData) |
| | | 645 | | { |
| | 55 | 646 | | rpc = await _authorizationBehavior.AuthorizePolicyAsync(rpc); |
| | | 647 | | } |
| | | 648 | | |
| | | 649 | | try |
| | | 650 | | { |
| | 2505 | 651 | | SetActivityIdOnThread(rpc); |
| | 2505 | 652 | | rpc = await rpc.Operation.InvokeAsync(rpc); |
| | 2375 | 653 | | } |
| | 130 | 654 | | catch |
| | | 655 | | { |
| | | 656 | | // This catch clause forces ClearCallContext to run prior to stackwalks exiting this frame. |
| | 130 | 657 | | throw; |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | try |
| | | 661 | | { |
| | | 662 | | // Switch back to thread pool if we're using a non-default Sync Context. This only switches threads if n |
| | 2375 | 663 | | await TaskHelpers.EnsureDefaultTaskScheduler(); |
| | 2375 | 664 | | } |
| | 0 | 665 | | catch (Exception e) |
| | | 666 | | { |
| | 0 | 667 | | if (Fx.IsFatal(e)) |
| | | 668 | | { |
| | 0 | 669 | | throw; |
| | | 670 | | } |
| | | 671 | | |
| | 0 | 672 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperFatal(e.Message, e); |
| | | 673 | | } |
| | | 674 | | |
| | 2375 | 675 | | await ProcessError(rpc); |
| | | 676 | | |
| | 2375 | 677 | | if (!_concurrency.IsConcurrent(rpc)) |
| | | 678 | | { |
| | 58 | 679 | | rpc.EnsureReceive(); |
| | | 680 | | } |
| | | 681 | | |
| | 2375 | 682 | | return rpc; |
| | 2375 | 683 | | } |
| | | 684 | | |
| | | 685 | | private Activity CreateActivity(ref Message request, IClientChannel channel, InstanceContext instanceContext) |
| | | 686 | | { |
| | 2536 | 687 | | var activity = WcfInstrumentationActivitySource.ActivitySource.StartActivity( |
| | 2536 | 688 | | WcfInstrumentationActivitySource.IncomingRequestActivityName, |
| | 2536 | 689 | | ActivityKind.Server); |
| | | 690 | | |
| | 2536 | 691 | | if (activity != null) |
| | | 692 | | { |
| | | 693 | | string action; |
| | 2 | 694 | | if (!string.IsNullOrEmpty(request.Headers.Action)) |
| | | 695 | | { |
| | 2 | 696 | | action = request.Headers.Action; |
| | 2 | 697 | | activity.DisplayName = action; |
| | | 698 | | } |
| | | 699 | | else |
| | | 700 | | { |
| | 0 | 701 | | action = string.Empty; |
| | | 702 | | } |
| | | 703 | | |
| | 2 | 704 | | if (activity.IsAllDataRequested) |
| | | 705 | | { |
| | 2 | 706 | | activity.SetTag(WcfInstrumentationConstants.RpcSystemTag, |
| | 2 | 707 | | WcfInstrumentationConstants.WcfSystemValue); |
| | | 708 | | |
| | 2 | 709 | | var actionMetadata = GetActionMetadata(request, action); |
| | | 710 | | |
| | 2 | 711 | | activity.SetTag(WcfInstrumentationConstants.RpcServiceTag, actionMetadata.ContractName); |
| | 2 | 712 | | activity.SetTag(WcfInstrumentationConstants.RpcMethodTag, actionMetadata.OperationName); |
| | 2 | 713 | | activity.SetTag(WcfInstrumentationConstants.SoapMessageVersionTag, request.Version.ToString()); |
| | | 714 | | |
| | 2 | 715 | | var localAddressUri = channel.LocalAddress?.Uri; |
| | 2 | 716 | | if (localAddressUri != null) |
| | | 717 | | { |
| | 2 | 718 | | activity.SetTag(WcfInstrumentationConstants.NetHostNameTag, localAddressUri.Host); |
| | 2 | 719 | | activity.SetTag(WcfInstrumentationConstants.NetHostPortTag, localAddressUri.Port); |
| | 2 | 720 | | activity.SetTag(WcfInstrumentationConstants.WcfChannelSchemeTag, localAddressUri.Scheme); |
| | 2 | 721 | | activity.SetTag(WcfInstrumentationConstants.WcfChannelPathTag, localAddressUri.LocalPath); |
| | | 722 | | } |
| | | 723 | | } |
| | | 724 | | } |
| | | 725 | | |
| | 2536 | 726 | | return activity; |
| | | 727 | | } |
| | | 728 | | |
| | | 729 | | private async Task ProcessError(MessageRpc rpc) |
| | | 730 | | { |
| | | 731 | | try |
| | | 732 | | { |
| | 2538 | 733 | | ErrorBehavior.ProvideMessageFault(rpc); |
| | 2538 | 734 | | } |
| | 0 | 735 | | catch (Exception e) |
| | | 736 | | { |
| | 0 | 737 | | if (Fx.IsFatal(e)) |
| | | 738 | | { |
| | 0 | 739 | | throw; |
| | | 740 | | } |
| | | 741 | | |
| | 0 | 742 | | ErrorBehavior.HandleError(e); |
| | 0 | 743 | | } |
| | | 744 | | |
| | 2538 | 745 | | await PrepareReplyAsync(rpc); |
| | | 746 | | |
| | 2538 | 747 | | if (rpc.CanSendReply) |
| | | 748 | | { |
| | 728 | 749 | | rpc.ReplyTimeoutHelper = new TimeoutHelper(rpc.Channel.OperationTimeout); |
| | | 750 | | //if (rpc.Reply != null) |
| | | 751 | | //{ |
| | | 752 | | // TraceUtility.MessageFlowAtMessageSent(rpc.Reply, rpc.EventTraceActivity); |
| | | 753 | | //} |
| | | 754 | | |
| | 728 | 755 | | await ReplyAsync(rpc); |
| | | 756 | | } |
| | | 757 | | |
| | 2538 | 758 | | await ProcessMessageCleanupAsync(rpc); |
| | 2538 | 759 | | } |
| | | 760 | | |
| | | 761 | | private static ActionMetadata GetActionMetadata(Message request, string action) |
| | | 762 | | { |
| | 2 | 763 | | ActionMetadata? actionMetadata = null; |
| | 2 | 764 | | if (request.Properties.TryGetValue(TelemetryContextMessageProperty.Name, out var telemetryContextProperty)) |
| | | 765 | | { |
| | 0 | 766 | | var actionMappings = (telemetryContextProperty as TelemetryContextMessageProperty)?.ActionMappings; |
| | 0 | 767 | | if (actionMappings != null && actionMappings.TryGetValue(action, out var metadata)) |
| | | 768 | | { |
| | 0 | 769 | | actionMetadata = metadata; |
| | | 770 | | } |
| | | 771 | | } |
| | | 772 | | |
| | 2 | 773 | | return actionMetadata ?? new ActionMetadata( |
| | 2 | 774 | | contractName: null, |
| | 2 | 775 | | operationName: action); |
| | | 776 | | } |
| | | 777 | | |
| | | 778 | | // Logic for knowing when to close stuff: |
| | | 779 | | // |
| | | 780 | | // ASSUMPTIONS: |
| | | 781 | | // Closing a stream over a message also closes the message. |
| | | 782 | | // Closing a message over a stream does not close the stream. |
| | | 783 | | // (OperationStreamProvider.ReleaseStream is no-op) |
| | | 784 | | // |
| | | 785 | | // This is a table of what should be disposed in what cases. |
| | | 786 | | // The rows represent the type of parameter to the method and |
| | | 787 | | // whether we are disposing parameters or not. The columns |
| | | 788 | | // are for the inputs vs. the outputs. The cells contain the |
| | | 789 | | // values that need to be Disposed. M^P means that exactly |
| | | 790 | | // one of the message and parameter needs to be disposed, |
| | | 791 | | // since they refer to the same object. |
| | | 792 | | // |
| | | 793 | | // Request Reply |
| | | 794 | | // Message | M or P | M or P |
| | | 795 | | // Dispose Stream | P | M and P |
| | | 796 | | // Params | M and P | M and P |
| | | 797 | | // | | |
| | | 798 | | // Message | none | none |
| | | 799 | | // NoDispose Stream | none | M |
| | | 800 | | // Params | M | M |
| | | 801 | | // |
| | | 802 | | // By choosing to dispose the parameter in both of the "M or P" |
| | | 803 | | // cases, the logic needed to generate this table is: |
| | | 804 | | // |
| | | 805 | | // CloseRequestMessage = IsParams |
| | | 806 | | // CloseRequestParams = rpc.Operation.DisposeParameters |
| | | 807 | | // CloseReplyMessage = rpc.Operation.SerializeReply |
| | | 808 | | // CloseReplyParams = rpc.Operation.DisposeParameters |
| | | 809 | | // |
| | | 810 | | // IsParams can be calculated based on whether the request |
| | | 811 | | // message was consumed after deserializing but before calling |
| | | 812 | | // the user. This is stored as rpc.DidDeserializeRequestBody. |
| | | 813 | | // |
| | | 814 | | private async Task ProcessMessageCleanupAsync(MessageRpc rpc) |
| | | 815 | | { |
| | | 816 | | Fx.Assert( |
| | | 817 | | !ReferenceEquals(rpc.ErrorProcessor, _processMessageCleanupError), |
| | | 818 | | "ProcessMessageCleanup run twice on the same MessageRpc!"); |
| | 2538 | 819 | | rpc.ErrorProcessor = _processMessageCleanupError; |
| | | 820 | | |
| | 2538 | 821 | | bool replyWasSent = false; |
| | | 822 | | |
| | 2538 | 823 | | if (rpc.CanSendReply) |
| | | 824 | | { |
| | 728 | 825 | | replyWasSent = rpc.SuccessfullySendReply; |
| | | 826 | | } |
| | | 827 | | |
| | | 828 | | try |
| | | 829 | | { |
| | | 830 | | try |
| | | 831 | | { |
| | 2538 | 832 | | if (rpc.DidDeserializeRequestBody) |
| | | 833 | | { |
| | 2425 | 834 | | rpc.Request.Close(); |
| | | 835 | | } |
| | 2538 | 836 | | } |
| | 0 | 837 | | catch (Exception e) |
| | | 838 | | { |
| | 0 | 839 | | if (Fx.IsFatal(e)) |
| | | 840 | | { |
| | 0 | 841 | | throw; |
| | | 842 | | } |
| | 0 | 843 | | ErrorBehavior.HandleError(e); |
| | 0 | 844 | | } |
| | | 845 | | |
| | 2538 | 846 | | rpc.DisposeParameters(false); //Dispose all input/output/return parameters |
| | | 847 | | |
| | 2538 | 848 | | if (rpc.FaultInfo.IsConsideredUnhandled) |
| | | 849 | | { |
| | 58 | 850 | | if (!replyWasSent) |
| | | 851 | | { |
| | 53 | 852 | | await rpc.AbortRequestContextAsync(); |
| | 53 | 853 | | rpc.AbortChannel(); |
| | | 854 | | } |
| | | 855 | | else |
| | | 856 | | { |
| | 5 | 857 | | await rpc.CloseRequestContextAsync(); |
| | 5 | 858 | | await rpc.CloseChannelAsync(); |
| | | 859 | | } |
| | 58 | 860 | | rpc.AbortInstanceContext(); |
| | | 861 | | } |
| | | 862 | | else |
| | | 863 | | { |
| | 2480 | 864 | | if (rpc.RequestContextThrewOnReply) |
| | | 865 | | { |
| | 0 | 866 | | await rpc.AbortRequestContextAsync(); |
| | | 867 | | } |
| | | 868 | | else |
| | | 869 | | { |
| | 2480 | 870 | | await rpc.CloseRequestContextAsync(); |
| | | 871 | | } |
| | | 872 | | } |
| | | 873 | | |
| | 2538 | 874 | | if ((rpc.Reply != null) && (rpc.Reply != rpc.ReturnParameter)) |
| | | 875 | | { |
| | | 876 | | try |
| | | 877 | | { |
| | 761 | 878 | | rpc.Reply.Close(); |
| | 761 | 879 | | } |
| | 0 | 880 | | catch (Exception e) |
| | | 881 | | { |
| | 0 | 882 | | if (Fx.IsFatal(e)) |
| | | 883 | | { |
| | 0 | 884 | | throw; |
| | | 885 | | } |
| | 0 | 886 | | ErrorBehavior.HandleError(e); |
| | 0 | 887 | | } |
| | | 888 | | } |
| | | 889 | | |
| | 2538 | 890 | | if ((rpc.FaultInfo.Fault != null) && (rpc.FaultInfo.Fault.State != MessageState.Closed)) |
| | | 891 | | { |
| | | 892 | | // maybe ProvideFault gave a Message, but then BeforeSendReply replaced it |
| | | 893 | | // in that case, we need to close the one from ProvideFault |
| | | 894 | | try |
| | | 895 | | { |
| | 0 | 896 | | rpc.FaultInfo.Fault.Close(); |
| | 0 | 897 | | } |
| | 0 | 898 | | catch (Exception e) |
| | | 899 | | { |
| | 0 | 900 | | if (Fx.IsFatal(e)) |
| | | 901 | | { |
| | 0 | 902 | | throw; |
| | | 903 | | } |
| | 0 | 904 | | ErrorBehavior.HandleError(e); |
| | 0 | 905 | | } |
| | | 906 | | } |
| | | 907 | | |
| | | 908 | | try |
| | | 909 | | { |
| | 2538 | 910 | | rpc.OperationContext.FireOperationCompleted(); |
| | 2538 | 911 | | } |
| | | 912 | | |
| | 0 | 913 | | catch (Exception e) |
| | | 914 | | { |
| | 0 | 915 | | if (Fx.IsFatal(e)) |
| | | 916 | | { |
| | 0 | 917 | | throw; |
| | | 918 | | } |
| | 0 | 919 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 920 | | } |
| | | 921 | | |
| | 2538 | 922 | | InstanceBehavior.AfterReply(ref rpc, ErrorBehavior); |
| | | 923 | | |
| | 2538 | 924 | | if (rpc.SuccessfullyLockedInstance) |
| | | 925 | | { |
| | | 926 | | try |
| | | 927 | | { |
| | 2536 | 928 | | _concurrency.UnlockInstance(ref rpc); |
| | 2536 | 929 | | } |
| | 0 | 930 | | catch (Exception e) |
| | | 931 | | { |
| | 0 | 932 | | if (Fx.IsFatal(e)) |
| | | 933 | | { |
| | 0 | 934 | | throw; |
| | | 935 | | } |
| | | 936 | | |
| | | 937 | | Fx.Assert("Exceptions should be caught by callee"); |
| | 0 | 938 | | rpc.InstanceContext.FaultInternal(); |
| | 0 | 939 | | ErrorBehavior.HandleError(e); |
| | 0 | 940 | | } |
| | | 941 | | } |
| | | 942 | | |
| | 2538 | 943 | | if (_terminate != null) |
| | | 944 | | { |
| | | 945 | | try |
| | | 946 | | { |
| | 11 | 947 | | _terminate.AfterReply(ref rpc); |
| | 11 | 948 | | } |
| | 0 | 949 | | catch (Exception e) |
| | | 950 | | { |
| | 0 | 951 | | if (Fx.IsFatal(e)) |
| | | 952 | | { |
| | 0 | 953 | | throw; |
| | | 954 | | } |
| | 0 | 955 | | ErrorBehavior.HandleError(e); |
| | 0 | 956 | | } |
| | | 957 | | } |
| | | 958 | | |
| | 2538 | 959 | | if (rpc.SuccessfullyIncrementedActivity) |
| | | 960 | | { |
| | | 961 | | try |
| | | 962 | | { |
| | 2480 | 963 | | await rpc.Channel.DecrementActivityAsync(); |
| | 2480 | 964 | | } |
| | 0 | 965 | | catch (Exception e) |
| | | 966 | | { |
| | 0 | 967 | | if (Fx.IsFatal(e)) |
| | | 968 | | { |
| | 0 | 969 | | throw; |
| | | 970 | | } |
| | 0 | 971 | | ErrorBehavior.HandleError(e); |
| | 0 | 972 | | } |
| | | 973 | | } |
| | 2538 | 974 | | } |
| | | 975 | | finally |
| | | 976 | | { |
| | | 977 | | // TODO: Add the code for the other half of InstanceContextServiceThrottle being acquired |
| | 2538 | 978 | | if (rpc.MessageRpcOwnsInstanceContextThrottle && rpc.ChannelHandler.InstanceContextServiceThrottle != nu |
| | | 979 | | { |
| | 0 | 980 | | rpc.ChannelHandler.InstanceContextServiceThrottle.DeactivateInstanceContext(); |
| | | 981 | | } |
| | | 982 | | |
| | | 983 | | //if (rpc.Activity != null && DiagnosticUtility.ShouldUseActivity) |
| | | 984 | | //{ |
| | | 985 | | // rpc.Activity.Stop(); |
| | | 986 | | //} |
| | | 987 | | } |
| | | 988 | | |
| | 2538 | 989 | | ErrorBehavior.HandleError(rpc); |
| | 2538 | 990 | | } |
| | | 991 | | |
| | | 992 | | private async Task ProcessMessageNonCleanupError(MessageRpc rpc) |
| | | 993 | | { |
| | | 994 | | try |
| | | 995 | | { |
| | 0 | 996 | | ErrorBehavior.ProvideMessageFault(rpc); |
| | 0 | 997 | | } |
| | 0 | 998 | | catch (Exception e) |
| | | 999 | | { |
| | 0 | 1000 | | if (Fx.IsFatal(e)) |
| | | 1001 | | { |
| | 0 | 1002 | | throw; |
| | | 1003 | | } |
| | | 1004 | | |
| | 0 | 1005 | | ErrorBehavior.HandleError(e); |
| | 0 | 1006 | | } |
| | | 1007 | | |
| | 0 | 1008 | | await PrepareReplyAsync(rpc); |
| | 0 | 1009 | | } |
| | | 1010 | | |
| | | 1011 | | private Task ProcessMessageCleanupError(MessageRpc rpc) |
| | | 1012 | | { |
| | 0 | 1013 | | ErrorBehavior.HandleError(rpc); |
| | 0 | 1014 | | return Task.CompletedTask; |
| | | 1015 | | } |
| | | 1016 | | |
| | | 1017 | | private void SetActivityIdOnThread(MessageRpc rpc) |
| | | 1018 | | { |
| | | 1019 | | //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled && rpc.EventTraceActivity != null) |
| | | 1020 | | //{ |
| | | 1021 | | // // Propogate the ActivityId to the service operation |
| | | 1022 | | // EventTraceActivityHelper.SetOnThread(rpc.EventTraceActivity); |
| | | 1023 | | //} |
| | 2505 | 1024 | | } |
| | | 1025 | | |
| | | 1026 | | private void TransferChannelFromPendingList(MessageRpc rpc) |
| | | 1027 | | { |
| | 2536 | 1028 | | if (rpc.Channel.IsPending) |
| | | 1029 | | { |
| | 513 | 1030 | | rpc.Channel.IsPending = false; |
| | | 1031 | | |
| | 513 | 1032 | | ChannelDispatcher channelDispatcher = rpc.Channel.ChannelDispatcher; |
| | 513 | 1033 | | IInstanceContextProvider provider = InstanceBehavior.InstanceContextProvider; |
| | | 1034 | | |
| | 513 | 1035 | | if (!InstanceContextProviderBase.IsProviderSessionful(provider) && |
| | 513 | 1036 | | !InstanceContextProviderBase.IsProviderSingleton(provider)) |
| | | 1037 | | { |
| | 13 | 1038 | | IChannel proxy = rpc.Channel.Proxy as IChannel; |
| | 13 | 1039 | | if (!rpc.InstanceContext.IncomingChannels.Contains(proxy)) |
| | | 1040 | | { |
| | 13 | 1041 | | channelDispatcher.Channels.Add(proxy); |
| | | 1042 | | } |
| | | 1043 | | } |
| | | 1044 | | |
| | | 1045 | | // TODO: Do we need to keep track of pending channels with the new hosting model? |
| | | 1046 | | //channelDispatcher.PendingChannels.Remove(rpc.Channel.Binder.Channel); |
| | | 1047 | | } |
| | 2536 | 1048 | | } |
| | | 1049 | | |
| | | 1050 | | private interface IDemuxer |
| | | 1051 | | { |
| | | 1052 | | DispatchOperationRuntime GetOperation(ref Message request); |
| | | 1053 | | } |
| | | 1054 | | |
| | | 1055 | | private class ActionDemuxer : IDemuxer |
| | | 1056 | | { |
| | | 1057 | | private readonly HybridDictionary _map; |
| | | 1058 | | private DispatchOperationRuntime _unhandled; |
| | | 1059 | | |
| | 623 | 1060 | | internal ActionDemuxer() |
| | | 1061 | | { |
| | 623 | 1062 | | _map = new HybridDictionary(); |
| | 623 | 1063 | | } |
| | | 1064 | | |
| | | 1065 | | internal void Add(string action, DispatchOperationRuntime operation) |
| | | 1066 | | { |
| | 2844 | 1067 | | if (_map.Contains(action)) |
| | | 1068 | | { |
| | 0 | 1069 | | DispatchOperationRuntime existingOperation = (DispatchOperationRuntime)_map[action]; |
| | 0 | 1070 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 1071 | | } |
| | 2844 | 1072 | | _map.Add(action, operation); |
| | 2844 | 1073 | | } |
| | | 1074 | | |
| | | 1075 | | internal void SetUnhandled(DispatchOperationRuntime operation) |
| | | 1076 | | { |
| | 623 | 1077 | | _unhandled = operation; |
| | 623 | 1078 | | } |
| | | 1079 | | |
| | | 1080 | | public DispatchOperationRuntime GetOperation(ref Message request) |
| | | 1081 | | { |
| | 2503 | 1082 | | string action = request.Headers.Action; |
| | 2503 | 1083 | | if (action == null) |
| | | 1084 | | { |
| | 0 | 1085 | | action = MessageHeaders.WildcardAction; |
| | | 1086 | | } |
| | 2503 | 1087 | | DispatchOperationRuntime operation = (DispatchOperationRuntime)_map[action]; |
| | 2503 | 1088 | | if (operation != null) |
| | | 1089 | | { |
| | 2488 | 1090 | | return operation; |
| | | 1091 | | } |
| | | 1092 | | |
| | 15 | 1093 | | return _unhandled; |
| | | 1094 | | } |
| | | 1095 | | } |
| | | 1096 | | |
| | | 1097 | | private class CustomDemuxer : IDemuxer |
| | | 1098 | | { |
| | | 1099 | | private readonly Dictionary<string, DispatchOperationRuntime> _map; |
| | | 1100 | | private readonly IDispatchOperationSelector _selector; |
| | | 1101 | | private DispatchOperationRuntime _unhandled; |
| | | 1102 | | |
| | 38 | 1103 | | internal CustomDemuxer(IDispatchOperationSelector selector) |
| | | 1104 | | { |
| | 38 | 1105 | | _selector = selector; |
| | 38 | 1106 | | _map = new Dictionary<string, DispatchOperationRuntime>(); |
| | 38 | 1107 | | } |
| | | 1108 | | |
| | | 1109 | | internal void Add(string name, DispatchOperationRuntime operation) |
| | | 1110 | | { |
| | 169 | 1111 | | _map.Add(name, operation); |
| | 169 | 1112 | | } |
| | | 1113 | | |
| | | 1114 | | internal void SetUnhandled(DispatchOperationRuntime operation) |
| | | 1115 | | { |
| | 38 | 1116 | | _unhandled = operation; |
| | 38 | 1117 | | } |
| | | 1118 | | |
| | | 1119 | | public DispatchOperationRuntime GetOperation(ref Message request) |
| | | 1120 | | { |
| | 36 | 1121 | | string operationName = _selector.SelectOperation(ref request); |
| | 35 | 1122 | | if (_map.TryGetValue(operationName, out DispatchOperationRuntime operation)) |
| | | 1123 | | { |
| | 35 | 1124 | | return operation; |
| | | 1125 | | } |
| | | 1126 | | else |
| | | 1127 | | { |
| | 0 | 1128 | | return _unhandled; |
| | | 1129 | | } |
| | | 1130 | | } |
| | | 1131 | | } |
| | | 1132 | | } |
| | | 1133 | | } |