| | | 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.Reflection; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | using CoreWCF.Telemetry; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Dispatcher |
| | | 11 | | { |
| | | 12 | | internal class ImmutableClientRuntime |
| | | 13 | | { |
| | | 14 | | private readonly IChannelInitializer[] _channelInitializers; |
| | | 15 | | private readonly IClientMessageInspector[] _messageInspectors; |
| | | 16 | | private readonly Dictionary<string, ProxyOperationRuntime> _operations; |
| | | 17 | | private readonly Dictionary<string, ActionMetadata> _actionMappings; |
| | | 18 | | |
| | 1 | 19 | | internal ImmutableClientRuntime(ClientRuntime behavior) |
| | | 20 | | { |
| | 1 | 21 | | _channelInitializers = EmptyArray<IChannelInitializer>.ToArray(behavior.ChannelInitializers); |
| | | 22 | | //this.interactiveChannelInitializers = EmptyArray<IInteractiveChannelInitializer>.ToArray(behavior.Interact |
| | 1 | 23 | | _messageInspectors = EmptyArray<IClientMessageInspector>.ToArray(behavior.MessageInspectors); |
| | | 24 | | |
| | 1 | 25 | | OperationSelector = behavior.OperationSelector; |
| | 1 | 26 | | UseSynchronizationContext = behavior.UseSynchronizationContext; |
| | 1 | 27 | | ValidateMustUnderstand = behavior.ValidateMustUnderstand; |
| | | 28 | | |
| | 1 | 29 | | UnhandledProxyOperation = new ProxyOperationRuntime(behavior.UnhandledClientOperation, this); |
| | | 30 | | |
| | | 31 | | //this.addTransactionFlowProperties = behavior.AddTransactionFlowProperties; |
| | | 32 | | |
| | 1 | 33 | | _operations = new Dictionary<string, ProxyOperationRuntime>(); |
| | 1 | 34 | | _actionMappings = new Dictionary<string, ActionMetadata>(); |
| | 4 | 35 | | for (int i = 0; i < behavior.Operations.Count; i++) |
| | | 36 | | { |
| | 1 | 37 | | ClientOperation operation = behavior.Operations[i]; |
| | 1 | 38 | | ProxyOperationRuntime operationRuntime = new ProxyOperationRuntime(operation, this); |
| | 1 | 39 | | _operations.Add(operation.Name, operationRuntime); |
| | | 40 | | } |
| | | 41 | | |
| | 4 | 42 | | foreach (var clientOperation in behavior.ClientOperations) |
| | | 43 | | { |
| | 1 | 44 | | _actionMappings[clientOperation.Action] = new ActionMetadata( |
| | 1 | 45 | | contractName: $"{behavior.ContractNamespace}{behavior.ContractName}", |
| | 1 | 46 | | operationName: clientOperation.Name); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | |
| | 1 | 50 | | CorrelationCount = _messageInspectors.Length + behavior.MaxParameterInspectors; |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | internal int MessageInspectorCorrelationOffset |
| | | 54 | | { |
| | 2 | 55 | | get { return 0; } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | internal int ParameterInspectorCorrelationOffset |
| | | 59 | | { |
| | 2 | 60 | | get { return _messageInspectors.Length; } |
| | | 61 | | } |
| | | 62 | | |
| | 1 | 63 | | internal int CorrelationCount { get; } |
| | | 64 | | |
| | 3 | 65 | | internal IClientOperationSelector OperationSelector { get; } |
| | | 66 | | |
| | 0 | 67 | | internal ProxyOperationRuntime UnhandledProxyOperation { get; } |
| | | 68 | | |
| | 0 | 69 | | internal bool UseSynchronizationContext { get; } |
| | | 70 | | |
| | 2 | 71 | | internal bool ValidateMustUnderstand { get; set; } |
| | | 72 | | |
| | | 73 | | internal void AfterReceiveReply(ref ProxyRpc rpc) |
| | | 74 | | { |
| | 1 | 75 | | int offset = MessageInspectorCorrelationOffset; |
| | | 76 | | try |
| | | 77 | | { |
| | 2 | 78 | | for (int i = 0; i < _messageInspectors.Length; i++) |
| | | 79 | | { |
| | 0 | 80 | | _messageInspectors[i].AfterReceiveReply(ref rpc.Reply, rpc.Correlation[offset + i]); |
| | | 81 | | //if (TD.ClientMessageInspectorAfterReceiveInvokedIsEnabled()) |
| | | 82 | | //{ |
| | | 83 | | // TD.ClientMessageInspectorAfterReceiveInvoked(rpc.EventTraceActivity, this.messageInspectors[i] |
| | | 84 | | //} |
| | | 85 | | } |
| | 1 | 86 | | } |
| | 0 | 87 | | catch (Exception e) |
| | | 88 | | { |
| | 0 | 89 | | if (Fx.IsFatal(e)) |
| | | 90 | | { |
| | 0 | 91 | | throw; |
| | | 92 | | } |
| | 0 | 93 | | if (ErrorBehavior.ShouldRethrowClientSideExceptionAsIs(e)) |
| | | 94 | | { |
| | 0 | 95 | | throw; |
| | | 96 | | } |
| | 0 | 97 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 98 | | } |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | internal void BeforeSendRequest(ref ProxyRpc rpc) |
| | | 102 | | { |
| | 1 | 103 | | int offset = MessageInspectorCorrelationOffset; |
| | | 104 | | try |
| | | 105 | | { |
| | 1 | 106 | | rpc.Request.Properties.Add(TelemetryContextMessageProperty.Name, new TelemetryContextMessageProperty(_ac |
| | | 107 | | |
| | 2 | 108 | | for (int i = 0; i < _messageInspectors.Length; i++) |
| | | 109 | | { |
| | 0 | 110 | | rpc.Correlation[offset + i] = _messageInspectors[i].BeforeSendRequest(ref rpc.Request, (IClientChann |
| | | 111 | | //if (TD.ClientMessageInspectorBeforeSendInvokedIsEnabled()) |
| | | 112 | | //{ |
| | | 113 | | // TD.ClientMessageInspectorBeforeSendInvoked(rpc.EventTraceActivity, this.messageInspectors[i].G |
| | | 114 | | //} |
| | | 115 | | } |
| | 1 | 116 | | } |
| | 0 | 117 | | catch (Exception e) |
| | | 118 | | { |
| | 0 | 119 | | if (Fx.IsFatal(e)) |
| | | 120 | | { |
| | 0 | 121 | | throw; |
| | | 122 | | } |
| | 0 | 123 | | if (ErrorBehavior.ShouldRethrowClientSideExceptionAsIs(e)) |
| | | 124 | | { |
| | 0 | 125 | | throw; |
| | | 126 | | } |
| | 0 | 127 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | //if (this.addTransactionFlowProperties) |
| | | 131 | | //{ |
| | | 132 | | // SendTransaction(ref rpc); |
| | | 133 | | //} |
| | 1 | 134 | | } |
| | | 135 | | |
| | | 136 | | // this should not be inlined, since we want to JIT the reference to System.Transactions |
| | | 137 | | // only if transactions are being flowed. |
| | | 138 | | //[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] |
| | | 139 | | //static void SendTransaction(ref ProxyRpc rpc) |
| | | 140 | | //{ |
| | | 141 | | // System.ServiceModel.Channels.TransactionFlowProperty.Set(Transaction.Current, rpc.Request); |
| | | 142 | | //} |
| | | 143 | | |
| | | 144 | | internal void InitializeChannel(IClientChannel channel) |
| | | 145 | | { |
| | | 146 | | try |
| | | 147 | | { |
| | 0 | 148 | | for (int i = 0; i < _channelInitializers.Length; ++i) |
| | | 149 | | { |
| | 0 | 150 | | _channelInitializers[i].Initialize(channel); |
| | | 151 | | } |
| | 0 | 152 | | } |
| | 0 | 153 | | catch (Exception e) |
| | | 154 | | { |
| | 0 | 155 | | if (Fx.IsFatal(e)) |
| | | 156 | | { |
| | 0 | 157 | | throw; |
| | | 158 | | } |
| | 0 | 159 | | if (ErrorBehavior.ShouldRethrowClientSideExceptionAsIs(e)) |
| | | 160 | | { |
| | 0 | 161 | | throw; |
| | | 162 | | } |
| | 0 | 163 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 164 | | } |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | internal ProxyOperationRuntime GetOperation(MethodBase methodBase, object[] args, out bool canCacheResult) |
| | | 168 | | { |
| | 1 | 169 | | if (OperationSelector == null) |
| | | 170 | | { |
| | 0 | 171 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException |
| | 0 | 172 | | (SR.Format(SR.SFxNeedProxyBehaviorOperationSelector2, methodBase |
| | 0 | 173 | | methodBase.DeclaringType.Name))); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | try |
| | | 177 | | { |
| | 1 | 178 | | if (OperationSelector.AreParametersRequiredForSelection) |
| | | 179 | | { |
| | 0 | 180 | | canCacheResult = false; |
| | | 181 | | } |
| | | 182 | | else |
| | | 183 | | { |
| | 1 | 184 | | args = null; |
| | 1 | 185 | | canCacheResult = true; |
| | | 186 | | } |
| | 1 | 187 | | string operationName = OperationSelector.SelectOperation(methodBase, args); |
| | 1 | 188 | | if ((operationName != null) && _operations.TryGetValue(operationName, out ProxyOperationRuntime operatio |
| | | 189 | | { |
| | 1 | 190 | | return operation; |
| | | 191 | | } |
| | | 192 | | else |
| | | 193 | | { |
| | | 194 | | // did not find the right operation, will not know how |
| | | 195 | | // to invoke the method. |
| | 0 | 196 | | return null; |
| | | 197 | | } |
| | | 198 | | } |
| | 0 | 199 | | catch (Exception e) |
| | | 200 | | { |
| | 0 | 201 | | if (Fx.IsFatal(e)) |
| | | 202 | | { |
| | 0 | 203 | | throw; |
| | | 204 | | } |
| | 0 | 205 | | if (ErrorBehavior.ShouldRethrowClientSideExceptionAsIs(e)) |
| | | 206 | | { |
| | 0 | 207 | | throw; |
| | | 208 | | } |
| | 0 | 209 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); |
| | | 210 | | } |
| | 1 | 211 | | } |
| | | 212 | | |
| | | 213 | | internal ProxyOperationRuntime GetOperationByName(string operationName) |
| | | 214 | | { |
| | 0 | 215 | | if (_operations.TryGetValue(operationName, out ProxyOperationRuntime operation)) |
| | | 216 | | { |
| | 0 | 217 | | return operation; |
| | | 218 | | } |
| | | 219 | | else |
| | | 220 | | { |
| | 0 | 221 | | return null; |
| | | 222 | | } |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | } |