< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.SyncMethodInvoker
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/SyncMethodInvoker.cs
Line coverage
63%
Covered lines: 24
Uncovered lines: 14
Coverable lines: 38
Total lines: 207
Line coverage: 63.1%
Branch coverage
50%
Covered branches: 8
Total branches: 16
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%44100%
AllocateInputs()100%11100%
Invoke(...)100%110%
InvokeBegin(...)100%110%
InvokeEnd(...)100%110%
InvokeAsync(...)50%8857.14%
EnsureIsInitialized()100%22100%
EnsureIsInitializedCore()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/SyncMethodInvoker.cs

#LineLine coverage
 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
 4using System;
 5using System.Reflection;
 6using System.Threading.Tasks;
 7using CoreWCF.Runtime;
 8using CoreWCF.Security;
 9using Microsoft.Extensions.DependencyInjection;
 10using Microsoft.Extensions.Logging;
 11
 12namespace 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
 17522        public SyncMethodInvoker(IServiceProvider serviceProvider, MethodInfo method)
 23        {
 17524            _serviceProvider = serviceProvider ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo
 17525            Method = method ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(method));
 17526        }
 27
 4628        public MethodInfo Method { get; }
 29
 30        public string MethodName
 31        {
 32            get
 33            {
 034                if (_methodName == null)
 35                {
 036                    _methodName = Method.Name;
 37                }
 38
 039                return _methodName;
 40            }
 41        }
 42
 43        public object[] AllocateInputs()
 44        {
 4445            EnsureIsInitialized();
 46
 4447            return EmptyArray<object>.Allocate(_inputParameterCount);
 48        }
 49
 50        public object Invoke(object instance, object[] inputs, out object[] outputs)
 51        {
 052            throw new NotImplementedException();
 53        }
 54
 55        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
 56        {
 057            return InvokeAsync(instance, inputs).ToApm(callback, state);
 58        }
 59
 60        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
 61        {
 062            Tuple<object, object[]> tuple = TaskHelpers.ToApmEnd<Tuple<object, object[]>>(result);
 063            outputs = tuple.Item2;
 064            return tuple.Item1;
 65        }
 66
 67        public ValueTask<(object returnValue, object[] outputs)> InvokeAsync(object instance, object[] inputs)
 68        {
 4469            EnsureIsInitialized();
 70
 4471            if (instance == null)
 72            {
 073                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxNoServiceO
 74            }
 75
 4476            if (inputs == null)
 77            {
 078                if (_inputParameterCount > 0)
 79                {
 080                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 81                }
 82            }
 4483            else if (inputs.Length != _inputParameterCount)
 84            {
 085                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx
 86            }
 87
 4488            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                //    }
 44145                returnValue = _invokeDelegate(instance, inputs, outputs);
 146                //callSucceeded = true;
 147                //}
 42148            }
 0149            catch (FaultException)
 150            {
 151                //callFaulted = true;
 0152                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
 42185            return new ValueTask<(object returnValue, object[] outputs)>((returnValue, outputs));
 186        }
 187
 188        private void EnsureIsInitialized()
 189        {
 88190            if (_invokeDelegate == null)
 191            {
 43192                EnsureIsInitializedCore();
 193            }
 88194        }
 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.
 43200            ILogger<InvokeDelegate> logger = _serviceProvider.GetRequiredService<ILogger<InvokeDelegate>>();
 43201            InvokeDelegate invokeDelegate = InvokerUtil.GenerateInvokeDelegate(logger, Method, out int inputParameterCou
 43202            _outputParameterCount = outputParameterCount;
 43203            _inputParameterCount = inputParameterCount;
 43204            _invokeDelegate = invokeDelegate;  // must set this last due to race
 43205        }
 206    }
 207}