< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.PrimitiveOperationFormatter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/PrimitiveOperationFormatter.cs
Line coverage
64%
Covered lines: 281
Uncovered lines: 153
Coverable lines: 434
Total lines: 1078
Line coverage: 64.7%
Branch coverage
64%
Covered branches: 192
Total branches: 299
Branch coverage: 64.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)93.75%161696.29%
AddToDictionary(...)100%22100%
AddToDictionary(...)100%22100%
GetActionHeader(...)33.33%6637.5%
GetReplyActionHeader(...)75%8870%
GetArrayItemName(...)27.27%111133.33%
AddToDictionary(...)83.33%66100%
IsContractSupported(...)82.14%282881.48%
AreTypesSupported(...)100%44100%
IsTypeSupported(...)93.1%292985.71%
IsArrayTypeSupported(...)94.11%171780%
SerializeRequest(...)50%4460%
SerializeReply(...)50%4460%
DeserializeReply(...)37.5%8839.13%
DeserializeRequest(...)37.5%8848.27%
DeserializeRequest(...)100%88100%
DeserializeResponse(...)56.25%161655%
DeserializeParameters(...)90%101085.71%
IsPartElements(...)0%440%
IsPartElement(...)100%11100%
DeserializeParameter(...)50%6666.66%
SerializeParameter(...)100%22100%
SerializeParameters(...)75%4471.42%
SerializeRequest(...)100%44100%
SerializeResponse(...)100%66100%
.ctor(...)100%22100%
ReadValue(...)22.72%444425%
WriteValue(...)43.33%303055.55%
.ctor(...)100%11100%
OnWriteBodyContents(...)100%11100%
.ctor(...)100%11100%
OnWriteBodyContents(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/PrimitiveOperationFormatter.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.Runtime.Serialization;
 7using System.Xml;
 8using CoreWCF.Channels;
 9using CoreWCF.Description;
 10using CoreWCF.Diagnostics;
 11using CoreWCF.Runtime;
 12
 13namespace CoreWCF.Dispatcher
 14{
 15    internal class PrimitiveOperationFormatter : IClientMessageFormatter, IDispatchMessageFormatter
 16    {
 17        internal const string NsXsi = "http://www.w3.org/2001/XMLSchema-instance";
 18        private readonly OperationDescription _operation;
 19        private readonly MessageDescription _responseMessage;
 20        private readonly MessageDescription _requestMessage;
 21        private readonly XmlDictionaryString _action;
 22        private readonly XmlDictionaryString _replyAction;
 23        private ActionHeader _actionHeaderNone;
 24        private ActionHeader _actionHeader10;
 25        private ActionHeader _replyActionHeaderNone;
 26        private ActionHeader _replyActionHeader10;
 27        private ActionHeader _replyActionHeaderAugust2004;
 28        private readonly XmlDictionaryString _requestWrapperName;
 29        private readonly XmlDictionaryString _requestWrapperNamespace;
 30        private readonly XmlDictionaryString _responseWrapperName;
 31        private readonly XmlDictionaryString _responseWrapperNamespace;
 32        private readonly PartInfo[] _requestParts;
 33        private readonly PartInfo[] _responseParts;
 34        private readonly PartInfo _returnPart;
 35        private readonly XmlDictionaryString _xsiNilLocalName;
 36        private readonly XmlDictionaryString _xsiNilNamespace;
 37
 197238        public PrimitiveOperationFormatter(OperationDescription description, bool isRpc)
 39        {
 197240            if (description == null)
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description));
 43            }
 44
 197245            OperationFormatter.Validate(description, isRpc, false/*isEncoded*/);
 46
 197247            _operation = description;
 197248            _requestMessage = description.Messages[0];
 197249            if (description.Messages.Count == 2)
 50            {
 166251                _responseMessage = description.Messages[1];
 52            }
 53
 197254            int stringCount = 3 + _requestMessage.Body.Parts.Count;
 197255            if (_responseMessage != null)
 56            {
 166257                stringCount += 2 + _responseMessage.Body.Parts.Count;
 58            }
 59
 197260            XmlDictionary dictionary = new XmlDictionary(stringCount * 2);
 61
 197262            _xsiNilLocalName = dictionary.Add("nil");
 197263            _xsiNilNamespace = dictionary.Add(NsXsi);
 64
 197265            OperationFormatter.GetActions(description, dictionary, out _action, out _replyAction);
 66
 197267            if (_requestMessage.Body.WrapperName != null)
 68            {
 197269                _requestWrapperName = AddToDictionary(dictionary, _requestMessage.Body.WrapperName);
 197270                _requestWrapperNamespace = AddToDictionary(dictionary, _requestMessage.Body.WrapperNamespace);
 71            }
 72
 197273            _requestParts = AddToDictionary(dictionary, _requestMessage.Body.Parts, isRpc);
 74
 197275            if (_responseMessage != null)
 76            {
 166277                if (_responseMessage.Body.WrapperName != null)
 78                {
 166279                    _responseWrapperName = AddToDictionary(dictionary, _responseMessage.Body.WrapperName);
 166280                    _responseWrapperNamespace = AddToDictionary(dictionary, _responseMessage.Body.WrapperNamespace);
 81                }
 82
 166283                _responseParts = AddToDictionary(dictionary, _responseMessage.Body.Parts, isRpc);
 84
 166285                if (_responseMessage.Body.ReturnValue != null && _responseMessage.Body.ReturnValue.Type != typeof(void))
 86                {
 151387                    _returnPart = AddToDictionary(dictionary, _responseMessage.Body.ReturnValue, isRpc);
 88                }
 89            }
 197290        }
 91
 92        private ActionHeader ActionHeaderNone
 93        {
 94            get
 95            {
 096                if (_actionHeaderNone == null)
 97                {
 098                    _actionHeaderNone =
 099                        ActionHeader.Create(_action, AddressingVersion.None);
 100                }
 101
 0102                return _actionHeaderNone;
 103            }
 104        }
 105
 106        private ActionHeader ActionHeader10
 107        {
 108            get
 109            {
 1110                if (_actionHeader10 == null)
 111                {
 1112                    _actionHeader10 =
 1113                        ActionHeader.Create(_action, AddressingVersion.WSAddressing10);
 114                }
 115
 1116                return _actionHeader10;
 117            }
 118        }
 119
 120        //ActionHeader ActionHeaderAugust2004
 121        //{
 122        //    get
 123        //    {
 124        //        if (actionHeaderAugust2004 == null)
 125        //        {
 126        //            actionHeaderAugust2004 =
 127        //                ActionHeader.Create(this.action, AddressingVersion.WSAddressingAugust2004);
 128        //        }
 129
 130        //        return actionHeaderAugust2004;
 131        //    }
 132        //}
 133
 134        private ActionHeader ReplyActionHeaderNone
 135        {
 136            get
 137            {
 141138                if (_replyActionHeaderNone == null)
 139                {
 136140                    _replyActionHeaderNone =
 136141                        ActionHeader.Create(_replyAction, AddressingVersion.None);
 142                }
 143
 141144                return _replyActionHeaderNone;
 145            }
 146        }
 147
 148        private ActionHeader ReplyActionHeader10
 149        {
 150            get
 151            {
 244152                if (_replyActionHeader10 == null)
 153                {
 148154                    _replyActionHeader10 =
 148155                        ActionHeader.Create(_replyAction, AddressingVersion.WSAddressing10);
 156                }
 157
 244158                return _replyActionHeader10;
 159            }
 160        }
 161
 162        ActionHeader ReplyActionHeaderAugust2004
 163        {
 164            get
 165            {
 15166                if (_replyActionHeaderAugust2004 == null)
 167                {
 15168                    _replyActionHeaderAugust2004 =
 15169                        ActionHeader.Create(_replyAction, AddressingVersion.WSAddressingAugust2004);
 170                }
 171
 15172                return _replyActionHeaderAugust2004;
 173            }
 174        }
 175
 176        private static XmlDictionaryString AddToDictionary(XmlDictionary dictionary, string s)
 177        {
 13248178            if (!dictionary.TryLookup(s, out XmlDictionaryString dictionaryString))
 179            {
 8597180                dictionaryString = dictionary.Add(s);
 181            }
 13248182            return dictionaryString;
 183        }
 184
 185        private static PartInfo[] AddToDictionary(XmlDictionary dictionary, MessagePartDescriptionCollection parts, bool
 186        {
 3634187            PartInfo[] partInfos = new PartInfo[parts.Count];
 10218188            for (int i = 0; i < parts.Count; i++)
 189            {
 1475190                partInfos[i] = AddToDictionary(dictionary, parts[i], isRpc);
 191            }
 3634192            return partInfos;
 193        }
 194
 195        private ActionHeader GetActionHeader(AddressingVersion addressing)
 196        {
 1197            if (_action == null)
 198            {
 0199                return null;
 200            }
 201
 202            //if (addressing == AddressingVersion.WSAddressingAugust2004)
 203            //{
 204            //    return ActionHeaderAugust2004;
 205            //}
 206            //else
 1207            if (addressing == AddressingVersion.WSAddressing10)
 208            {
 1209                return ActionHeader10;
 210            }
 0211            else if (addressing == AddressingVersion.None)
 212            {
 0213                return ActionHeaderNone;
 214            }
 215            else
 216            {
 0217                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0218                    new InvalidOperationException(SR.Format(SR.AddressingVersionNotSupported, addressing)));
 219            }
 220        }
 221
 222        private ActionHeader GetReplyActionHeader(AddressingVersion addressing)
 223        {
 400224            if (_replyAction == null)
 225            {
 0226                return null;
 227            }
 228
 400229            if (addressing == AddressingVersion.WSAddressingAugust2004)
 230            {
 15231                return ReplyActionHeaderAugust2004;
 232            }
 385233            else if (addressing == AddressingVersion.WSAddressing10)
 234            {
 244235                return ReplyActionHeader10;
 236            }
 141237            else if (addressing == AddressingVersion.None)
 238            {
 141239                return ReplyActionHeaderNone;
 240            }
 241            else
 242            {
 0243                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0244                    new InvalidOperationException(SR.Format(SR.AddressingVersionNotSupported, addressing)));
 245            }
 246        }
 247
 248        private static string GetArrayItemName(Type type)
 249        {
 2250            switch (type.GetTypeCode())
 251            {
 252                case TypeCode.Boolean:
 0253                    return "boolean";
 254                case TypeCode.DateTime:
 0255                    return "dateTime";
 256                case TypeCode.Decimal:
 0257                    return "decimal";
 258                case TypeCode.Int32:
 1259                    return "int";
 260                case TypeCode.Int64:
 0261                    return "long";
 262                case TypeCode.Single:
 1263                    return "float";
 264                case TypeCode.Double:
 0265                    return "double";
 266                default:
 0267                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxInvali
 268            }
 269        }
 270
 271        private static PartInfo AddToDictionary(XmlDictionary dictionary, MessagePartDescription part, bool isRpc)
 272        {
 2988273            Type type = part.Type;
 2988274            XmlDictionaryString itemName = null;
 2988275            XmlDictionaryString itemNamespace = null;
 2988276            if (type.IsArray && type != typeof(byte[]))
 277            {
 278                const string ns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
 2279                string name = GetArrayItemName(type.GetElementType());
 2280                itemName = AddToDictionary(dictionary, name);
 2281                itemNamespace = AddToDictionary(dictionary, ns);
 282            }
 2988283            return new PartInfo(part,
 2988284                AddToDictionary(dictionary, part.Name),
 2988285                AddToDictionary(dictionary, isRpc ? string.Empty : part.Namespace),
 2988286                itemName, itemNamespace);
 287        }
 288
 289        public static bool IsContractSupported(OperationDescription description)
 290        {
 2789291            if (description == null)
 292            {
 0293                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(description));
 294            }
 295
 2789296            MessageDescription requestMessage = description.Messages[0];
 2789297            MessageDescription responseMessage = null;
 2789298            if (description.Messages.Count == 2)
 299            {
 2475300                responseMessage = description.Messages[1];
 301            }
 302
 2789303            if (requestMessage.Headers.Count > 0)
 304            {
 91305                return false;
 306            }
 307
 2698308            if (requestMessage.Properties.Count > 0)
 309            {
 0310                return false;
 311            }
 312
 2698313            if (requestMessage.IsTypedMessage)
 314            {
 5315                return false;
 316            }
 317
 2693318            if (responseMessage != null)
 319            {
 2379320                if (responseMessage.Headers.Count > 0)
 321                {
 0322                    return false;
 323                }
 324
 2379325                if (responseMessage.Properties.Count > 0)
 326                {
 0327                    return false;
 328                }
 329
 2379330                if (responseMessage.IsTypedMessage)
 331                {
 0332                    return false;
 333                }
 334            }
 2693335            if (!AreTypesSupported(requestMessage.Body.Parts))
 336            {
 666337                return false;
 338            }
 339
 2027340            if (responseMessage != null)
 341            {
 1717342                if (!AreTypesSupported(responseMessage.Body.Parts))
 343                {
 1344                    return false;
 345                }
 346
 1716347                if (responseMessage.Body.ReturnValue != null && !IsTypeSupported(responseMessage.Body.ReturnValue))
 348                {
 54349                    return false;
 350                }
 351            }
 1972352            return true;
 353        }
 354
 355        private static bool AreTypesSupported(MessagePartDescriptionCollection bodyDescriptions)
 356        {
 11784357            for (int i = 0; i < bodyDescriptions.Count; i++)
 358            {
 2149359                if (!IsTypeSupported(bodyDescriptions[i]))
 360                {
 667361                    return false;
 362                }
 363            }
 364
 3743365            return true;
 366        }
 367
 368        private static bool IsTypeSupported(MessagePartDescription bodyDescription)
 369        {
 370            Fx.Assert(bodyDescription != null, "");
 3865371            Type type = bodyDescription.Type;
 3865372            if (type == null)
 373            {
 0374                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx
 375            }
 376
 3865377            if (bodyDescription.Multiple)
 378            {
 0379                return false;
 380            }
 381
 3865382            if (type == typeof(void))
 383            {
 149384                return true;
 385            }
 386
 3716387            if (type.GetTypeInfo().IsEnum)
 388            {
 1389                return false;
 390            }
 391
 3715392            switch (type.GetTypeCode())
 393            {
 394                case TypeCode.Boolean:
 395                case TypeCode.DateTime:
 396                case TypeCode.Decimal:
 397                case TypeCode.Double:
 398                case TypeCode.Int32:
 399                case TypeCode.Int64:
 400                case TypeCode.Single:
 401                case TypeCode.String:
 2940402                    return true;
 403                case TypeCode.Object:
 762404                    if (type.IsArray && type.GetArrayRank() == 1 && IsArrayTypeSupported(type.GetElementType()))
 405                    {
 55406                        return true;
 407                    }
 408
 409                    break;
 410                default:
 411                    break;
 412            }
 720413            return false;
 414        }
 415
 416        private static bool IsArrayTypeSupported(Type type)
 417        {
 59418            if (type.GetTypeInfo().IsEnum)
 419            {
 0420                return false;
 421            }
 422
 59423            switch (type.GetTypeCode())
 424            {
 425                case TypeCode.Byte:
 426                case TypeCode.Boolean:
 427                case TypeCode.DateTime:
 428                case TypeCode.Decimal:
 429                case TypeCode.Int32:
 430                case TypeCode.Int64:
 431                case TypeCode.Single:
 432                case TypeCode.Double:
 55433                    return true;
 434                default:
 4435                    return false;
 436            }
 437        }
 438
 439        public Message SerializeRequest(MessageVersion messageVersion, object[] parameters)
 440        {
 1441            if (messageVersion == null)
 442            {
 0443                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageVersion));
 444            }
 445
 1446            if (parameters == null)
 447            {
 0448                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters));
 449            }
 450
 1451            return Message.CreateMessage(messageVersion, GetActionHeader(messageVersion.Addressing), new PrimitiveReques
 452        }
 453
 454        public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
 455        {
 400456            if (messageVersion == null)
 457            {
 0458                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageVersion));
 459            }
 460
 400461            if (parameters == null)
 462            {
 0463                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters));
 464            }
 465
 400466            return Message.CreateMessage(messageVersion, GetReplyActionHeader(messageVersion.Addressing), new PrimitiveR
 467        }
 468
 469        public object DeserializeReply(Message message, object[] parameters)
 470        {
 1471            if (message == null)
 472            {
 0473                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(message)));
 474            }
 475
 1476            if (parameters == null)
 477            {
 0478                throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters)), message);
 479            }
 480
 481            try
 482            {
 1483                if (message.IsEmpty)
 484                {
 0485                    if (_responseWrapperName == null)
 486                    {
 0487                        return null;
 488                    }
 489
 0490                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.SFxInvalidMe
 491                }
 492
 1493                XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
 1494                using (bodyReader)
 495                {
 1496                    object returnValue = DeserializeResponse(bodyReader, parameters);
 1497                    message.ReadFromBodyContentsToEnd(bodyReader);
 1498                    return returnValue;
 499                }
 500            }
 0501            catch (XmlException xe)
 502            {
 0503                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
 0504                    SR.Format(SR.SFxErrorDeserializingReplyBodyMore, _operation.Name, xe.Message), xe));
 505            }
 0506            catch (FormatException fe)
 507            {
 0508                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
 0509                    SR.Format(SR.SFxErrorDeserializingReplyBodyMore, _operation.Name, fe.Message), fe));
 510            }
 0511            catch (SerializationException se)
 512            {
 0513                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
 0514                    SR.Format(SR.SFxErrorDeserializingReplyBodyMore, _operation.Name, se.Message), se));
 515            }
 1516        }
 517
 518        public void DeserializeRequest(Message message, object[] parameters)
 519        {
 2263520            if (message == null)
 521            {
 0522                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(message)));
 523            }
 524
 2263525            if (parameters == null)
 526            {
 0527                throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters)), message);
 528            }
 529
 530            try
 531            {
 2263532                if (message.IsEmpty)
 533                {
 0534                    if (_requestWrapperName == null)
 535                    {
 0536                        return;
 537                    }
 538
 0539                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.SFxInvalidMe
 540                }
 541
 2263542                XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
 2263543                using (bodyReader)
 544                {
 2263545                    DeserializeRequest(bodyReader, parameters);
 2262546                    message.ReadFromBodyContentsToEnd(bodyReader);
 2262547                }
 2262548            }
 0549            catch (XmlException xe)
 550            {
 0551                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0552                    NetDispatcherFaultException.CreateDeserializationFailedFault(
 0553                        SR.Format(SR.SFxErrorDeserializingRequestBodyMore, _operation.Name, xe.Message),
 0554                        xe));
 555            }
 0556            catch (FormatException fe)
 557            {
 0558                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0559                    NetDispatcherFaultException.CreateDeserializationFailedFault(
 0560                        SR.Format(SR.SFxErrorDeserializingRequestBodyMore, _operation.Name, fe.Message),
 0561                        fe));
 562            }
 1563            catch (SerializationException se)
 564            {
 1565                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
 1566                    SR.Format(SR.SFxErrorDeserializingRequestBodyMore, _operation.Name, se.Message),
 1567                    se));
 568            }
 2262569        }
 570
 571        private void DeserializeRequest(XmlDictionaryReader reader, object[] parameters)
 572        {
 2263573            if (_requestWrapperName != null)
 574            {
 2263575                if (!reader.IsStartElement(_requestWrapperName, _requestWrapperNamespace))
 576                {
 1577                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.SF
 578                }
 579
 2262580                bool isEmptyElement = reader.IsEmptyElement;
 2262581                reader.Read();
 2262582                if (isEmptyElement)
 583                {
 138584                    return;
 585                }
 586            }
 587
 2124588            DeserializeParameters(reader, _requestParts, parameters);
 589
 2124590            if (_requestWrapperName != null)
 591            {
 2124592                reader.ReadEndElement();
 593            }
 2124594        }
 595
 596        private object DeserializeResponse(XmlDictionaryReader reader, object[] parameters)
 597        {
 1598            if (_responseWrapperName != null)
 599            {
 1600                if (!reader.IsStartElement(_responseWrapperName, _responseWrapperNamespace))
 601                {
 0602                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.SF
 603                }
 604
 1605                bool isEmptyElement = reader.IsEmptyElement;
 1606                reader.Read();
 1607                if (isEmptyElement)
 608                {
 0609                    return null;
 610                }
 611            }
 612
 1613            object returnValue = null;
 1614            if (_returnPart != null)
 615            {
 0616                while (true)
 617                {
 0618                    if (IsPartElement(reader, _returnPart))
 619                    {
 0620                        returnValue = DeserializeParameter(reader, _returnPart);
 0621                        break;
 622                    }
 0623                    if (!reader.IsStartElement())
 624                    {
 625                        break;
 626                    }
 627
 0628                    if (IsPartElements(reader, _responseParts))
 629                    {
 630                        break;
 631                    }
 632
 0633                    OperationFormatter.TraceAndSkipElement(reader);
 634                }
 635            }
 1636            DeserializeParameters(reader, _responseParts, parameters);
 637
 1638            if (_responseWrapperName != null)
 639            {
 1640                reader.ReadEndElement();
 641            }
 642
 1643            return returnValue;
 644        }
 645
 646        private void DeserializeParameters(XmlDictionaryReader reader, PartInfo[] parts, object[] parameters)
 647        {
 2125648            if (parts.Length != parameters.Length)
 649            {
 0650                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0651                    new ArgumentException(SR.Format(SR.SFxParameterCountMismatch, "parts", parts.Length, "parameters", p
 652            }
 653
 2125654            int nextPartIndex = 0;
 4196655            while (reader.IsStartElement())
 656            {
 8322657                for (int i = nextPartIndex; i < parts.Length; i++)
 658                {
 2090659                    PartInfo part = parts[i];
 2090660                    if (IsPartElement(reader, part))
 661                    {
 2086662                        parameters[part.Description.Index] = DeserializeParameter(reader, parts[i]);
 2086663                        nextPartIndex = i + 1;
 664                    }
 665                    else
 666                    {
 4667                        parameters[part.Description.Index] = null;
 668                    }
 669                }
 670
 2071671                if (reader.IsStartElement())
 672                {
 4673                    OperationFormatter.TraceAndSkipElement(reader);
 674                }
 675            }
 2125676        }
 677
 678        private bool IsPartElements(XmlDictionaryReader reader, PartInfo[] parts)
 679        {
 0680            foreach (PartInfo part in parts)
 681            {
 0682                if (IsPartElement(reader, part))
 683                {
 0684                    return true;
 685                }
 686            }
 687
 0688            return false;
 689        }
 690
 691        private bool IsPartElement(XmlDictionaryReader reader, PartInfo part)
 692        {
 2090693            return reader.IsStartElement(part.DictionaryName, part.DictionaryNamespace);
 694        }
 695
 696        private object DeserializeParameter(XmlDictionaryReader reader, PartInfo part)
 697        {
 2086698            if (reader.AttributeCount > 0 &&
 2086699                reader.MoveToAttribute(_xsiNilLocalName.Value, _xsiNilNamespace.Value) &&
 2086700                reader.ReadContentAsBoolean())
 701            {
 0702                reader.Skip();
 0703                return null;
 704            }
 2086705            return part.ReadValue(reader);
 706        }
 707
 708        private void SerializeParameter(XmlDictionaryWriter writer, PartInfo part, object graph)
 709        {
 383710            writer.WriteStartElement(part.DictionaryName, part.DictionaryNamespace);
 383711            if (graph == null)
 712            {
 1713                writer.WriteStartAttribute(_xsiNilLocalName, _xsiNilNamespace);
 1714                writer.WriteValue(true);
 1715                writer.WriteEndAttribute();
 716            }
 717            else
 718            {
 382719                part.WriteValue(writer, graph);
 720            }
 721
 383722            writer.WriteEndElement();
 383723        }
 724
 725        private void SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, object[] parameters)
 726        {
 401727            if (parts.Length != parameters.Length)
 728            {
 0729                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0730                    new ArgumentException(SR.Format(SR.SFxParameterCountMismatch, "parts", parts.Length, "parameters", p
 731            }
 732
 816733            for (int i = 0; i < parts.Length; i++)
 734            {
 7735                PartInfo part = parts[i];
 7736                SerializeParameter(writer, part, parameters[part.Description.Index]);
 737            }
 401738        }
 739
 740        private void SerializeRequest(XmlDictionaryWriter writer, object[] parameters)
 741        {
 1742            if (_requestWrapperName != null)
 743            {
 1744                writer.WriteStartElement(_requestWrapperName, _requestWrapperNamespace);
 745            }
 746
 1747            SerializeParameters(writer, _requestParts, parameters);
 748
 1749            if (_requestWrapperName != null)
 750            {
 1751                writer.WriteEndElement();
 752            }
 1753        }
 754
 755        private void SerializeResponse(XmlDictionaryWriter writer, object returnValue, object[] parameters)
 756        {
 400757            if (_responseWrapperName != null)
 758            {
 400759                writer.WriteStartElement(_responseWrapperName, _responseWrapperNamespace);
 760            }
 761
 400762            if (_returnPart != null)
 763            {
 376764                SerializeParameter(writer, _returnPart, returnValue);
 765            }
 766
 400767            SerializeParameters(writer, _responseParts, parameters);
 768
 400769            if (_responseWrapperName != null)
 770            {
 400771                writer.WriteEndElement();
 772            }
 400773        }
 774
 775        private class PartInfo
 776        {
 777            private readonly XmlDictionaryString _itemName;
 778            private readonly XmlDictionaryString _itemNamespace;
 779            private readonly TypeCode _typeCode;
 780            private readonly bool _isArray;
 781
 2988782            public PartInfo(MessagePartDescription description, XmlDictionaryString dictionaryName, XmlDictionaryString 
 783            {
 2988784                DictionaryName = dictionaryName;
 2988785                DictionaryNamespace = dictionaryNamespace;
 2988786                _itemName = itemName;
 2988787                _itemNamespace = itemNamespace;
 2988788                Description = description;
 2988789                if (description.Type.IsArray)
 790                {
 55791                    _isArray = true;
 55792                    _typeCode = description.Type.GetElementType().GetTypeCode();
 793                }
 794                else
 795                {
 2933796                    _isArray = false;
 2933797                    _typeCode = description.Type.GetTypeCode();
 798                }
 2933799            }
 800
 2097801            public MessagePartDescription Description { get; }
 802
 2473803            public XmlDictionaryString DictionaryName { get; }
 804
 2473805            public XmlDictionaryString DictionaryNamespace { get; }
 806
 807            public object ReadValue(XmlDictionaryReader reader)
 808            {
 809                object value;
 2086810                if (_isArray)
 811                {
 30812                    switch (_typeCode)
 813                    {
 814                        case TypeCode.Byte:
 26815                            value = reader.ReadElementContentAsBase64();
 26816                            break;
 817                        case TypeCode.Boolean:
 0818                            if (!reader.IsEmptyElement)
 819                            {
 0820                                reader.ReadStartElement();
 0821                                value = reader.ReadBooleanArray(_itemName, _itemNamespace);
 0822                                reader.ReadEndElement();
 823                            }
 824                            else
 825                            {
 0826                                reader.Read();
 0827                                value = Array.Empty<bool>();
 828                            }
 0829                            break;
 830                        case TypeCode.DateTime:
 0831                            if (!reader.IsEmptyElement)
 832                            {
 0833                                reader.ReadStartElement();
 0834                                value = reader.ReadDateTimeArray(_itemName, _itemNamespace);
 0835                                reader.ReadEndElement();
 836                            }
 837                            else
 838                            {
 0839                                reader.Read();
 0840                                value = Array.Empty<DateTime>();
 841                            }
 0842                            break;
 843                        case TypeCode.Decimal:
 0844                            if (!reader.IsEmptyElement)
 845                            {
 0846                                reader.ReadStartElement();
 0847                                value = reader.ReadDecimalArray(_itemName, _itemNamespace);
 0848                                reader.ReadEndElement();
 849                            }
 850                            else
 851                            {
 0852                                reader.Read();
 0853                                value = Array.Empty<decimal>();
 854                            }
 0855                            break;
 856                        case TypeCode.Int32:
 4857                            if (!reader.IsEmptyElement)
 858                            {
 3859                                reader.ReadStartElement();
 3860                                value = reader.ReadInt32Array(_itemName, _itemNamespace);
 3861                                reader.ReadEndElement();
 862                            }
 863                            else
 864                            {
 1865                                reader.Read();
 1866                                value = Array.Empty<int>();
 867                            }
 1868                            break;
 869                        case TypeCode.Int64:
 0870                            if (!reader.IsEmptyElement)
 871                            {
 0872                                reader.ReadStartElement();
 0873                                value = reader.ReadInt64Array(_itemName, _itemNamespace);
 0874                                reader.ReadEndElement();
 875                            }
 876                            else
 877                            {
 0878                                reader.Read();
 0879                                value = Array.Empty<long>();
 880                            }
 0881                            break;
 882                        case TypeCode.Single:
 0883                            if (!reader.IsEmptyElement)
 884                            {
 0885                                reader.ReadStartElement();
 0886                                value = reader.ReadSingleArray(_itemName, _itemNamespace);
 0887                                reader.ReadEndElement();
 888                            }
 889                            else
 890                            {
 0891                                reader.Read();
 0892                                value = Array.Empty<float>();
 893                            }
 0894                            break;
 895                        case TypeCode.Double:
 0896                            if (!reader.IsEmptyElement)
 897                            {
 0898                                reader.ReadStartElement();
 0899                                value = reader.ReadDoubleArray(_itemName, _itemNamespace);
 0900                                reader.ReadEndElement();
 901                            }
 902                            else
 903                            {
 0904                                reader.Read();
 0905                                value = Array.Empty<double>();
 906                            }
 0907                            break;
 908                        default:
 0909                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.S
 910                    }
 911                }
 912                else
 913                {
 2056914                    switch (_typeCode)
 915                    {
 916                        case TypeCode.Boolean:
 4917                            value = reader.ReadElementContentAsBoolean();
 4918                            break;
 919                        case TypeCode.DateTime:
 0920                            value = reader.ReadElementContentAsDateTime();
 0921                            break;
 922                        case TypeCode.Decimal:
 0923                            value = reader.ReadElementContentAsDecimal();
 0924                            break;
 925                        case TypeCode.Double:
 0926                            value = reader.ReadElementContentAsDouble();
 0927                            break;
 928                        case TypeCode.Int32:
 24929                            value = reader.ReadElementContentAsInt();
 24930                            break;
 931                        case TypeCode.Int64:
 0932                            value = reader.ReadElementContentAsLong();
 0933                            break;
 934                        case TypeCode.Single:
 0935                            value = reader.ReadElementContentAsFloat();
 0936                            break;
 937                        case TypeCode.String:
 2028938                            return reader.ReadElementContentAsString();
 939                        default:
 0940                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.S
 941                    }
 942                }
 58943                return value;
 944            }
 945
 946            public void WriteValue(XmlDictionaryWriter writer, object value)
 947            {
 382948                if (_isArray)
 949                {
 28950                    switch (_typeCode)
 951                    {
 952                        case TypeCode.Byte:
 953                            {
 27954                                byte[] arrayValue = (byte[])value;
 27955                                writer.WriteBase64(arrayValue, 0, arrayValue.Length);
 956                            }
 27957                            break;
 958                        case TypeCode.Boolean:
 959                            {
 0960                                bool[] arrayValue = (bool[])value;
 0961                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 962                            }
 0963                            break;
 964                        case TypeCode.DateTime:
 965                            {
 0966                                DateTime[] arrayValue = (DateTime[])value;
 0967                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 968                            }
 0969                            break;
 970                        case TypeCode.Decimal:
 971                            {
 0972                                decimal[] arrayValue = (decimal[])value;
 0973                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 974                            }
 0975                            break;
 976                        case TypeCode.Int32:
 977                            {
 0978                                int[] arrayValue = (int[])value;
 0979                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 980                            }
 0981                            break;
 982                        case TypeCode.Int64:
 983                            {
 0984                                long[] arrayValue = (long[])value;
 0985                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 986                            }
 0987                            break;
 988                        case TypeCode.Single:
 989                            {
 1990                                float[] arrayValue = (float[])value;
 1991                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 992                            }
 1993                            break;
 994                        case TypeCode.Double:
 995                            {
 0996                                double[] arrayValue = (double[])value;
 0997                                writer.WriteArray(null, _itemName, _itemNamespace, arrayValue, 0, arrayValue.Length);
 998                            }
 0999                            break;
 1000                        default:
 01001                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.S
 1002                    }
 1003                }
 1004                else
 1005                {
 3541006                    switch (_typeCode)
 1007                    {
 1008                        case TypeCode.Boolean:
 41009                            writer.WriteValue((bool)value);
 41010                            break;
 1011                        case TypeCode.DateTime:
 11012                            writer.WriteValue((DateTime)value);
 11013                            break;
 1014                        case TypeCode.Decimal:
 11015                            writer.WriteValue((decimal)value);
 11016                            break;
 1017                        case TypeCode.Double:
 11018                            writer.WriteValue((double)value);
 11019                            break;
 1020                        case TypeCode.Int32:
 171021                            writer.WriteValue((int)value);
 171022                            break;
 1023                        case TypeCode.Int64:
 31024                            writer.WriteValue((long)value);
 31025                            break;
 1026                        case TypeCode.Single:
 11027                            writer.WriteValue((float)value);
 11028                            break;
 1029                        case TypeCode.String:
 3261030                            writer.WriteString((string)value);
 3261031                            break;
 1032                        default:
 01033                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.S
 1034                    }
 1035                }
 1036            }
 1037        }
 1038
 1039        private class PrimitiveRequestBodyWriter : BodyWriter
 1040        {
 1041            private readonly object[] _parameters;
 1042            private readonly PrimitiveOperationFormatter _primitiveOperationFormatter;
 1043
 1044            public PrimitiveRequestBodyWriter(object[] parameters, PrimitiveOperationFormatter primitiveOperationFormatt
 11045                : base(true)
 1046            {
 11047                _parameters = parameters;
 11048                _primitiveOperationFormatter = primitiveOperationFormatter;
 11049            }
 1050
 1051            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 1052            {
 11053                _primitiveOperationFormatter.SerializeRequest(writer, _parameters);
 11054            }
 1055        }
 1056
 1057        private class PrimitiveResponseBodyWriter : BodyWriter
 1058        {
 1059            private readonly object[] _parameters;
 1060            private readonly object _returnValue;
 1061            private readonly PrimitiveOperationFormatter _primitiveOperationFormatter;
 1062
 1063            public PrimitiveResponseBodyWriter(object[] parameters, object returnValue,
 1064                PrimitiveOperationFormatter primitiveOperationFormatter)
 4001065                : base(true)
 1066            {
 4001067                _parameters = parameters;
 4001068                _returnValue = returnValue;
 4001069                _primitiveOperationFormatter = primitiveOperationFormatter;
 4001070            }
 1071
 1072            protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 1073            {
 4001074                _primitiveOperationFormatter.SerializeResponse(writer, _returnValue, _parameters);
 4001075            }
 1076        }
 1077    }
 1078}

Methods/Properties

.ctor(CoreWCF.Description.OperationDescription,System.Boolean)
ActionHeaderNone()
ActionHeader10()
ReplyActionHeaderNone()
ReplyActionHeader10()
ReplyActionHeaderAugust2004()
AddToDictionary(System.Xml.XmlDictionary,System.String)
AddToDictionary(System.Xml.XmlDictionary,CoreWCF.Description.MessagePartDescriptionCollection,System.Boolean)
GetActionHeader(CoreWCF.Channels.AddressingVersion)
GetReplyActionHeader(CoreWCF.Channels.AddressingVersion)
GetArrayItemName(System.Type)
AddToDictionary(System.Xml.XmlDictionary,CoreWCF.Description.MessagePartDescription,System.Boolean)
IsContractSupported(CoreWCF.Description.OperationDescription)
AreTypesSupported(CoreWCF.Description.MessagePartDescriptionCollection)
IsTypeSupported(CoreWCF.Description.MessagePartDescription)
IsArrayTypeSupported(System.Type)
SerializeRequest(CoreWCF.Channels.MessageVersion,System.Object[])
SerializeReply(CoreWCF.Channels.MessageVersion,System.Object[],System.Object)
DeserializeReply(CoreWCF.Channels.Message,System.Object[])
DeserializeRequest(CoreWCF.Channels.Message,System.Object[])
DeserializeRequest(System.Xml.XmlDictionaryReader,System.Object[])
DeserializeResponse(System.Xml.XmlDictionaryReader,System.Object[])
DeserializeParameters(System.Xml.XmlDictionaryReader,CoreWCF.Dispatcher.PrimitiveOperationFormatter/PartInfo[],System.Object[])
IsPartElements(System.Xml.XmlDictionaryReader,CoreWCF.Dispatcher.PrimitiveOperationFormatter/PartInfo[])
IsPartElement(System.Xml.XmlDictionaryReader,CoreWCF.Dispatcher.PrimitiveOperationFormatter/PartInfo)
DeserializeParameter(System.Xml.XmlDictionaryReader,CoreWCF.Dispatcher.PrimitiveOperationFormatter/PartInfo)
SerializeParameter(System.Xml.XmlDictionaryWriter,CoreWCF.Dispatcher.PrimitiveOperationFormatter/PartInfo,System.Object)
SerializeParameters(System.Xml.XmlDictionaryWriter,CoreWCF.Dispatcher.PrimitiveOperationFormatter/PartInfo[],System.Object[])
SerializeRequest(System.Xml.XmlDictionaryWriter,System.Object[])
SerializeResponse(System.Xml.XmlDictionaryWriter,System.Object,System.Object[])
.ctor(CoreWCF.Description.MessagePartDescription,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString)
Description()
DictionaryName()
DictionaryNamespace()
ReadValue(System.Xml.XmlDictionaryReader)
WriteValue(System.Xml.XmlDictionaryWriter,System.Object)
.ctor(System.Object[],CoreWCF.Dispatcher.PrimitiveOperationFormatter)
OnWriteBodyContents(System.Xml.XmlDictionaryWriter)
.ctor(System.Object[],System.Object,CoreWCF.Dispatcher.PrimitiveOperationFormatter)
OnWriteBodyContents(System.Xml.XmlDictionaryWriter)