| | | 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.Reflection; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.Collections.Generic; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Dispatcher |
| | | 14 | | { |
| | | 15 | | internal class FaultFormatter : IClientFaultFormatter, IDispatchFaultFormatter |
| | | 16 | | { |
| | | 17 | | private readonly FaultContractInfo[] _faultContractInfos; |
| | | 18 | | |
| | 0 | 19 | | internal FaultFormatter(Type[] detailTypes) |
| | | 20 | | { |
| | 0 | 21 | | List<FaultContractInfo> faultContractInfoList = new List<FaultContractInfo>(); |
| | 0 | 22 | | for (int i = 0; i < detailTypes.Length; i++) |
| | | 23 | | { |
| | 0 | 24 | | faultContractInfoList.Add(new FaultContractInfo(MessageHeaders.WildcardAction, detailTypes[i])); |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | AddInfrastructureFaults(faultContractInfoList); |
| | 0 | 28 | | _faultContractInfos = GetSortedArray(faultContractInfoList); |
| | 0 | 29 | | } |
| | | 30 | | |
| | 3676 | 31 | | internal FaultFormatter(SynchronizedCollection<FaultContractInfo> faultContractInfoCollection) |
| | | 32 | | { |
| | | 33 | | List<FaultContractInfo> faultContractInfoList; |
| | 3676 | 34 | | lock (faultContractInfoCollection.SyncRoot) |
| | | 35 | | { |
| | 3676 | 36 | | faultContractInfoList = new List<FaultContractInfo>(faultContractInfoCollection); |
| | 3676 | 37 | | } |
| | 3676 | 38 | | AddInfrastructureFaults(faultContractInfoList); |
| | 3676 | 39 | | _faultContractInfos = GetSortedArray(faultContractInfoList); |
| | 3676 | 40 | | } |
| | | 41 | | |
| | | 42 | | public MessageFault Serialize(FaultException faultException, out string action) |
| | | 43 | | { |
| | 103 | 44 | | XmlObjectSerializer serializer = null; |
| | 103 | 45 | | Type detailType = null; |
| | 103 | 46 | | string faultExceptionAction = action = faultException.Action; |
| | | 47 | | |
| | 103 | 48 | | Type faultExceptionOfT = null; |
| | 206 | 49 | | for (Type faultType = faultException.GetType(); faultType != typeof(FaultException); faultType = faultType.G |
| | | 50 | | { |
| | 65 | 51 | | if (faultType.GetTypeInfo().IsGenericType && (faultType.GetGenericTypeDefinition() == typeof(FaultExcept |
| | | 52 | | { |
| | 65 | 53 | | faultExceptionOfT = faultType; |
| | 65 | 54 | | break; |
| | | 55 | | } |
| | | 56 | | } |
| | 103 | 57 | | if (faultExceptionOfT != null) |
| | | 58 | | { |
| | 65 | 59 | | detailType = faultExceptionOfT.GetGenericArguments()[0]; |
| | 65 | 60 | | serializer = GetSerializer(detailType, faultExceptionAction, out action); |
| | | 61 | | } |
| | 103 | 62 | | return CreateMessageFault(serializer, faultException, detailType); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public FaultException Deserialize(MessageFault messageFault, string action) |
| | | 66 | | { |
| | 0 | 67 | | if (!messageFault.HasDetail) |
| | | 68 | | { |
| | 0 | 69 | | return new FaultException(messageFault, action); |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | return CreateFaultException(messageFault, action); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | protected virtual XmlObjectSerializer GetSerializer(Type detailType, string faultExceptionAction, out string act |
| | | 76 | | { |
| | 65 | 77 | | action = faultExceptionAction; |
| | 65 | 78 | | FaultContractInfo faultInfo = null; |
| | 298 | 79 | | for (int i = 0; i < _faultContractInfos.Length; i++) |
| | | 80 | | { |
| | 149 | 81 | | if (_faultContractInfos[i].Detail == detailType) |
| | | 82 | | { |
| | 65 | 83 | | faultInfo = _faultContractInfos[i]; |
| | 65 | 84 | | break; |
| | | 85 | | } |
| | | 86 | | } |
| | 65 | 87 | | if (faultInfo != null) |
| | | 88 | | { |
| | 65 | 89 | | if (action == null) |
| | | 90 | | { |
| | 65 | 91 | | action = faultInfo.Action; |
| | | 92 | | } |
| | | 93 | | |
| | 65 | 94 | | return faultInfo.Serializer; |
| | | 95 | | } |
| | | 96 | | else |
| | | 97 | | { |
| | 0 | 98 | | return DataContractSerializerDefaults.CreateSerializer(detailType, int.MaxValue /* maxItemsInObjectGraph |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | protected virtual FaultException CreateFaultException(MessageFault messageFault, string action) |
| | | 103 | | { |
| | | 104 | | IList<FaultContractInfo> faultInfos; |
| | 0 | 105 | | if (action != null) |
| | | 106 | | { |
| | 0 | 107 | | faultInfos = new List<FaultContractInfo>(); |
| | 0 | 108 | | for (int i = 0; i < _faultContractInfos.Length; i++) |
| | | 109 | | { |
| | 0 | 110 | | if (_faultContractInfos[i].Action == action || _faultContractInfos[i].Action == MessageHeaders.Wildc |
| | | 111 | | { |
| | 0 | 112 | | faultInfos.Add(_faultContractInfos[i]); |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | else |
| | | 117 | | { |
| | 0 | 118 | | faultInfos = _faultContractInfos; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | for (int i = 0; i < faultInfos.Count; i++) |
| | | 122 | | { |
| | 0 | 123 | | FaultContractInfo faultInfo = faultInfos[i]; |
| | 0 | 124 | | XmlDictionaryReader detailReader = messageFault.GetReaderAtDetailContents(); |
| | 0 | 125 | | XmlObjectSerializer serializer = faultInfo.Serializer; |
| | 0 | 126 | | if (serializer.IsStartObject(detailReader)) |
| | | 127 | | { |
| | 0 | 128 | | Type detailType = faultInfo.Detail; |
| | | 129 | | try |
| | | 130 | | { |
| | 0 | 131 | | object detailObj = serializer.ReadObject(detailReader); |
| | 0 | 132 | | FaultException faultException = CreateFaultException(messageFault, action, |
| | 0 | 133 | | detailObj, detailType, detailReader); |
| | 0 | 134 | | if (faultException != null) |
| | | 135 | | { |
| | 0 | 136 | | return faultException; |
| | | 137 | | } |
| | 0 | 138 | | } |
| | | 139 | | #pragma warning disable CA1031 // Do not catch general exception types - return a non-specific FaultException is can't d |
| | 0 | 140 | | catch (SerializationException) |
| | | 141 | | { |
| | 0 | 142 | | } |
| | | 143 | | #pragma warning restore CA1031 // Do not catch general exception types |
| | | 144 | | } |
| | | 145 | | } |
| | 0 | 146 | | return new FaultException(messageFault, action); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | protected FaultException CreateFaultException(MessageFault messageFault, string action, |
| | | 150 | | object detailObj, Type detailType, XmlDictionaryReader detailReader) |
| | | 151 | | { |
| | 0 | 152 | | if (!detailReader.EOF) |
| | | 153 | | { |
| | 0 | 154 | | detailReader.MoveToContent(); |
| | 0 | 155 | | if (detailReader.NodeType != XmlNodeType.EndElement && !detailReader.EOF) |
| | | 156 | | { |
| | 0 | 157 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.ExtraContentIsPrese |
| | | 158 | | } |
| | | 159 | | } |
| | | 160 | | bool isDetailObjectValid; |
| | 0 | 161 | | if (detailObj == null) |
| | | 162 | | { |
| | 0 | 163 | | isDetailObjectValid = !detailType.GetTypeInfo().IsValueType; |
| | | 164 | | } |
| | | 165 | | else |
| | | 166 | | { |
| | 0 | 167 | | isDetailObjectValid = detailType.IsAssignableFrom(detailObj.GetType()); |
| | | 168 | | } |
| | 0 | 169 | | if (isDetailObjectValid) |
| | | 170 | | { |
| | 0 | 171 | | Type knownFaultType = typeof(FaultException<>).MakeGenericType(detailType); |
| | 0 | 172 | | return (FaultException)Activator.CreateInstance(knownFaultType, |
| | 0 | 173 | | detailObj, |
| | 0 | 174 | | messageFault.Reason, |
| | 0 | 175 | | messageFault.Code, |
| | 0 | 176 | | action); |
| | | 177 | | } |
| | 0 | 178 | | return null; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | private static FaultContractInfo[] GetSortedArray(List<FaultContractInfo> faultContractInfoList) |
| | | 182 | | { |
| | 3676 | 183 | | FaultContractInfo[] temp = faultContractInfoList.ToArray(); |
| | 3676 | 184 | | Array.Sort<FaultContractInfo>(temp, |
| | 3676 | 185 | | delegate (FaultContractInfo x, FaultContractInfo y) |
| | 246 | 186 | | { return string.CompareOrdinal(x.Action, y.Action); } |
| | 3676 | 187 | | ); |
| | 3676 | 188 | | return temp; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | private static void AddInfrastructureFaults(List<FaultContractInfo> faultContractInfos) |
| | | 192 | | { |
| | 3676 | 193 | | faultContractInfos.Add(new FaultContractInfo(FaultCodeConstants.Actions.NetDispatcher, typeof(ExceptionDetai |
| | 3676 | 194 | | } |
| | | 195 | | |
| | | 196 | | private static MessageFault CreateMessageFault(XmlObjectSerializer serializer, FaultException faultException, Ty |
| | | 197 | | { |
| | 103 | 198 | | if (detailType == null) |
| | | 199 | | { |
| | 38 | 200 | | if (faultException.Fault != null) |
| | | 201 | | { |
| | 0 | 202 | | return faultException.Fault; |
| | | 203 | | } |
| | | 204 | | |
| | 38 | 205 | | return MessageFault.CreateFault(faultException.Code, faultException.Reason); |
| | | 206 | | } |
| | | 207 | | Fx.Assert(serializer != null, ""); |
| | | 208 | | |
| | 65 | 209 | | Type operationFaultType = typeof(OperationFault<>).MakeGenericType(detailType); |
| | 65 | 210 | | return (MessageFault)Activator.CreateInstance(operationFaultType, serializer, faultException); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | internal class OperationFault<T> : XmlObjectSerializerFault |
| | | 214 | | { |
| | | 215 | | public OperationFault(XmlObjectSerializer serializer, FaultException<T> faultException) : |
| | 65 | 216 | | base(faultException.Code, faultException.Reason, |
| | 65 | 217 | | faultException.Detail, |
| | 65 | 218 | | serializer, |
| | 65 | 219 | | string.Empty/*actor*/, |
| | 65 | 220 | | string.Empty/*node*/) |
| | | 221 | | { |
| | 65 | 222 | | } |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | } |