| | | 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 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Collections.Generic; |
| | | 10 | | using CoreWCF.Description; |
| | | 11 | | |
| | | 12 | | namespace 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 |
| | 0 | 20 | | : base(detailTypes) |
| | | 21 | | { |
| | 0 | 22 | | Initialize(xmlSerializerFaultContractInfos); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | internal XmlSerializerFaultFormatter(SynchronizedCollection<FaultContractInfo> faultContractInfoCollection, |
| | | 26 | | SynchronizedCollection<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo> xmlSerialize |
| | 5 | 27 | | : base(faultContractInfoCollection) |
| | | 28 | | { |
| | 5 | 29 | | Initialize(xmlSerializerFaultContractInfos); |
| | 5 | 30 | | } |
| | | 31 | | |
| | | 32 | | private void Initialize(SynchronizedCollection<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContra |
| | | 33 | | { |
| | 5 | 34 | | _xmlSerializerFaultContractInfos = xmlSerializerFaultContractInfos ?? throw DiagnosticUtility.ExceptionUtili |
| | 5 | 35 | | } |
| | | 36 | | |
| | | 37 | | protected override XmlObjectSerializer GetSerializer(Type detailType, string faultExceptionAction, out string ac |
| | | 38 | | { |
| | 0 | 39 | | action = faultExceptionAction; |
| | | 40 | | |
| | 0 | 41 | | XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo faultInfo = null; |
| | 0 | 42 | | for (int i = 0; i < _xmlSerializerFaultContractInfos.Count; i++) |
| | | 43 | | { |
| | 0 | 44 | | if (_xmlSerializerFaultContractInfos[i].FaultContractInfo.Detail == detailType) |
| | | 45 | | { |
| | 0 | 46 | | faultInfo = _xmlSerializerFaultContractInfos[i]; |
| | 0 | 47 | | break; |
| | | 48 | | } |
| | | 49 | | } |
| | 0 | 50 | | if (faultInfo != null) |
| | | 51 | | { |
| | 0 | 52 | | if (action == null) |
| | | 53 | | { |
| | 0 | 54 | | action = faultInfo.FaultContractInfo.Action; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | return faultInfo.Serializer; |
| | | 58 | | } |
| | | 59 | | else |
| | | 60 | | { |
| | 0 | 61 | | return new XmlSerializerObjectSerializer(detailType); |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | protected override FaultException CreateFaultException(MessageFault messageFault, string action) |
| | | 66 | | { |
| | | 67 | | IList<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo> faultInfos; |
| | 0 | 68 | | if (action != null) |
| | | 69 | | { |
| | 0 | 70 | | faultInfos = new List<XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo>(); |
| | 0 | 71 | | for (int i = 0; i < _xmlSerializerFaultContractInfos.Count; i++) |
| | | 72 | | { |
| | 0 | 73 | | if (_xmlSerializerFaultContractInfos[i].FaultContractInfo.Action == action |
| | 0 | 74 | | || _xmlSerializerFaultContractInfos[i].FaultContractInfo.Action == MessageHeaders.WildcardAction |
| | | 75 | | { |
| | 0 | 76 | | faultInfos.Add(_xmlSerializerFaultContractInfos[i]); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | else |
| | | 81 | | { |
| | 0 | 82 | | faultInfos = _xmlSerializerFaultContractInfos; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | for (int i = 0; i < faultInfos.Count; i++) |
| | | 86 | | { |
| | 0 | 87 | | XmlSerializerOperationBehavior.Reflector.XmlSerializerFaultContractInfo faultInfo = faultInfos[i]; |
| | 0 | 88 | | XmlDictionaryReader detailReader = messageFault.GetReaderAtDetailContents(); |
| | 0 | 89 | | XmlObjectSerializer serializer = faultInfo.Serializer; |
| | | 90 | | |
| | 0 | 91 | | if (serializer.IsStartObject(detailReader)) |
| | | 92 | | { |
| | 0 | 93 | | Type detailType = faultInfo.FaultContractInfo.Detail; |
| | | 94 | | try |
| | | 95 | | { |
| | 0 | 96 | | object detailObj = serializer.ReadObject(detailReader); |
| | 0 | 97 | | FaultException faultException = CreateFaultException(messageFault, action, |
| | 0 | 98 | | detailObj, detailType, detailReader); |
| | 0 | 99 | | if (faultException != null) |
| | | 100 | | { |
| | 0 | 101 | | return faultException; |
| | | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | #pragma warning disable CA1031 // Do not catch general exception types - if we can't deserialie the message fault detail |
| | 0 | 105 | | catch (SerializationException) |
| | | 106 | | { |
| | 0 | 107 | | } |
| | | 108 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 109 | | } |
| | | 110 | | } |
| | 0 | 111 | | return new FaultException(messageFault, action); |
| | 0 | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |