< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.FaultFormatter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/FaultFormatter.cs
Line coverage
49%
Covered lines: 48
Uncovered lines: 49
Coverable lines: 97
Total lines: 225
Line coverage: 49.4%
Branch coverage
33%
Covered branches: 16
Total branches: 48
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%220%
.ctor(...)100%11100%
Serialize(...)75%88100%
Deserialize(...)0%220%
GetSerializer(...)87.5%8890.9%
CreateFaultException(...)0%14140%
CreateFaultException(...)0%10100%
GetSortedArray(...)100%11100%
AddInfrastructureFaults(...)100%11100%
CreateMessageFault(...)75%4483.33%
.ctor(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/FaultFormatter.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.Collections.Generic;
 6using System.Reflection;
 7using System.Runtime.Serialization;
 8using System.Xml;
 9using CoreWCF.Channels;
 10using CoreWCF.Collections.Generic;
 11using CoreWCF.Runtime;
 12
 13namespace CoreWCF.Dispatcher
 14{
 15    internal class FaultFormatter : IClientFaultFormatter, IDispatchFaultFormatter
 16    {
 17        private readonly FaultContractInfo[] _faultContractInfos;
 18
 019        internal FaultFormatter(Type[] detailTypes)
 20        {
 021            List<FaultContractInfo> faultContractInfoList = new List<FaultContractInfo>();
 022            for (int i = 0; i < detailTypes.Length; i++)
 23            {
 024                faultContractInfoList.Add(new FaultContractInfo(MessageHeaders.WildcardAction, detailTypes[i]));
 25            }
 26
 027            AddInfrastructureFaults(faultContractInfoList);
 028            _faultContractInfos = GetSortedArray(faultContractInfoList);
 029        }
 30
 367631        internal FaultFormatter(SynchronizedCollection<FaultContractInfo> faultContractInfoCollection)
 32        {
 33            List<FaultContractInfo> faultContractInfoList;
 367634            lock (faultContractInfoCollection.SyncRoot)
 35            {
 367636                faultContractInfoList = new List<FaultContractInfo>(faultContractInfoCollection);
 367637            }
 367638            AddInfrastructureFaults(faultContractInfoList);
 367639            _faultContractInfos = GetSortedArray(faultContractInfoList);
 367640        }
 41
 42        public MessageFault Serialize(FaultException faultException, out string action)
 43        {
 10344            XmlObjectSerializer serializer = null;
 10345            Type detailType = null;
 10346            string faultExceptionAction = action = faultException.Action;
 47
 10348            Type faultExceptionOfT = null;
 20649            for (Type faultType = faultException.GetType(); faultType != typeof(FaultException); faultType = faultType.G
 50            {
 6551                if (faultType.GetTypeInfo().IsGenericType && (faultType.GetGenericTypeDefinition() == typeof(FaultExcept
 52                {
 6553                    faultExceptionOfT = faultType;
 6554                    break;
 55                }
 56            }
 10357            if (faultExceptionOfT != null)
 58            {
 6559                detailType = faultExceptionOfT.GetGenericArguments()[0];
 6560                serializer = GetSerializer(detailType, faultExceptionAction, out action);
 61            }
 10362            return CreateMessageFault(serializer, faultException, detailType);
 63        }
 64
 65        public FaultException Deserialize(MessageFault messageFault, string action)
 66        {
 067            if (!messageFault.HasDetail)
 68            {
 069                return new FaultException(messageFault, action);
 70            }
 71
 072            return CreateFaultException(messageFault, action);
 73        }
 74
 75        protected virtual XmlObjectSerializer GetSerializer(Type detailType, string faultExceptionAction, out string act
 76        {
 6577            action = faultExceptionAction;
 6578            FaultContractInfo faultInfo = null;
 29879            for (int i = 0; i < _faultContractInfos.Length; i++)
 80            {
 14981                if (_faultContractInfos[i].Detail == detailType)
 82                {
 6583                    faultInfo = _faultContractInfos[i];
 6584                    break;
 85                }
 86            }
 6587            if (faultInfo != null)
 88            {
 6589                if (action == null)
 90                {
 6591                    action = faultInfo.Action;
 92                }
 93
 6594                return faultInfo.Serializer;
 95            }
 96            else
 97            {
 098                return DataContractSerializerDefaults.CreateSerializer(detailType, int.MaxValue /* maxItemsInObjectGraph
 99            }
 100        }
 101
 102        protected virtual FaultException CreateFaultException(MessageFault messageFault, string action)
 103        {
 104            IList<FaultContractInfo> faultInfos;
 0105            if (action != null)
 106            {
 0107                faultInfos = new List<FaultContractInfo>();
 0108                for (int i = 0; i < _faultContractInfos.Length; i++)
 109                {
 0110                    if (_faultContractInfos[i].Action == action || _faultContractInfos[i].Action == MessageHeaders.Wildc
 111                    {
 0112                        faultInfos.Add(_faultContractInfos[i]);
 113                    }
 114                }
 115            }
 116            else
 117            {
 0118                faultInfos = _faultContractInfos;
 119            }
 120
 0121            for (int i = 0; i < faultInfos.Count; i++)
 122            {
 0123                FaultContractInfo faultInfo = faultInfos[i];
 0124                XmlDictionaryReader detailReader = messageFault.GetReaderAtDetailContents();
 0125                XmlObjectSerializer serializer = faultInfo.Serializer;
 0126                if (serializer.IsStartObject(detailReader))
 127                {
 0128                    Type detailType = faultInfo.Detail;
 129                    try
 130                    {
 0131                        object detailObj = serializer.ReadObject(detailReader);
 0132                        FaultException faultException = CreateFaultException(messageFault, action,
 0133                            detailObj, detailType, detailReader);
 0134                        if (faultException != null)
 135                        {
 0136                            return faultException;
 137                        }
 0138                    }
 139#pragma warning disable CA1031 // Do not catch general exception types - return a non-specific FaultException is can't d
 0140                    catch (SerializationException)
 141                    {
 0142                    }
 143#pragma warning restore CA1031 // Do not catch general exception types
 144                }
 145            }
 0146            return new FaultException(messageFault, action);
 0147        }
 148
 149        protected FaultException CreateFaultException(MessageFault messageFault, string action,
 150            object detailObj, Type detailType, XmlDictionaryReader detailReader)
 151        {
 0152            if (!detailReader.EOF)
 153            {
 0154                detailReader.MoveToContent();
 0155                if (detailReader.NodeType != XmlNodeType.EndElement && !detailReader.EOF)
 156                {
 0157                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.ExtraContentIsPrese
 158                }
 159            }
 160            bool isDetailObjectValid;
 0161            if (detailObj == null)
 162            {
 0163                isDetailObjectValid = !detailType.GetTypeInfo().IsValueType;
 164            }
 165            else
 166            {
 0167                isDetailObjectValid = detailType.IsAssignableFrom(detailObj.GetType());
 168            }
 0169            if (isDetailObjectValid)
 170            {
 0171                Type knownFaultType = typeof(FaultException<>).MakeGenericType(detailType);
 0172                return (FaultException)Activator.CreateInstance(knownFaultType,
 0173                                                            detailObj,
 0174                                                            messageFault.Reason,
 0175                                                            messageFault.Code,
 0176                                                            action);
 177            }
 0178            return null;
 179        }
 180
 181        private static FaultContractInfo[] GetSortedArray(List<FaultContractInfo> faultContractInfoList)
 182        {
 3676183            FaultContractInfo[] temp = faultContractInfoList.ToArray();
 3676184            Array.Sort<FaultContractInfo>(temp,
 3676185                delegate (FaultContractInfo x, FaultContractInfo y)
 246186                { return string.CompareOrdinal(x.Action, y.Action); }
 3676187                );
 3676188            return temp;
 189        }
 190
 191        private static void AddInfrastructureFaults(List<FaultContractInfo> faultContractInfos)
 192        {
 3676193            faultContractInfos.Add(new FaultContractInfo(FaultCodeConstants.Actions.NetDispatcher, typeof(ExceptionDetai
 3676194        }
 195
 196        private static MessageFault CreateMessageFault(XmlObjectSerializer serializer, FaultException faultException, Ty
 197        {
 103198            if (detailType == null)
 199            {
 38200                if (faultException.Fault != null)
 201                {
 0202                    return faultException.Fault;
 203                }
 204
 38205                return MessageFault.CreateFault(faultException.Code, faultException.Reason);
 206            }
 207            Fx.Assert(serializer != null, "");
 208
 65209            Type operationFaultType = typeof(OperationFault<>).MakeGenericType(detailType);
 65210            return (MessageFault)Activator.CreateInstance(operationFaultType, serializer, faultException);
 211        }
 212
 213        internal class OperationFault<T> : XmlObjectSerializerFault
 214        {
 215            public OperationFault(XmlObjectSerializer serializer, FaultException<T> faultException) :
 65216                base(faultException.Code, faultException.Reason,
 65217                      faultException.Detail,
 65218                      serializer,
 65219                      string.Empty/*actor*/,
 65220                      string.Empty/*node*/)
 221            {
 65222            }
 223        }
 224    }
 225}