| | | 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.Globalization; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Channels |
| | | 8 | | { |
| | | 9 | | public abstract class FaultConverter |
| | | 10 | | { |
| | | 11 | | public static FaultConverter GetDefaultFaultConverter(MessageVersion version) |
| | | 12 | | { |
| | 0 | 13 | | return new DefaultFaultConverter(version); |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | protected abstract bool OnTryCreateException(Message message, MessageFault fault, out Exception exception); |
| | | 17 | | protected abstract bool OnTryCreateFaultMessage(Exception exception, out Message message); |
| | | 18 | | |
| | | 19 | | public bool TryCreateException(Message message, MessageFault fault, out Exception exception) |
| | | 20 | | { |
| | 0 | 21 | | if (message == null) |
| | | 22 | | { |
| | 0 | 23 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 24 | | } |
| | 0 | 25 | | if (fault == null) |
| | | 26 | | { |
| | 0 | 27 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault)); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | bool created = OnTryCreateException(message, fault, out exception); |
| | | 31 | | |
| | 0 | 32 | | if (created) |
| | | 33 | | { |
| | 0 | 34 | | if (exception == null) |
| | | 35 | | { |
| | 0 | 36 | | string text = SR.Format(SR.FaultConverterDidNotCreateException, GetType().Name); |
| | 0 | 37 | | Exception error = new InvalidOperationException(text); |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | else |
| | | 42 | | { |
| | 0 | 43 | | if (exception != null) |
| | | 44 | | { |
| | 0 | 45 | | string text = SR.Format(SR.FaultConverterCreatedException, GetType().Name); |
| | 0 | 46 | | Exception error = new InvalidOperationException(text, exception); |
| | 0 | 47 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | return created; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public bool TryCreateFaultMessage(Exception exception, out Message message) |
| | | 55 | | { |
| | 0 | 56 | | bool created = OnTryCreateFaultMessage(exception, out message); |
| | | 57 | | |
| | 0 | 58 | | if (created) |
| | | 59 | | { |
| | 0 | 60 | | if (message == null) |
| | | 61 | | { |
| | 0 | 62 | | string text = SR.Format(SR.FaultConverterDidNotCreateFaultMessage, GetType().Name); |
| | 0 | 63 | | Exception error = new InvalidOperationException(text); |
| | 0 | 64 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | else |
| | | 68 | | { |
| | 0 | 69 | | if (message != null) |
| | | 70 | | { |
| | 0 | 71 | | string text = SR.Format(SR.FaultConverterCreatedFaultMessage, GetType().Name); |
| | 0 | 72 | | Exception error = new InvalidOperationException(text); |
| | 0 | 73 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error); |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | return created; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private class DefaultFaultConverter : FaultConverter |
| | | 81 | | { |
| | | 82 | | private readonly MessageVersion _version; |
| | | 83 | | |
| | 0 | 84 | | internal DefaultFaultConverter(MessageVersion version) |
| | | 85 | | { |
| | 0 | 86 | | _version = version; |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | protected override bool OnTryCreateException(Message message, MessageFault fault, out Exception exception) |
| | | 90 | | { |
| | 0 | 91 | | exception = null; |
| | | 92 | | |
| | | 93 | | // SOAP MustUnderstand |
| | 0 | 94 | | if (string.Compare(fault.Code.Namespace, _version.Envelope.Namespace, StringComparison.Ordinal) == 0 |
| | 0 | 95 | | && string.Compare(fault.Code.Name, MessageStrings.MustUnderstandFault, StringComparison.Ordinal) == |
| | | 96 | | { |
| | 0 | 97 | | exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture).Te |
| | 0 | 98 | | return true; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | bool checkSender; |
| | | 102 | | bool checkReceiver; |
| | | 103 | | FaultCode code; |
| | | 104 | | |
| | 0 | 105 | | if (_version.Envelope == EnvelopeVersion.Soap11) |
| | | 106 | | { |
| | 0 | 107 | | checkSender = true; |
| | 0 | 108 | | checkReceiver = true; |
| | 0 | 109 | | code = fault.Code; |
| | | 110 | | } |
| | | 111 | | else |
| | | 112 | | { |
| | 0 | 113 | | checkSender = fault.Code.IsSenderFault; |
| | 0 | 114 | | checkReceiver = fault.Code.IsReceiverFault; |
| | 0 | 115 | | code = fault.Code.SubCode; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | if (code == null) |
| | | 119 | | { |
| | 0 | 120 | | return false; |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | if (code.Namespace == null) |
| | | 124 | | { |
| | 0 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | if (checkSender) |
| | | 129 | | { |
| | | 130 | | // WS-Addressing |
| | 0 | 131 | | if (string.Compare(code.Namespace, _version.Addressing.Namespace, StringComparison.Ordinal) == 0) |
| | | 132 | | { |
| | 0 | 133 | | if (string.Compare(code.Name, AddressingStrings.ActionNotSupported, StringComparison.Ordinal) == |
| | | 134 | | { |
| | 0 | 135 | | exception = new ActionNotSupportedException(fault.Reason.GetMatchingTranslation(CultureInfo. |
| | 0 | 136 | | return true; |
| | | 137 | | } |
| | 0 | 138 | | else if (string.Compare(code.Name, AddressingStrings.DestinationUnreachable, StringComparison.Or |
| | | 139 | | { |
| | 0 | 140 | | exception = new EndpointNotFoundException(fault.Reason.GetMatchingTranslation(CultureInfo.Cu |
| | 0 | 141 | | return true; |
| | | 142 | | } |
| | 0 | 143 | | else if (string.Compare(code.Name, Addressing10Strings.InvalidAddressingHeader, StringComparison |
| | | 144 | | { |
| | 0 | 145 | | if (code.SubCode != null && string.Compare(code.SubCode.Namespace, _version.Addressing.Names |
| | 0 | 146 | | string.Compare(code.SubCode.Name, Addressing10Strings.InvalidCardinality, StringComparis |
| | | 147 | | { |
| | 0 | 148 | | exception = new MessageHeaderException(fault.Reason.GetMatchingTranslation(CultureInfo.C |
| | 0 | 149 | | return true; |
| | | 150 | | } |
| | | 151 | | } |
| | 0 | 152 | | else if (_version.Addressing == AddressingVersion.WSAddressing10) |
| | | 153 | | { |
| | 0 | 154 | | if (string.Compare(code.Name, Addressing10Strings.MessageAddressingHeaderRequired, StringCom |
| | | 155 | | { |
| | 0 | 156 | | exception = new MessageHeaderException(fault.Reason.GetMatchingTranslation(CultureInfo.C |
| | 0 | 157 | | return true; |
| | | 158 | | } |
| | 0 | 159 | | else if (string.Compare(code.Name, Addressing10Strings.InvalidAddressingHeader, StringCompar |
| | | 160 | | { |
| | 0 | 161 | | exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.Curren |
| | 0 | 162 | | return true; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | else |
| | | 166 | | { |
| | 0 | 167 | | if (string.Compare(code.Name, Addressing200408Strings.MessageInformationHeaderRequired, Stri |
| | | 168 | | { |
| | 0 | 169 | | exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.Curren |
| | 0 | 170 | | return true; |
| | | 171 | | } |
| | 0 | 172 | | else if (string.Compare(code.Name, Addressing200408Strings.InvalidMessageInformationHeader, |
| | | 173 | | { |
| | 0 | 174 | | exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.Curren |
| | 0 | 175 | | return true; |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | 0 | 181 | | if (checkReceiver) |
| | | 182 | | { |
| | | 183 | | // WS-Addressing |
| | 0 | 184 | | if (string.Compare(code.Namespace, _version.Addressing.Namespace, StringComparison.Ordinal) == 0) |
| | | 185 | | { |
| | 0 | 186 | | if (string.Compare(code.Name, AddressingStrings.EndpointUnavailable, StringComparison.Ordinal) = |
| | | 187 | | { |
| | 0 | 188 | | exception = new ServerTooBusyException(fault.Reason.GetMatchingTranslation(CultureInfo.Curre |
| | 0 | 189 | | return true; |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | return false; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | protected override bool OnTryCreateFaultMessage(Exception exception, out Message message) |
| | | 198 | | { |
| | | 199 | | // WSA |
| | 0 | 200 | | if (_version.Addressing == AddressingVersion.WSAddressing10) |
| | | 201 | | { |
| | 0 | 202 | | if (exception is MessageHeaderException) |
| | | 203 | | { |
| | 0 | 204 | | MessageHeaderException mhe = exception as MessageHeaderException; |
| | 0 | 205 | | if (mhe.HeaderNamespace == AddressingVersion.WSAddressing10.Namespace) |
| | | 206 | | { |
| | 0 | 207 | | message = mhe.ProvideFault(_version); |
| | 0 | 208 | | return true; |
| | | 209 | | } |
| | | 210 | | } |
| | 0 | 211 | | else if (exception is ActionMismatchAddressingException) |
| | | 212 | | { |
| | 0 | 213 | | ActionMismatchAddressingException amae = exception as ActionMismatchAddressingException; |
| | 0 | 214 | | message = amae.ProvideFault(_version); |
| | 0 | 215 | | return true; |
| | | 216 | | } |
| | | 217 | | } |
| | 0 | 218 | | if (_version.Addressing != AddressingVersion.None) |
| | | 219 | | { |
| | 0 | 220 | | if (exception is ActionNotSupportedException) |
| | | 221 | | { |
| | 0 | 222 | | ActionNotSupportedException anse = exception as ActionNotSupportedException; |
| | 0 | 223 | | message = anse.ProvideFault(_version); |
| | 0 | 224 | | return true; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | // SOAP |
| | 0 | 229 | | if (exception is MustUnderstandSoapException) |
| | | 230 | | { |
| | 0 | 231 | | MustUnderstandSoapException muse = exception as MustUnderstandSoapException; |
| | 0 | 232 | | message = muse.ProvideFault(_version); |
| | 0 | 233 | | return true; |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | message = null; |
| | 0 | 237 | | return false; |
| | | 238 | | } |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | } |