< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.OperationContext
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/OperationContext.cs
Line coverage
47%
Covered lines: 54
Uncovered lines: 59
Coverable lines: 113
Total lines: 344
Line coverage: 47.7%
Branch coverage
29%
Covered branches: 19
Total branches: 64
Branch coverage: 29.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor(...)0%660%
.ctor(...)100%110%
.ctor(...)0%220%
.ctor(...)100%11100%
ClearClientReplyNoThrow()100%11100%
FireOperationCompleted()25%4444.44%
GetCallbackChannel()50%4466.66%
ReInit(...)100%110%
Recycle()100%110%
SetClientReply(...)25%8855.55%
SetInstanceContext(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/OperationContext.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.Security.Claims;
 6using System.Security.Principal;
 7using System.Threading;
 8using CoreWCF.Channels;
 9using CoreWCF.Dispatcher;
 10using CoreWCF.Runtime;
 11
 12namespace CoreWCF
 13{
 14    public sealed class OperationContext : IExtensibleObject<OperationContext>
 15    {
 816        private static readonly AsyncLocal<OperationContext> s_currentContext = new AsyncLocal<OperationContext>();
 17        private Message _clientReply;
 18        private bool _closeClientReply;
 19        private ExtensionCollection<OperationContext> _extensions;
 20        private Message _request;
 21        internal IPrincipal _threadPrincipal;
 22        private MessageProperties _outgoingMessageProperties;
 23        private MessageHeaders _outgoingMessageHeaders;
 24
 25        public event EventHandler OperationCompleted;
 26
 027        public OperationContext(IContextChannel channel)
 28        {
 029            if (channel == null)
 30            {
 031                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(channel));
 32            }
 33
 34
 35            //Could be a TransparentProxy
 036            if (!(channel is ServiceChannel serviceChannel))
 37            {
 038                serviceChannel = ServiceChannelFactory.GetServiceChannel(channel);
 39            }
 40
 041            if (serviceChannel != null)
 42            {
 043                OutgoingMessageVersion = serviceChannel.MessageVersion;
 044                InternalServiceChannel = serviceChannel;
 45            }
 46            else
 47            {
 048                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvalidCha
 49            }
 50        }
 51
 52        internal OperationContext(ServiceHostBase host)
 053            : this(host, MessageVersion.Soap12WSAddressing10)
 54        {
 055        }
 56
 057        internal OperationContext(ServiceHostBase host, MessageVersion outgoingMessageVersion)
 58        {
 059            Host = host;
 060            OutgoingMessageVersion = outgoingMessageVersion ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgum
 061        }
 62
 253863        internal OperationContext(RequestContext requestContext, Message request, ServiceChannel channel, ServiceHostBas
 64        {
 253865            InternalServiceChannel = channel;
 253866            Host = host;
 253867            RequestContext = requestContext;
 253868            _request = request;
 253869            OutgoingMessageVersion = channel.MessageVersion;
 253870        }
 71
 72        // TODO: Probably want to revert this to public
 73        internal IContextChannel Channel
 74        {
 075            get { return GetCallbackChannel<IContextChannel>(); }
 76        }
 77
 78        public static OperationContext Current
 79        {
 80            get
 81            {
 549282                return s_currentContext.Value;
 83            }
 84
 85            set
 86            {
 507687                s_currentContext.Value = value;
 507688            }
 89        }
 90
 691        public ServiceHostBase Host { get; }
 92
 507693        public EndpointDispatcher EndpointDispatcher { get; set; }
 94        public bool IsUserContext
 95        {
 96            get
 97            {
 298                return (_request == null);
 99            }
 100        }
 101
 102        public IExtensionCollection<OperationContext> Extensions
 103        {
 104            get
 105            {
 124106                if (_extensions == null)
 107                {
 34108                    _extensions = new ExtensionCollection<OperationContext>(this);
 109                }
 124110                return _extensions;
 111            }
 112        }
 113
 2114        internal bool IsServiceReentrant { get; set; } = false;
 115
 116        internal Message IncomingMessage
 117        {
 2391118            get { return _clientReply ?? _request; }
 119        }
 120
 8051121        internal ServiceChannel InternalServiceChannel { get; set; }
 122
 123        internal bool HasOutgoingMessageHeaders
 124        {
 729125            get { return (_outgoingMessageHeaders != null); }
 126        }
 127
 128        public MessageHeaders OutgoingMessageHeaders
 129        {
 130            get
 131            {
 0132                if (_outgoingMessageHeaders == null)
 133                {
 0134                    _outgoingMessageHeaders = new MessageHeaders(OutgoingMessageVersion);
 135                }
 136
 0137                return _outgoingMessageHeaders;
 138            }
 139        }
 140
 141        internal bool HasOutgoingMessageProperties
 142        {
 729143            get { return (_outgoingMessageProperties != null); }
 144        }
 145
 146        public MessageProperties OutgoingMessageProperties
 147        {
 148            get
 149            {
 259150                if (_outgoingMessageProperties == null)
 151                {
 35152                    _outgoingMessageProperties = new MessageProperties();
 153                }
 154
 259155                return _outgoingMessageProperties;
 156            }
 157        }
 158
 1159        internal MessageVersion OutgoingMessageVersion { get; }
 160
 161        public MessageHeaders IncomingMessageHeaders
 162        {
 163            get
 164            {
 4165                Message message = _clientReply ?? _request;
 4166                if (message != null)
 167                {
 4168                    return message.Headers;
 169                }
 170                else
 171                {
 0172                    return null;
 173                }
 174            }
 175        }
 176
 177        public MessageProperties IncomingMessageProperties
 178        {
 179            get
 180            {
 134181                Message message = _clientReply ?? _request;
 134182                if (message != null)
 183                {
 134184                    return message.Properties;
 185                }
 186                else
 187                {
 0188                    return null;
 189                }
 190            }
 191        }
 192
 193        public MessageVersion IncomingMessageVersion
 194        {
 195            get
 196            {
 0197                Message message = _clientReply ?? _request;
 0198                if (message != null)
 199                {
 0200                    return message.Version;
 201                }
 202                else
 203                {
 0204                    return null;
 205                }
 206            }
 207        }
 208
 2641209        public InstanceContext InstanceContext { get; private set; }
 210
 12703211        public RequestContext RequestContext { get; set; }
 212
 213        public ServiceSecurityContext ServiceSecurityContext
 214        {
 215            get
 216            {
 25217                MessageProperties properties = IncomingMessageProperties;
 25218                if (properties != null && properties.Security != null)
 219                {
 25220                    return properties.Security.ServiceSecurityContext;
 221                }
 0222                return null;
 223            }
 224        }
 225
 226        public string SessionId
 227        {
 228            get
 229            {
 0230                if (InternalServiceChannel != null)
 231                {
 0232                    IChannel inner = InternalServiceChannel.InnerChannel;
 0233                    if (inner != null)
 234                    {
 0235                        if ((inner is ISessionChannel<IDuplexSession> duplex) && (duplex.Session != null))
 0236                            return duplex.Session.Id;
 237
 0238                        if ((inner is ISessionChannel<IInputSession> input) && (input.Session != null))
 0239                            return input.Session.Id;
 240
 0241                        if ((inner is ISessionChannel<IOutputSession> output) && (output.Session != null))
 0242                            return output.Session.Id;
 243                    }
 244                }
 0245                return null;
 246            }
 247        }
 248
 249        internal IPrincipal ThreadPrincipal
 250        {
 0251            get { return _threadPrincipal; }
 0252            set { _threadPrincipal = value; }
 253        }
 254
 255        public ClaimsPrincipal ClaimsPrincipal
 256        {
 134257            get;
 80258            internal set;
 259        }
 260
 261        internal void ClearClientReplyNoThrow()
 262        {
 2538263            _clientReply = null;
 2538264        }
 265
 266        internal void FireOperationCompleted()
 267        {
 268            try
 269            {
 2538270                EventHandler handler = OperationCompleted;
 271
 2538272                if (handler != null)
 273                {
 0274                    handler(this, EventArgs.Empty);
 275                }
 2538276            }
 0277            catch (Exception e)
 278            {
 0279                if (Fx.IsFatal(e))
 280                {
 0281                    throw;
 282                }
 283
 0284                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
 285            }
 2538286        }
 287
 288        public T GetCallbackChannel<T>()
 289        {
 1290            if (InternalServiceChannel == null || IsUserContext)
 291            {
 0292                return default;
 293            }
 294
 295            // yes, we might throw InvalidCastException here.  Is it really
 296            // better to check and throw something else instead?
 1297            return (T)InternalServiceChannel.Proxy;
 298        }
 299
 300        internal void ReInit(RequestContext requestContext, Message request, ServiceChannel channel)
 301        {
 0302            RequestContext = requestContext;
 0303            _request = request;
 0304            InternalServiceChannel = channel;
 0305        }
 306
 307        internal void Recycle()
 308        {
 0309            RequestContext = null;
 0310            _request = null;
 0311            _extensions = null;
 0312            InstanceContext = null;
 0313            _threadPrincipal = null;
 0314            SetClientReply(null, false);
 0315        }
 316
 317        internal void SetClientReply(Message message, bool closeMessage)
 318        {
 2375319            Message oldClientReply = null;
 320
 2375321            if (!Equals(message, _clientReply))
 322            {
 0323                if (_closeClientReply && (_clientReply != null))
 324                {
 0325                    oldClientReply = _clientReply;
 326                }
 327
 0328                _clientReply = message;
 329            }
 330
 2375331            _closeClientReply = closeMessage;
 332
 2375333            if (oldClientReply != null)
 334            {
 0335                oldClientReply.Close();
 336            }
 2375337        }
 338
 339        internal void SetInstanceContext(InstanceContext instanceContext)
 340        {
 2536341            InstanceContext = instanceContext;
 2536342        }
 343    }
 344}