< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.XmlSerializerFaultFormatter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/XmlSerializerFaultFormatter.cs
Line coverage
11%
Covered lines: 5
Uncovered lines: 37
Coverable lines: 42
Total lines: 114
Line coverage: 11.9%
Branch coverage
4%
Covered branches: 1
Total branches: 24
Branch coverage: 4.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%11100%
Initialize(...)50%22100%
GetSerializer(...)0%880%
CreateFaultException(...)0%14140%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/XmlSerializerFaultFormatter.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.Runtime.Serialization;
 7using System.Xml;
 8using CoreWCF.Channels;
 9using CoreWCF.Collections.Generic;
 10using CoreWCF.Description;
 11
 12namespace CoreWCF.Dispatcher
 13{
 14    internal class XmlSerializerFaultFormatter : FaultFormatter
 15    {
 16        private SynchronizedCollection<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo> _xmlSeri
 17
 18        internal XmlSerializerFaultFormatter(Type[] detailTypes,
 19            SynchronizedCollection<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo> xmlSerialize
 020            : base(detailTypes)
 21        {
 022            Initialize(xmlSerializerFaultContractInfos);
 023        }
 24
 25        internal XmlSerializerFaultFormatter(SynchronizedCollection<FaultContractInfo> faultContractInfoCollection,
 26            SynchronizedCollection<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo> xmlSerialize
 527            : base(faultContractInfoCollection)
 28        {
 529            Initialize(xmlSerializerFaultContractInfos);
 530        }
 31
 32        private void Initialize(SynchronizedCollection<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContra
 33        {
 534            _xmlSerializerFaultContractInfos = xmlSerializerFaultContractInfos ?? throw DiagnosticUtility.ExceptionUtili
 535        }
 36
 37        protected override XmlObjectSerializer GetSerializer(Type detailType, string faultExceptionAction, out string ac
 38        {
 039            action = faultExceptionAction;
 40
 041            XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo faultInfo = null;
 042            for (int i = 0; i < _xmlSerializerFaultContractInfos.Count; i++)
 43            {
 044                if (_xmlSerializerFaultContractInfos[i].FaultContractInfo.Detail == detailType)
 45                {
 046                    faultInfo = _xmlSerializerFaultContractInfos[i];
 047                    break;
 48                }
 49            }
 050            if (faultInfo != null)
 51            {
 052                if (action == null)
 53                {
 054                    action = faultInfo.FaultContractInfo.Action;
 55                }
 56
 057                return faultInfo.Serializer;
 58            }
 59            else
 60            {
 061                return new XmlSerializerObjectSerializer(detailType);
 62            }
 63        }
 64
 65        protected override FaultException CreateFaultException(MessageFault messageFault, string action)
 66        {
 67            IList<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo> faultInfos;
 068            if (action != null)
 69            {
 070                faultInfos = new List<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo>();
 071                for (int i = 0; i < _xmlSerializerFaultContractInfos.Count; i++)
 72                {
 073                    if (_xmlSerializerFaultContractInfos[i].FaultContractInfo.Action == action
 074                        || _xmlSerializerFaultContractInfos[i].FaultContractInfo.Action == MessageHeaders.WildcardAction
 75                    {
 076                        faultInfos.Add(_xmlSerializerFaultContractInfos[i]);
 77                    }
 78                }
 79            }
 80            else
 81            {
 082                faultInfos = _xmlSerializerFaultContractInfos;
 83            }
 84
 085            for (int i = 0; i < faultInfos.Count; i++)
 86            {
 087                XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo faultInfo = faultInfos[i];
 088                XmlDictionaryReader detailReader = messageFault.GetReaderAtDetailContents();
 089                XmlObjectSerializer serializer = faultInfo.Serializer;
 90
 091                if (serializer.IsStartObject(detailReader))
 92                {
 093                    Type detailType = faultInfo.FaultContractInfo.Detail;
 94                    try
 95                    {
 096                        object detailObj = serializer.ReadObject(detailReader);
 097                        FaultException faultException = CreateFaultException(messageFault, action,
 098                            detailObj, detailType, detailReader);
 099                        if (faultException != null)
 100                        {
 0101                            return faultException;
 102                        }
 0103                    }
 104#pragma warning disable CA1031 // Do not catch general exception types - if we can't deserialie the message fault detail
 0105                    catch (SerializationException)
 106                    {
 0107                    }
 108#pragma warning restore CA1031 // Do not catch general exception types
 109                }
 110            }
 0111            return new FaultException(messageFault, action);
 0112        }
 113    }
 114}