< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.SingleBodyParameterXmlSerializerMessageFormatter
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/SingleBodyParameterXmlSerializerMessageFormatter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 47
Coverable lines: 47
Total lines: 116
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%14140%
GetInputSerializers()100%110%
GetOutputSerializer(...)0%10100%
EnsureSerializers()0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/SingleBodyParameterXmlSerializerMessageFormatter.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 CoreWCF.Description;
 8
 9namespace CoreWCF.Dispatcher
 10{
 11    internal class SingleBodyParameterXmlSerializerMessageFormatter : SingleBodyParameterMessageFormatter
 12    {
 13        private XmlObjectSerializer _cachedOutputSerializer;
 14        private Type _cachedOutputSerializerType;
 15        private readonly List<Type> _knownTypes;
 16        private readonly Type _parameterType;
 17        private readonly UnwrappedTypesXmlSerializerManager _serializerManager;
 18        private XmlObjectSerializer[] _serializers;
 19        private readonly object _thisLock;
 20        private UnwrappedTypesXmlSerializerManager.TypeSerializerPair[] _typeSerializerPairs;
 21
 22        public SingleBodyParameterXmlSerializerMessageFormatter(OperationDescription operation, Type parameterType, bool
 023            : base(operation, isRequestFormatter, "XmlSerializer")
 24        {
 025            if (operation == null)
 26            {
 027                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operation));
 28            }
 29
 030            if (xsob == null)
 31            {
 032                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(xsob));
 33            }
 34
 035            _serializerManager = serializerManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(n
 036            _parameterType = parameterType ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(pa
 037            List<Type> operationTypes = new List<Type>();
 038            operationTypes.Add(parameterType);
 039            _knownTypes = new List<Type>();
 040            if (operation.KnownTypes != null)
 41            {
 042                foreach (Type knownType in operation.KnownTypes)
 43                {
 044                    _knownTypes.Add(knownType);
 045                    operationTypes.Add(knownType);
 46                }
 47            }
 48
 049            Type nullableType = SingleBodyParameterDataContractMessageFormatter.UnwrapNullableType(_parameterType);
 050            if (nullableType != _parameterType)
 51            {
 052                _knownTypes.Add(nullableType);
 053                operationTypes.Add(nullableType);
 54            }
 55
 056            _serializerManager.RegisterType(this, operationTypes);
 057            _thisLock = new object();
 058        }
 59
 60        protected override XmlObjectSerializer[] GetInputSerializers()
 61        {
 062            lock (_thisLock)
 63            {
 064                EnsureSerializers();
 065                return _serializers;
 66            }
 067        }
 68
 69        protected override XmlObjectSerializer GetOutputSerializer(Type type)
 70        {
 071            lock (_thisLock)
 72            {
 073                if (_cachedOutputSerializerType != type)
 74                {
 075                    Type typeForSerializer = GetTypeForSerializer(type, _parameterType, _knownTypes);
 076                    EnsureSerializers();
 077                    bool foundSerializer = false;
 078                    if (_typeSerializerPairs != null)
 79                    {
 080                        for (int i = 0; i < _typeSerializerPairs.Length; ++i)
 81                        {
 082                            if (typeForSerializer == _typeSerializerPairs[i].Type)
 83                            {
 084                                _cachedOutputSerializer = _typeSerializerPairs[i].Serializer;
 085                                _cachedOutputSerializerType = type;
 086                                foundSerializer = true;
 087                                break;
 88                            }
 89                        }
 90                    }
 91
 092                    if (!foundSerializer)
 93                    {
 094                        return null;
 95                    }
 96                }
 97
 098                return _cachedOutputSerializer;
 99            }
 0100        }
 101
 102        //  must be called under a lock
 103        private void EnsureSerializers()
 104        {
 0105            if (_typeSerializerPairs == null)
 106            {
 0107                _typeSerializerPairs = _serializerManager.GetOperationSerializers(this);
 0108                _serializers = new XmlObjectSerializer[_typeSerializerPairs.Length];
 0109                for (int i = 0; i < _typeSerializerPairs.Length; ++i)
 110                {
 0111                    _serializers[i] = _typeSerializerPairs[i].Serializer;
 112                }
 113            }
 0114        }
 115    }
 116}