| | | 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.Concurrent; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Diagnostics.Contracts; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Reflection; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using CoreWCF.Configuration; |
| | | 13 | | using CoreWCF.Description; |
| | | 14 | | using CoreWCF.Dispatcher; |
| | | 15 | | using CoreWCF.Runtime; |
| | | 16 | | |
| | | 17 | | namespace CoreWCF.Channels |
| | | 18 | | { |
| | | 19 | | [Browsable(false)] |
| | | 20 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 21 | | [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your cod |
| | | 22 | | public class ServiceChannelProxy : DispatchProxy, ICommunicationObject, IChannel, IClientChannel, IOutputChannel, IR |
| | | 23 | | { |
| | | 24 | | private const string activityIdSlotName = "E2ETrace.ActivityID"; |
| | | 25 | | private Type _proxiedType; |
| | | 26 | | private ServiceChannel _serviceChannel; |
| | | 27 | | private ImmutableClientRuntime _proxyRuntime; |
| | | 28 | | private MethodDataCache _methodDataCache; |
| | | 29 | | |
| | | 30 | | // ServiceChannelProxy serves 2 roles. It is the TChannel proxy called by the client, |
| | | 31 | | // and it is also the handler of those calls that dispatches them to the appropriate service channel. |
| | | 32 | | // In .Net Remoting terms, it is conceptually the same as a RealProxy and a TransparentProxy combined. |
| | | 33 | | internal static TChannel CreateProxy<TChannel>(MessageDirection direction, ServiceChannel serviceChannel) |
| | | 34 | | { |
| | 1 | 35 | | TChannel proxy = Create<TChannel, ServiceChannelProxy>(); |
| | 1 | 36 | | if (proxy == null) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Fai |
| | | 39 | | } |
| | | 40 | | |
| | 1 | 41 | | ServiceChannelProxy channelProxy = (ServiceChannelProxy)(object)proxy; |
| | 1 | 42 | | channelProxy._proxiedType = typeof(TChannel); |
| | 1 | 43 | | channelProxy._serviceChannel = serviceChannel; |
| | 1 | 44 | | channelProxy._proxyRuntime = serviceChannel.ClientRuntime.GetRuntime(); |
| | 1 | 45 | | channelProxy._methodDataCache = new MethodDataCache(); |
| | 1 | 46 | | return proxy; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | //Workaround is to set the activityid in remoting call's LogicalCallContext |
| | | 50 | | |
| | | 51 | | // Override ToString() to reveal only the expected proxy type, not the generated one |
| | | 52 | | public sealed override string ToString() |
| | | 53 | | { |
| | 0 | 54 | | return _proxiedType.ToString(); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | private MethodData GetMethodData(MethodCall methodCall) |
| | | 58 | | { |
| | 1 | 59 | | MethodBase method = methodCall.MethodBase; |
| | 1 | 60 | | if (_methodDataCache.TryGetMethodData(method, out MethodData methodData)) |
| | | 61 | | { |
| | 0 | 62 | | return methodData; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | bool canCacheMessageData; |
| | | 66 | | |
| | 1 | 67 | | Type declaringType = method.DeclaringType; |
| | 1 | 68 | | if (declaringType == typeof(object) && method == typeof(object).GetMethod("GetType")) |
| | | 69 | | { |
| | 0 | 70 | | canCacheMessageData = true; |
| | 0 | 71 | | methodData = new MethodData(method, MethodType.GetType); |
| | | 72 | | } |
| | 1 | 73 | | else if (declaringType.IsAssignableFrom(_serviceChannel.GetType())) |
| | | 74 | | { |
| | 0 | 75 | | canCacheMessageData = true; |
| | 0 | 76 | | methodData = new MethodData(method, MethodType.Channel); |
| | | 77 | | } |
| | | 78 | | else |
| | | 79 | | { |
| | 1 | 80 | | ProxyOperationRuntime operation = _proxyRuntime.GetOperation(method, methodCall.Args, out canCacheMessag |
| | | 81 | | |
| | 1 | 82 | | if (operation == null) |
| | | 83 | | { |
| | 0 | 84 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR.SFx |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | MethodType methodType; |
| | | 88 | | |
| | 1 | 89 | | if (operation.IsTaskCall(methodCall)) |
| | | 90 | | { |
| | 0 | 91 | | methodType = MethodType.TaskService; |
| | | 92 | | } |
| | 1 | 93 | | else if (operation.IsSyncCall(methodCall)) |
| | | 94 | | { |
| | 1 | 95 | | methodType = MethodType.Service; |
| | | 96 | | } |
| | 0 | 97 | | else if (operation.IsBeginCall(methodCall)) |
| | | 98 | | { |
| | 0 | 99 | | methodType = MethodType.BeginService; |
| | | 100 | | } |
| | | 101 | | else |
| | | 102 | | { |
| | 0 | 103 | | methodType = MethodType.EndService; |
| | | 104 | | } |
| | | 105 | | |
| | 1 | 106 | | methodData = new MethodData(method, methodType, operation); |
| | | 107 | | } |
| | | 108 | | |
| | 1 | 109 | | if (canCacheMessageData) |
| | | 110 | | { |
| | 1 | 111 | | _methodDataCache.SetMethodData(methodData); |
| | | 112 | | } |
| | | 113 | | |
| | 1 | 114 | | return methodData; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | internal ServiceChannel GetServiceChannel() |
| | | 118 | | { |
| | 2 | 119 | | return _serviceChannel; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | protected sealed override object Invoke(MethodInfo targetMethod, object[] args) |
| | | 123 | | { |
| | 1 | 124 | | if (args == null) |
| | | 125 | | { |
| | 0 | 126 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(args)); |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | if (targetMethod == null) |
| | | 130 | | { |
| | 0 | 131 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Inv |
| | | 132 | | } |
| | | 133 | | |
| | 1 | 134 | | MethodCall methodCall = new MethodCall(targetMethod, args); |
| | 1 | 135 | | MethodData methodData = GetMethodData(methodCall); |
| | | 136 | | |
| | 1 | 137 | | switch (methodData.MethodType) |
| | | 138 | | { |
| | | 139 | | case MethodType.Service: |
| | 1 | 140 | | return InvokeService(methodCall, methodData.Operation); |
| | | 141 | | case MethodType.BeginService: |
| | 0 | 142 | | return InvokeBeginService(methodCall, methodData.Operation); |
| | | 143 | | case MethodType.EndService: |
| | 0 | 144 | | return InvokeEndService(methodCall, methodData.Operation); |
| | | 145 | | case MethodType.TaskService: |
| | 0 | 146 | | return InvokeTaskService(methodCall, methodData.Operation); |
| | | 147 | | case MethodType.Channel: |
| | 0 | 148 | | return InvokeChannel(methodCall); |
| | | 149 | | case MethodType.GetType: |
| | 0 | 150 | | return InvokeGetType(methodCall); |
| | | 151 | | default: |
| | | 152 | | Fx.Assert("Invalid proxy method type"); |
| | 0 | 153 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(string.Forma |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | internal static class TaskCreator |
| | | 158 | | { |
| | | 159 | | public static Task CreateTask(ServiceChannel channel, MethodCall methodCall, ProxyOperationRuntime operation |
| | | 160 | | { |
| | 0 | 161 | | if (operation.TaskTResult == ServiceReflector.VoidType) |
| | | 162 | | { |
| | 0 | 163 | | return CreateTask(channel, operation, methodCall.Args); |
| | | 164 | | } |
| | 0 | 165 | | return CreateGenericTask(channel, operation, methodCall.Args); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | private static Task CreateGenericTask(ServiceChannel channel, ProxyOperationRuntime operation, object[] inpu |
| | | 169 | | { |
| | 0 | 170 | | TaskCompletionSourceProxy tcsp = new TaskCompletionSourceProxy(operation.TaskTResult); |
| | 0 | 171 | | Action<Task<object>, object> completeCallDelegate = (antecedent, obj) => |
| | 0 | 172 | | { |
| | 0 | 173 | | var tcsProxy = obj as TaskCompletionSourceProxy; |
| | 0 | 174 | | Contract.Assert(tcsProxy != null); |
| | 0 | 175 | | if (antecedent.IsFaulted) |
| | 0 | 176 | | { |
| | 0 | 177 | | tcsProxy.TrySetException(antecedent.Exception.InnerException); |
| | 0 | 178 | | } |
| | 0 | 179 | | else if (antecedent.IsCanceled) |
| | 0 | 180 | | { |
| | 0 | 181 | | tcsProxy.TrySetCanceled(); |
| | 0 | 182 | | } |
| | 0 | 183 | | else |
| | 0 | 184 | | { |
| | 0 | 185 | | tcsProxy.TrySetResult(antecedent.Result); |
| | 0 | 186 | | } |
| | 0 | 187 | | }; |
| | | 188 | | |
| | | 189 | | try |
| | | 190 | | { |
| | 0 | 191 | | channel.CallAsync(operation.Action, operation.IsOneWay, operation, inputParameters, |
| | 0 | 192 | | Array.Empty<object>()).ContinueWith(completeCallDelegate, tcsp); |
| | 0 | 193 | | } |
| | | 194 | | #pragma warning disable CA1031 // Do not catch general exception types - copying all exceptions to TaskCompeletionSource |
| | 0 | 195 | | catch (Exception e) |
| | | 196 | | { |
| | 0 | 197 | | tcsp.TrySetException(e); |
| | 0 | 198 | | } |
| | | 199 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 200 | | |
| | 0 | 201 | | return tcsp.Task; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private static Task CreateTask(ServiceChannel channel, ProxyOperationRuntime operation, object[] inputParame |
| | | 205 | | { |
| | 0 | 206 | | TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); |
| | | 207 | | |
| | 0 | 208 | | Action<Task<object>, object> completeCallDelegate = (antecedent, obj) => |
| | 0 | 209 | | { |
| | 0 | 210 | | var tcsObj = obj as TaskCompletionSource<object>; |
| | 0 | 211 | | Contract.Assert(tcsObj != null); |
| | 0 | 212 | | if (antecedent.IsFaulted) |
| | 0 | 213 | | { |
| | 0 | 214 | | tcsObj.TrySetException(antecedent.Exception.InnerException); |
| | 0 | 215 | | } |
| | 0 | 216 | | else if (antecedent.IsCanceled) |
| | 0 | 217 | | { |
| | 0 | 218 | | tcsObj.TrySetCanceled(); |
| | 0 | 219 | | } |
| | 0 | 220 | | else |
| | 0 | 221 | | { |
| | 0 | 222 | | tcsObj.TrySetResult(antecedent.Result); |
| | 0 | 223 | | } |
| | 0 | 224 | | }; |
| | | 225 | | |
| | | 226 | | |
| | | 227 | | try |
| | | 228 | | { |
| | 0 | 229 | | channel.CallAsync(operation.Action, operation.IsOneWay, operation, inputParameters, |
| | 0 | 230 | | Array.Empty<object>()).ContinueWith(completeCallDelegate, tcs); |
| | 0 | 231 | | } |
| | | 232 | | #pragma warning disable CA1031 // Do not catch general exception types - copying all exceptions to TaskCompeletionSource |
| | | 233 | | |
| | 0 | 234 | | catch (Exception e) |
| | | 235 | | { |
| | 0 | 236 | | tcs.TrySetException(e); |
| | 0 | 237 | | } |
| | | 238 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 239 | | |
| | 0 | 240 | | return tcs.Task; |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private class TaskCompletionSourceProxy |
| | | 245 | | { |
| | | 246 | | private readonly TaskCompletionSourceInfo _tcsInfo; |
| | | 247 | | private readonly object _tcsInstance; |
| | | 248 | | |
| | 0 | 249 | | public TaskCompletionSourceProxy(Type resultType) |
| | | 250 | | { |
| | 0 | 251 | | _tcsInfo = TaskCompletionSourceInfo.GetTaskCompletionSourceInfo(resultType); |
| | 0 | 252 | | _tcsInstance = Activator.CreateInstance(_tcsInfo.GenericType); |
| | 0 | 253 | | } |
| | | 254 | | |
| | 0 | 255 | | public Task Task { get { return (Task)_tcsInfo.TaskProperty.GetValue(_tcsInstance); } } |
| | | 256 | | |
| | | 257 | | public bool TrySetResult(object result) |
| | | 258 | | { |
| | 0 | 259 | | return (bool)_tcsInfo.TrySetResultMethod.Invoke(_tcsInstance, new object[] { result }); |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | public bool TrySetException(Exception exception) |
| | | 263 | | { |
| | 0 | 264 | | return (bool)_tcsInfo.TrySetExceptionMethod.Invoke(_tcsInstance, new object[] { exception }); |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | public bool TrySetCanceled() |
| | | 268 | | { |
| | 0 | 269 | | return (bool)_tcsInfo.TrySetCanceledMethod.Invoke(_tcsInstance, Array.Empty<object>()); |
| | | 270 | | } |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private class TaskCompletionSourceInfo |
| | | 274 | | { |
| | 0 | 275 | | private static readonly ConcurrentDictionary<Type, TaskCompletionSourceInfo> s_cache = new ConcurrentDiction |
| | | 276 | | |
| | 0 | 277 | | public TaskCompletionSourceInfo(Type resultType) |
| | | 278 | | { |
| | 0 | 279 | | ResultType = resultType; |
| | 0 | 280 | | Type tcsType = typeof(TaskCompletionSource<>); |
| | 0 | 281 | | GenericType = tcsType.MakeGenericType(new Type[] { resultType }); |
| | 0 | 282 | | TaskProperty = GenericType.GetTypeInfo().GetDeclaredProperty("Task"); |
| | 0 | 283 | | TrySetResultMethod = GenericType.GetTypeInfo().GetDeclaredMethod("TrySetResult"); |
| | 0 | 284 | | TrySetExceptionMethod = GenericType.GetRuntimeMethod("TrySetException", new Type[] { typeof(Exception) } |
| | 0 | 285 | | TrySetCanceledMethod = GenericType.GetRuntimeMethod("TrySetCanceled", Array.Empty<Type>()); |
| | 0 | 286 | | } |
| | | 287 | | |
| | 0 | 288 | | public Type ResultType { get; private set; } |
| | 0 | 289 | | public Type GenericType { get; private set; } |
| | 0 | 290 | | public PropertyInfo TaskProperty { get; private set; } |
| | 0 | 291 | | public MethodInfo TrySetResultMethod { get; private set; } |
| | 0 | 292 | | public MethodInfo TrySetExceptionMethod { get; set; } |
| | 0 | 293 | | public MethodInfo TrySetCanceledMethod { get; set; } |
| | | 294 | | |
| | | 295 | | public static TaskCompletionSourceInfo GetTaskCompletionSourceInfo(Type resultType) |
| | | 296 | | { |
| | 0 | 297 | | return s_cache.GetOrAdd(resultType, t => new TaskCompletionSourceInfo(t)); |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | private object InvokeTaskService(MethodCall methodCall, ProxyOperationRuntime operation) |
| | | 302 | | { |
| | 0 | 303 | | Task task = TaskCreator.CreateTask(_serviceChannel, methodCall, operation); |
| | 0 | 304 | | return task; |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | private object InvokeChannel(MethodCall methodCall) |
| | | 308 | | { |
| | | 309 | | //string activityName = null; |
| | | 310 | | //ActivityType activityType = ActivityType.Unknown; |
| | | 311 | | //if (DiagnosticUtility.ShouldUseActivity) |
| | | 312 | | //{ |
| | | 313 | | // if (ServiceModelActivity.Current == null || |
| | | 314 | | // ServiceModelActivity.Current.ActivityType != ActivityType.Close) |
| | | 315 | | // { |
| | | 316 | | // MethodData methodData = this.GetMethodData(methodCall); |
| | | 317 | | // if (methodData.MethodBase.DeclaringType == typeof(System.ServiceModel.ICommunicationObject) |
| | | 318 | | // && methodData.MethodBase.Name.Equals("Close", StringComparison.Ordinal)) |
| | | 319 | | // { |
| | | 320 | | // activityName = SR.Format(SR.ActivityClose, _serviceChannel.GetType().FullName); |
| | | 321 | | // activityType = ActivityType.Close; |
| | | 322 | | // } |
| | | 323 | | // } |
| | | 324 | | //} |
| | | 325 | | |
| | | 326 | | //using (ServiceModelActivity activity = string.IsNullOrEmpty(activityName) ? null : ServiceModelActivity.Cr |
| | | 327 | | //{ |
| | | 328 | | // if (DiagnosticUtility.ShouldUseActivity) |
| | | 329 | | // { |
| | | 330 | | // ServiceModelActivity.Start(activity, activityName, activityType); |
| | | 331 | | // } |
| | 0 | 332 | | return ExecuteMessage(_serviceChannel, methodCall); |
| | | 333 | | //} |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | private object InvokeGetType(MethodCall methodCall) |
| | | 337 | | { |
| | 0 | 338 | | return _proxiedType; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | private object InvokeBeginService(MethodCall methodCall, ProxyOperationRuntime operation) |
| | | 342 | | { |
| | 0 | 343 | | object[] ins = operation.MapAsyncBeginInputs(methodCall, out AsyncCallback callback, out object asyncState); |
| | 0 | 344 | | object ret = _serviceChannel.BeginCall(operation.Action, operation.IsOneWay, operation, ins, callback, async |
| | 0 | 345 | | return ret; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | private object InvokeEndService(MethodCall methodCall, ProxyOperationRuntime operation) |
| | | 349 | | { |
| | 0 | 350 | | operation.MapAsyncEndInputs(methodCall, out IAsyncResult result, out object[] outs); |
| | 0 | 351 | | object ret = _serviceChannel.EndCall(operation.Action, outs, result); |
| | 0 | 352 | | operation.MapAsyncOutputs(methodCall, outs, ref ret); |
| | 0 | 353 | | return ret; |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private object InvokeService(MethodCall methodCall, ProxyOperationRuntime operation) |
| | | 357 | | { |
| | 1 | 358 | | object[] ins = operation.MapSyncInputs(methodCall, out object[] outs); |
| | | 359 | | object ret; |
| | 1 | 360 | | using (TaskHelpers.RunTaskContinuationsOnOurThreads()) |
| | | 361 | | { |
| | 1 | 362 | | ret = _serviceChannel.CallAsync(operation.Action, operation.IsOneWay, operation, ins, outs).GetAwaiter() |
| | 1 | 363 | | } |
| | 1 | 364 | | operation.MapSyncOutputs(methodCall, outs, ref ret); |
| | 1 | 365 | | return ret; |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | private object ExecuteMessage(object target, MethodCall methodCall) |
| | | 369 | | { |
| | 0 | 370 | | MethodBase targetMethod = methodCall.MethodBase; |
| | | 371 | | |
| | 0 | 372 | | object[] args = methodCall.Args; |
| | | 373 | | object returnValue; |
| | | 374 | | try |
| | | 375 | | { |
| | 0 | 376 | | returnValue = targetMethod.Invoke(target, args); |
| | 0 | 377 | | } |
| | | 378 | | catch (TargetInvocationException e) |
| | | 379 | | { |
| | 0 | 380 | | throw e.InnerException; |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | return returnValue; |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | internal class MethodDataCache |
| | | 387 | | { |
| | | 388 | | private MethodData[] _methodDatas; |
| | | 389 | | |
| | 1 | 390 | | public MethodDataCache() |
| | | 391 | | { |
| | 1 | 392 | | _methodDatas = new MethodData[4]; |
| | 1 | 393 | | } |
| | | 394 | | |
| | | 395 | | private object ThisLock |
| | | 396 | | { |
| | 2 | 397 | | get { return this; } |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | public bool TryGetMethodData(MethodBase method, out MethodData methodData) |
| | | 401 | | { |
| | 1 | 402 | | lock (ThisLock) |
| | | 403 | | { |
| | 1 | 404 | | MethodData[] methodDatas = _methodDatas; |
| | 1 | 405 | | int index = FindMethod(methodDatas, method); |
| | 1 | 406 | | if (index >= 0) |
| | | 407 | | { |
| | 0 | 408 | | methodData = methodDatas[index]; |
| | 0 | 409 | | return true; |
| | | 410 | | } |
| | | 411 | | else |
| | | 412 | | { |
| | 1 | 413 | | methodData = new MethodData(); |
| | 1 | 414 | | return false; |
| | | 415 | | } |
| | | 416 | | } |
| | 1 | 417 | | } |
| | | 418 | | |
| | | 419 | | private static int FindMethod(MethodData[] methodDatas, MethodBase methodToFind) |
| | | 420 | | { |
| | 4 | 421 | | for (int i = 0; i < methodDatas.Length; i++) |
| | | 422 | | { |
| | 2 | 423 | | MethodBase method = methodDatas[i].MethodBase; |
| | 2 | 424 | | if (method == null) |
| | | 425 | | { |
| | | 426 | | break; |
| | | 427 | | } |
| | 0 | 428 | | if (method == methodToFind) |
| | | 429 | | { |
| | 0 | 430 | | return i; |
| | | 431 | | } |
| | | 432 | | } |
| | 2 | 433 | | return -1; |
| | | 434 | | } |
| | | 435 | | |
| | | 436 | | public void SetMethodData(MethodData methodData) |
| | | 437 | | { |
| | 1 | 438 | | lock (ThisLock) |
| | | 439 | | { |
| | 1 | 440 | | int index = FindMethod(_methodDatas, methodData.MethodBase); |
| | 1 | 441 | | if (index < 0) |
| | | 442 | | { |
| | 2 | 443 | | for (int i = 0; i < _methodDatas.Length; i++) |
| | | 444 | | { |
| | 1 | 445 | | if (_methodDatas[i].MethodBase == null) |
| | | 446 | | { |
| | 1 | 447 | | _methodDatas[i] = methodData; |
| | 1 | 448 | | return; |
| | | 449 | | } |
| | | 450 | | } |
| | 0 | 451 | | MethodData[] newMethodDatas = new MethodData[_methodDatas.Length * 2]; |
| | 0 | 452 | | Array.Copy(_methodDatas, newMethodDatas, _methodDatas.Length); |
| | 0 | 453 | | newMethodDatas[_methodDatas.Length] = methodData; |
| | 0 | 454 | | _methodDatas = newMethodDatas; |
| | | 455 | | } |
| | 0 | 456 | | } |
| | 1 | 457 | | } |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | internal enum MethodType |
| | | 461 | | { |
| | | 462 | | Service, |
| | | 463 | | BeginService, |
| | | 464 | | EndService, |
| | | 465 | | Channel, |
| | | 466 | | Object, |
| | | 467 | | GetType, |
| | | 468 | | TaskService |
| | | 469 | | } |
| | | 470 | | |
| | | 471 | | internal struct MethodData |
| | | 472 | | { |
| | | 473 | | public MethodData(MethodBase methodBase, MethodType methodType) |
| | 0 | 474 | | : this(methodBase, methodType, null) |
| | | 475 | | { |
| | 0 | 476 | | } |
| | | 477 | | |
| | | 478 | | public MethodData(MethodBase methodBase, MethodType methodType, ProxyOperationRuntime operation) |
| | | 479 | | { |
| | 1 | 480 | | MethodBase = methodBase; |
| | 1 | 481 | | MethodType = methodType; |
| | 1 | 482 | | Operation = operation; |
| | 1 | 483 | | } |
| | | 484 | | |
| | 4 | 485 | | public MethodBase MethodBase { get; } |
| | | 486 | | |
| | 1 | 487 | | public MethodType MethodType { get; } |
| | | 488 | | |
| | 1 | 489 | | public ProxyOperationRuntime Operation { get; } |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | #region Channel interfaces |
| | | 493 | | // These channel methods exist only to implement additional channel interfaces for ServiceChannelProxy. |
| | | 494 | | // This is required because clients can down-cast typed proxies to the these channel interfaces. |
| | | 495 | | // On the desktop, the .Net Remoting layer allowed that type cast, and subsequent calls against the |
| | | 496 | | // interface went back through the RealProxy and invoked the underlying ServiceChannel. |
| | | 497 | | // Net Native and CoreClr do not have .Net Remoting and therefore cannot use that mechanism. |
| | | 498 | | // But because typed proxies derive from ServiceChannelProxy, implementing these interfaces |
| | | 499 | | // on ServiceChannelProxy permits casting the typed proxy to these interfaces. |
| | | 500 | | // All interface implementations delegate directly to the underlying ServiceChannel. |
| | | 501 | | T IChannel.GetProperty<T>() |
| | | 502 | | { |
| | 0 | 503 | | return _serviceChannel.GetProperty<T>(); |
| | | 504 | | } |
| | | 505 | | |
| | | 506 | | CommunicationState ICommunicationObject.State |
| | | 507 | | { |
| | 0 | 508 | | get { return _serviceChannel.State; } |
| | | 509 | | } |
| | | 510 | | |
| | | 511 | | event EventHandler ICommunicationObject.Closed |
| | | 512 | | { |
| | 4 | 513 | | add { _serviceChannel.Closed += value; } |
| | 4 | 514 | | remove { _serviceChannel.Closed -= value; } |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | event EventHandler ICommunicationObject.Closing |
| | | 518 | | { |
| | 0 | 519 | | add { _serviceChannel.Closing += value; } |
| | 0 | 520 | | remove { _serviceChannel.Closing -= value; } |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | event EventHandler ICommunicationObject.Faulted |
| | | 524 | | { |
| | 0 | 525 | | add { _serviceChannel.Faulted += value; } |
| | 0 | 526 | | remove { _serviceChannel.Faulted -= value; } |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | event EventHandler ICommunicationObject.Opened |
| | | 530 | | { |
| | 0 | 531 | | add { _serviceChannel.Opened += value; } |
| | 0 | 532 | | remove { _serviceChannel.Opened -= value; } |
| | | 533 | | } |
| | | 534 | | |
| | | 535 | | event EventHandler ICommunicationObject.Opening |
| | | 536 | | { |
| | 0 | 537 | | add { _serviceChannel.Opening += value; } |
| | 0 | 538 | | remove { _serviceChannel.Opening -= value; } |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | void ICommunicationObject.Abort() |
| | | 542 | | { |
| | 0 | 543 | | _serviceChannel.Abort(); |
| | 0 | 544 | | } |
| | | 545 | | |
| | | 546 | | Task ICommunicationObject.CloseAsync() |
| | | 547 | | { |
| | 0 | 548 | | return _serviceChannel.CloseAsync(); |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | Task ICommunicationObject.CloseAsync(CancellationToken token) |
| | | 552 | | { |
| | 0 | 553 | | return _serviceChannel.CloseAsync(token); |
| | | 554 | | } |
| | | 555 | | |
| | | 556 | | Task ICommunicationObject.OpenAsync() |
| | | 557 | | { |
| | 0 | 558 | | return _serviceChannel.OpenAsync(); |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | Task ICommunicationObject.OpenAsync(CancellationToken token) |
| | | 562 | | { |
| | 0 | 563 | | return _serviceChannel.OpenAsync(token); |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | //Uri IClientChannel.Via |
| | | 567 | | //{ |
| | | 568 | | // get { return _serviceChannel.Via; } |
| | | 569 | | //} |
| | | 570 | | |
| | | 571 | | event EventHandler<UnknownMessageReceivedEventArgs> IClientChannel.UnknownMessageReceived |
| | | 572 | | { |
| | 0 | 573 | | add { ((IClientChannel)_serviceChannel).UnknownMessageReceived += value; } |
| | 0 | 574 | | remove { ((IClientChannel)_serviceChannel).UnknownMessageReceived -= value; } |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | void IDisposable.Dispose() |
| | | 578 | | { |
| | 0 | 579 | | ((IClientChannel)_serviceChannel).Dispose(); |
| | 0 | 580 | | } |
| | | 581 | | |
| | | 582 | | //bool IContextChannel.AllowOutputBatching |
| | | 583 | | //{ |
| | | 584 | | // get |
| | | 585 | | // { |
| | | 586 | | // return ((IContextChannel)_serviceChannel).AllowOutputBatching; |
| | | 587 | | // } |
| | | 588 | | // set |
| | | 589 | | // { |
| | | 590 | | // ((IContextChannel)_serviceChannel).AllowOutputBatching = value; |
| | | 591 | | // } |
| | | 592 | | //} |
| | | 593 | | |
| | | 594 | | IInputSession IContextChannel.InputSession |
| | | 595 | | { |
| | 0 | 596 | | get { return ((IContextChannel)_serviceChannel).InputSession; } |
| | | 597 | | } |
| | | 598 | | |
| | | 599 | | EndpointAddress IContextChannel.LocalAddress |
| | | 600 | | { |
| | 0 | 601 | | get { return ((IContextChannel)_serviceChannel).LocalAddress; } |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | TimeSpan IContextChannel.OperationTimeout |
| | | 605 | | { |
| | | 606 | | get |
| | | 607 | | { |
| | 0 | 608 | | return ((IContextChannel)_serviceChannel).OperationTimeout; |
| | | 609 | | } |
| | | 610 | | set |
| | | 611 | | { |
| | 0 | 612 | | ((IContextChannel)_serviceChannel).OperationTimeout = value; |
| | 0 | 613 | | } |
| | | 614 | | } |
| | | 615 | | |
| | | 616 | | IOutputSession IContextChannel.OutputSession |
| | | 617 | | { |
| | 0 | 618 | | get { return ((IContextChannel)_serviceChannel).OutputSession; } |
| | | 619 | | } |
| | | 620 | | |
| | | 621 | | EndpointAddress IOutputChannel.RemoteAddress |
| | | 622 | | { |
| | 0 | 623 | | get { return ((IContextChannel)_serviceChannel).RemoteAddress; } |
| | | 624 | | } |
| | | 625 | | |
| | | 626 | | Uri IOutputChannel.Via |
| | | 627 | | { |
| | 0 | 628 | | get { return _serviceChannel.Via; } |
| | | 629 | | } |
| | | 630 | | |
| | | 631 | | EndpointAddress IContextChannel.RemoteAddress |
| | | 632 | | { |
| | 0 | 633 | | get { return ((IContextChannel)_serviceChannel).RemoteAddress; } |
| | | 634 | | } |
| | | 635 | | |
| | | 636 | | string IContextChannel.SessionId |
| | | 637 | | { |
| | 0 | 638 | | get { return ((IContextChannel)_serviceChannel).SessionId; } |
| | | 639 | | } |
| | | 640 | | |
| | | 641 | | IExtensionCollection<IContextChannel> IExtensibleObject<IContextChannel>.Extensions |
| | | 642 | | { |
| | 0 | 643 | | get { return ((IContextChannel)_serviceChannel).Extensions; } |
| | | 644 | | } |
| | | 645 | | |
| | | 646 | | Task IOutputChannel.SendAsync(Message message) |
| | | 647 | | { |
| | 0 | 648 | | return _serviceChannel.SendAsync(message); |
| | | 649 | | } |
| | | 650 | | |
| | | 651 | | Task IOutputChannel.SendAsync(Message message, CancellationToken token) |
| | | 652 | | { |
| | 0 | 653 | | return _serviceChannel.SendAsync(message, token); |
| | | 654 | | } |
| | | 655 | | |
| | | 656 | | Task<Message> IRequestChannel.RequestAsync(Message message) |
| | | 657 | | { |
| | 0 | 658 | | return _serviceChannel.RequestAsync(message); |
| | | 659 | | } |
| | | 660 | | |
| | | 661 | | Task<Message> IRequestChannel.RequestAsync(Message message, CancellationToken token) |
| | | 662 | | { |
| | 0 | 663 | | return _serviceChannel.RequestAsync(message, token); |
| | | 664 | | } |
| | | 665 | | |
| | | 666 | | Task IDuplexContextChannel.CloseOutputSessionAsync(CancellationToken token) |
| | | 667 | | { |
| | 0 | 668 | | return ((IDuplexContextChannel)_serviceChannel).CloseOutputSessionAsync(token); |
| | | 669 | | } |
| | | 670 | | |
| | | 671 | | EndpointAddress IRequestChannel.RemoteAddress |
| | | 672 | | { |
| | 0 | 673 | | get { return _serviceChannel.RemoteAddress; } |
| | | 674 | | } |
| | | 675 | | |
| | | 676 | | Uri IRequestChannel.Via |
| | | 677 | | { |
| | 0 | 678 | | get { return _serviceChannel.Via; } |
| | | 679 | | } |
| | | 680 | | |
| | | 681 | | Uri IServiceChannel.ListenUri |
| | | 682 | | { |
| | 0 | 683 | | get { return _serviceChannel.ListenUri; } |
| | | 684 | | } |
| | | 685 | | |
| | | 686 | | bool IDuplexContextChannel.AutomaticInputSessionShutdown |
| | | 687 | | { |
| | | 688 | | get |
| | | 689 | | { |
| | 0 | 690 | | return ((IDuplexContextChannel)_serviceChannel).AutomaticInputSessionShutdown; |
| | | 691 | | } |
| | | 692 | | |
| | | 693 | | set |
| | | 694 | | { |
| | 0 | 695 | | ((IDuplexContextChannel)_serviceChannel).AutomaticInputSessionShutdown = value; |
| | 0 | 696 | | } |
| | | 697 | | } |
| | | 698 | | |
| | | 699 | | InstanceContext IDuplexContextChannel.CallbackInstance |
| | | 700 | | { |
| | | 701 | | get |
| | | 702 | | { |
| | 0 | 703 | | return ((IDuplexContextChannel)_serviceChannel).CallbackInstance; |
| | | 704 | | } |
| | | 705 | | |
| | | 706 | | set |
| | | 707 | | { |
| | 0 | 708 | | ((IDuplexContextChannel)_serviceChannel).CallbackInstance = value; |
| | 0 | 709 | | } |
| | | 710 | | } |
| | | 711 | | |
| | 0 | 712 | | IServiceChannelDispatcher IChannel.ChannelDispatcher { get => ((IChannel)_serviceChannel).ChannelDispatcher; set |
| | | 713 | | #endregion // Channel interfaces |
| | | 714 | | } |
| | | 715 | | } |