| | | 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.Reflection; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | using CoreWCF.Security; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Dispatcher |
| | | 13 | | { |
| | | 14 | | internal class SyncMethodInvoker : IOperationInvoker |
| | | 15 | | { |
| | | 16 | | private InvokeDelegate _invokeDelegate; |
| | | 17 | | private int _inputParameterCount; |
| | | 18 | | private int _outputParameterCount; |
| | | 19 | | private string _methodName; |
| | | 20 | | private readonly IServiceProvider _serviceProvider; |
| | | 21 | | |
| | 175 | 22 | | public SyncMethodInvoker(IServiceProvider serviceProvider, MethodInfo method) |
| | | 23 | | { |
| | 175 | 24 | | _serviceProvider = serviceProvider ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo |
| | 175 | 25 | | Method = method ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(method)); |
| | 175 | 26 | | } |
| | | 27 | | |
| | 46 | 28 | | public MethodInfo Method { get; } |
| | | 29 | | |
| | | 30 | | public string MethodName |
| | | 31 | | { |
| | | 32 | | get |
| | | 33 | | { |
| | 0 | 34 | | if (_methodName == null) |
| | | 35 | | { |
| | 0 | 36 | | _methodName = Method.Name; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | return _methodName; |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public object[] AllocateInputs() |
| | | 44 | | { |
| | 44 | 45 | | EnsureIsInitialized(); |
| | | 46 | | |
| | 44 | 47 | | return EmptyArray<object>.Allocate(_inputParameterCount); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public object Invoke(object instance, object[] inputs, out object[] outputs) |
| | | 51 | | { |
| | 0 | 52 | | throw new NotImplementedException(); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) |
| | | 56 | | { |
| | 0 | 57 | | return InvokeAsync(instance, inputs).ToApm(callback, state); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result) |
| | | 61 | | { |
| | 0 | 62 | | Tuple<object, object[]> tuple = TaskHelpers.ToApmEnd<Tuple<object, object[]>>(result); |
| | 0 | 63 | | outputs = tuple.Item2; |
| | 0 | 64 | | return tuple.Item1; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public ValueTask<(object returnValue, object[] outputs)> InvokeAsync(object instance, object[] inputs) |
| | | 68 | | { |
| | 44 | 69 | | EnsureIsInitialized(); |
| | | 70 | | |
| | 44 | 71 | | if (instance == null) |
| | | 72 | | { |
| | 0 | 73 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxNoServiceO |
| | | 74 | | } |
| | | 75 | | |
| | 44 | 76 | | if (inputs == null) |
| | | 77 | | { |
| | 0 | 78 | | if (_inputParameterCount > 0) |
| | | 79 | | { |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 81 | | } |
| | | 82 | | } |
| | 44 | 83 | | else if (inputs.Length != _inputParameterCount) |
| | | 84 | | { |
| | 0 | 85 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 86 | | } |
| | | 87 | | |
| | 44 | 88 | | object[] outputs = EmptyArray<object>.Allocate(_outputParameterCount); |
| | | 89 | | |
| | | 90 | | //long beginOperation = 0; |
| | | 91 | | //bool callSucceeded = false; |
| | | 92 | | //bool callFaulted = false; |
| | | 93 | | |
| | | 94 | | //EventTraceActivity eventTraceActivity = null; |
| | | 95 | | //if (WcfEventSource.Instance.OperationCompletedIsEnabled() || |
| | | 96 | | // WcfEventSource.Instance.OperationFaultedIsEnabled() || |
| | | 97 | | // WcfEventSource.Instance.OperationFailedIsEnabled()) |
| | | 98 | | //{ |
| | | 99 | | // beginOperation = DateTime.UtcNow.Ticks; |
| | | 100 | | // OperationContext context = OperationContext.Current; |
| | | 101 | | // if (context != null && context.IncomingMessage != null) |
| | | 102 | | // { |
| | | 103 | | // eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(context.IncomingMessage); |
| | | 104 | | // } |
| | | 105 | | //} |
| | | 106 | | |
| | | 107 | | object returnValue; |
| | | 108 | | try |
| | | 109 | | { |
| | | 110 | | //ServiceModelActivity activity = null; |
| | | 111 | | //IDisposable boundActivity = null; |
| | | 112 | | //if (DiagnosticUtility.ShouldUseActivity) |
| | | 113 | | //{ |
| | | 114 | | // activity = ServiceModelActivity.CreateBoundedActivity(true); |
| | | 115 | | // boundActivity = activity; |
| | | 116 | | //} |
| | | 117 | | //else if (TraceUtility.MessageFlowTracingOnly) |
| | | 118 | | //{ |
| | | 119 | | // Guid activityId = TraceUtility.GetReceivedActivityId(OperationContext.Current); |
| | | 120 | | // if (activityId != Guid.Empty) |
| | | 121 | | // { |
| | | 122 | | // DiagnosticTraceBase.ActivityId = activityId; |
| | | 123 | | // } |
| | | 124 | | //} |
| | | 125 | | //else if (TraceUtility.ShouldPropagateActivity) |
| | | 126 | | //{ |
| | | 127 | | // //Message flow tracing only scenarios use a light-weight ActivityID management logic |
| | | 128 | | // Guid activityId = ActivityIdHeader.ExtractActivityId(OperationContext.Current.IncomingMessage); |
| | | 129 | | // if (activityId != Guid.Empty) |
| | | 130 | | // { |
| | | 131 | | // boundActivity = Activity.CreateActivity(activityId); |
| | | 132 | | // } |
| | | 133 | | //} |
| | | 134 | | |
| | | 135 | | //using (boundActivity) |
| | | 136 | | //{ |
| | | 137 | | // if (DiagnosticUtility.ShouldUseActivity) |
| | | 138 | | // { |
| | | 139 | | // ServiceModelActivity.Start(activity, SR.Format(SR.ActivityExecuteMethod, _method.DeclaringType |
| | | 140 | | // } |
| | | 141 | | // if (WcfEventSource.Instance.OperationInvokedIsEnabled()) |
| | | 142 | | // { |
| | | 143 | | // WcfEventSource.Instance.OperationInvoked(eventTraceActivity, MethodName, TraceUtility.GetCalle |
| | | 144 | | // } |
| | 44 | 145 | | returnValue = _invokeDelegate(instance, inputs, outputs); |
| | | 146 | | //callSucceeded = true; |
| | | 147 | | //} |
| | 42 | 148 | | } |
| | 0 | 149 | | catch (FaultException) |
| | | 150 | | { |
| | | 151 | | //callFaulted = true; |
| | 0 | 152 | | throw; |
| | | 153 | | } |
| | | 154 | | finally |
| | | 155 | | { |
| | | 156 | | //if (beginOperation != 0) |
| | | 157 | | //{ |
| | | 158 | | // if (callSucceeded) |
| | | 159 | | // { |
| | | 160 | | // if (WcfEventSource.Instance.OperationCompletedIsEnabled()) |
| | | 161 | | // { |
| | | 162 | | // WcfEventSource.Instance.OperationCompleted(eventTraceActivity, _methodName, |
| | | 163 | | // TraceUtility.GetUtcBasedDurationForTrace(beginOperation)); |
| | | 164 | | // } |
| | | 165 | | // } |
| | | 166 | | // else if (callFaulted) |
| | | 167 | | // { |
| | | 168 | | // if (WcfEventSource.Instance.OperationFaultedIsEnabled()) |
| | | 169 | | // { |
| | | 170 | | // WcfEventSource.Instance.OperationFaulted(eventTraceActivity, _methodName, |
| | | 171 | | // TraceUtility.GetUtcBasedDurationForTrace(beginOperation)); |
| | | 172 | | // } |
| | | 173 | | // } |
| | | 174 | | // else |
| | | 175 | | // { |
| | | 176 | | // if (WcfEventSource.Instance.OperationFailedIsEnabled()) |
| | | 177 | | // { |
| | | 178 | | // WcfEventSource.Instance.OperationFailed(eventTraceActivity, _methodName, |
| | | 179 | | // TraceUtility.GetUtcBasedDurationForTrace(beginOperation)); |
| | | 180 | | // } |
| | | 181 | | // } |
| | | 182 | | //} |
| | | 183 | | } |
| | | 184 | | |
| | 42 | 185 | | return new ValueTask<(object returnValue, object[] outputs)>((returnValue, outputs)); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private void EnsureIsInitialized() |
| | | 189 | | { |
| | 88 | 190 | | if (_invokeDelegate == null) |
| | | 191 | | { |
| | 43 | 192 | | EnsureIsInitializedCore(); |
| | | 193 | | } |
| | 88 | 194 | | } |
| | | 195 | | |
| | | 196 | | private void EnsureIsInitializedCore() |
| | | 197 | | { |
| | | 198 | | // Only pass locals byref because InvokerUtil may store temporary results in the byref. |
| | | 199 | | // If two threads both reference this.count, temporary results may interact. |
| | 43 | 200 | | ILogger<InvokeDelegate> logger = _serviceProvider.GetRequiredService<ILogger<InvokeDelegate>>(); |
| | 43 | 201 | | InvokeDelegate invokeDelegate = InvokerUtil.GenerateInvokeDelegate(logger, Method, out int inputParameterCou |
| | 43 | 202 | | _outputParameterCount = outputParameterCount; |
| | 43 | 203 | | _inputParameterCount = inputParameterCount; |
| | 43 | 204 | | _invokeDelegate = invokeDelegate; // must set this last due to race |
| | 43 | 205 | | } |
| | | 206 | | } |
| | | 207 | | } |