| | | 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.Linq; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Description; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Dispatcher |
| | | 13 | | { |
| | | 14 | | public class OperationInvokerBehavior : IOperationBehavior |
| | | 15 | | { |
| | | 16 | | private IServiceProvider _serviceProvider; |
| | | 17 | | |
| | 10 | 18 | | private static readonly Lazy<bool> s_isOperationInvokerGeneratorEnabled = new Lazy<bool>(() => |
| | 10 | 19 | | { |
| | 10 | 20 | | if (AppContext.TryGetSwitch("CoreWCF.Dispatcher.UseGeneratedOperationInvokers", out var value)) |
| | 10 | 21 | | { |
| | 0 | 22 | | return value; |
| | 10 | 23 | | } |
| | 10 | 24 | | |
| | 10 | 25 | | Assembly entryAssembly = Assembly.GetEntryAssembly(); |
| | 10 | 26 | | // return null when running UT with Xunit and .NET Framework |
| | 10 | 27 | | if (entryAssembly != null) |
| | 10 | 28 | | { |
| | 10 | 29 | | var assemblyAttributeArgsData = entryAssembly |
| | 10 | 30 | | .GetCustomAttributesData() |
| | 188 | 31 | | .Where(static data => data.AttributeType == typeof(EnableCoreWCFOperationInvokerGeneratorAttribute)) |
| | 0 | 32 | | .SelectMany(static data => data.ConstructorArguments) |
| | 0 | 33 | | .Where(static data => data.ArgumentType == typeof(bool)) |
| | 10 | 34 | | .ToList(); |
| | 10 | 35 | | |
| | 10 | 36 | | if (assemblyAttributeArgsData.Count > 0) |
| | 10 | 37 | | { |
| | 0 | 38 | | return assemblyAttributeArgsData[0].Value is true; |
| | 10 | 39 | | } |
| | 10 | 40 | | } |
| | 10 | 41 | | |
| | 10 | 42 | | return true; |
| | 10 | 43 | | }); |
| | | 44 | | |
| | 2686 | 45 | | public OperationInvokerBehavior() |
| | | 46 | | { |
| | | 47 | | |
| | 2686 | 48 | | } |
| | | 49 | | |
| | | 50 | | void IOperationBehavior.Validate(OperationDescription description) |
| | | 51 | | { |
| | 2645 | 52 | | } |
| | | 53 | | |
| | | 54 | | void IOperationBehavior.AddBindingParameters(OperationDescription description, BindingParameterCollection parame |
| | | 55 | | { |
| | 3187 | 56 | | _serviceProvider = parameters.Find<IServiceProvider>(); |
| | 3187 | 57 | | } |
| | | 58 | | |
| | | 59 | | void IOperationBehavior.ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch) |
| | | 60 | | { |
| | 3026 | 61 | | if (dispatch == null) |
| | | 62 | | { |
| | 0 | 63 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dispatch)); |
| | | 64 | | } |
| | 3026 | 65 | | if (description == null) |
| | | 66 | | { |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description)); |
| | | 68 | | } |
| | | 69 | | |
| | 3026 | 70 | | if (description.TaskMethod != null) |
| | | 71 | | { |
| | 409 | 72 | | if (s_isOperationInvokerGeneratorEnabled.Value) |
| | | 73 | | { |
| | 409 | 74 | | if (DispatchOperationRuntimeHelpers.OperationInvokers.TryGetValue(DispatchOperationRuntimeHelpers.Ge |
| | | 75 | | { |
| | 401 | 76 | | dispatch.Invoker = invoker; |
| | | 77 | | } |
| | | 78 | | else |
| | | 79 | | { |
| | | 80 | | // Fallback to reflection-based invoker if generated invoker not found |
| | 8 | 81 | | dispatch.Invoker = new TaskMethodInvoker(_serviceProvider, description.TaskMethod, description.T |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | else |
| | | 85 | | { |
| | 0 | 86 | | dispatch.Invoker = new TaskMethodInvoker(_serviceProvider, description.TaskMethod, description.TaskT |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | } |
| | 2617 | 90 | | else if (description.SyncMethod != null) |
| | | 91 | | { |
| | 2617 | 92 | | if (s_isOperationInvokerGeneratorEnabled.Value) |
| | | 93 | | { |
| | 2617 | 94 | | if (DispatchOperationRuntimeHelpers.OperationInvokers.TryGetValue(DispatchOperationRuntimeHelpers.Ge |
| | | 95 | | { |
| | 2442 | 96 | | dispatch.Invoker = invoker; |
| | | 97 | | } |
| | | 98 | | else |
| | | 99 | | { |
| | | 100 | | // Fallback to reflection-based invoker if generated invoker not found |
| | 175 | 101 | | if (description.BeginMethod != null) |
| | | 102 | | { |
| | | 103 | | // both sync and async methods are present on the contract |
| | 0 | 104 | | dispatch.Invoker = new SyncMethodInvoker(_serviceProvider, description.SyncMethod); |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | | 108 | | // only sync method is present on the contract |
| | 175 | 109 | | dispatch.Invoker = new SyncMethodInvoker(_serviceProvider, description.SyncMethod); |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | else |
| | | 114 | | { |
| | 0 | 115 | | if (description.BeginMethod != null) |
| | | 116 | | { |
| | | 117 | | // both sync and async methods are present on the contract, check the preference |
| | | 118 | | //OperationBehaviorAttribute operationBehaviorAttribute = description.Behaviors.Find<OperationBe |
| | | 119 | | //if ((operationBehaviorAttribute != null) && operationBehaviorAttribute.PreferAsyncInvocation) |
| | | 120 | | //{ |
| | | 121 | | // dispatch.Invoker = new AsyncMethodInvoker(description.BeginMethod, description.EndMethod); |
| | | 122 | | //} |
| | | 123 | | //else |
| | | 124 | | //{ |
| | 0 | 125 | | dispatch.Invoker = new SyncMethodInvoker(_serviceProvider, description.SyncMethod); |
| | | 126 | | //} |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | | 130 | | // only sync method is present on the contract |
| | 0 | 131 | | dispatch.Invoker = new SyncMethodInvoker(_serviceProvider, description.SyncMethod); |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | else |
| | | 136 | | { |
| | 0 | 137 | | if (description.BeginMethod != null) |
| | | 138 | | { |
| | | 139 | | // only async method is present on the contract |
| | 0 | 140 | | throw new PlatformNotSupportedException(); |
| | | 141 | | //dispatch.Invoker = new AsyncMethodInvoker(description.BeginMethod, description.EndMethod); |
| | | 142 | | } |
| | | 143 | | } |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | void IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy) |
| | | 147 | | { |
| | 2 | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |