| | | 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.ObjectModel; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Dispatcher; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | [Serializable] |
| | | 14 | | [KnownType(typeof(FaultCodeData))] |
| | | 15 | | [KnownType(typeof(FaultCodeData[]))] |
| | | 16 | | [KnownType(typeof(FaultReasonData))] |
| | | 17 | | [KnownType(typeof(FaultReasonData[]))] |
| | | 18 | | public class FaultException : CommunicationException |
| | | 19 | | { |
| | | 20 | | internal const string Namespace = "http://schemas.xmlsoap.org/Microsoft/WindowsCommunicationFoundation/2005/08/F |
| | | 21 | | private readonly string _action; |
| | | 22 | | private readonly FaultCode _code; |
| | | 23 | | private readonly FaultReason _reason; |
| | | 24 | | private readonly MessageFault _fault; |
| | | 25 | | |
| | | 26 | | public FaultException() |
| | | 27 | | : base(SR.SFxFaultReason) |
| | | 28 | | { |
| | | 29 | | _code = DefaultCode; |
| | | 30 | | _reason = DefaultReason; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public FaultException(string reason) |
| | | 34 | | : base(reason) |
| | | 35 | | { |
| | | 36 | | _code = DefaultCode; |
| | | 37 | | _reason = CreateReason(reason); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public FaultException(FaultReason reason) |
| | | 41 | | : base(GetSafeReasonText(reason)) |
| | | 42 | | { |
| | | 43 | | _code = DefaultCode; |
| | | 44 | | _reason = EnsureReason(reason); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public FaultException(string reason, FaultCode code) |
| | | 48 | | : base(reason) |
| | | 49 | | { |
| | | 50 | | _code = EnsureCode(code); |
| | | 51 | | _reason = CreateReason(reason); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | public FaultException(FaultReason reason, FaultCode code) |
| | | 55 | | : base(GetSafeReasonText(reason)) |
| | | 56 | | { |
| | | 57 | | _code = EnsureCode(code); |
| | | 58 | | _reason = EnsureReason(reason); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public FaultException(string reason, FaultCode code, string action) |
| | | 62 | | : base(reason) |
| | | 63 | | { |
| | | 64 | | _code = EnsureCode(code); |
| | | 65 | | _reason = CreateReason(reason); |
| | | 66 | | _action = action; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | internal FaultException(string reason, FaultCode code, string action, Exception innerException) |
| | | 70 | | : base(reason, innerException) |
| | | 71 | | { |
| | | 72 | | _code = EnsureCode(code); |
| | | 73 | | _reason = CreateReason(reason); |
| | | 74 | | _action = action; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public FaultException(FaultReason reason, FaultCode code, string action) |
| | | 78 | | : base(GetSafeReasonText(reason)) |
| | | 79 | | { |
| | | 80 | | _code = EnsureCode(code); |
| | | 81 | | _reason = EnsureReason(reason); |
| | | 82 | | _action = action; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | internal FaultException(FaultReason reason, FaultCode code, string action, Exception innerException) |
| | | 86 | | : base(GetSafeReasonText(reason), innerException) |
| | | 87 | | { |
| | | 88 | | _code = EnsureCode(code); |
| | | 89 | | _reason = EnsureReason(reason); |
| | | 90 | | _action = action; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | public FaultException(MessageFault fault) |
| | | 94 | | : base(GetSafeReasonText(GetReason(fault))) |
| | | 95 | | { |
| | | 96 | | if (fault == null) |
| | | 97 | | { |
| | | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault)); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | _code = EnsureCode(fault.Code); |
| | | 102 | | _reason = EnsureReason(fault.Reason); |
| | | 103 | | _fault = fault; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public FaultException(MessageFault fault, string action) |
| | | 107 | | : base(GetSafeReasonText(GetReason(fault))) |
| | | 108 | | { |
| | | 109 | | if (fault == null) |
| | | 110 | | { |
| | | 111 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault)); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | _code = fault.Code; |
| | | 115 | | _reason = fault.Reason; |
| | | 116 | | _fault = fault; |
| | | 117 | | _action = action; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | protected FaultException(SerializationInfo info, StreamingContext context) |
| | | 121 | | : base(info, context) |
| | | 122 | | { |
| | | 123 | | _code = ReconstructFaultCode(info, "code"); |
| | | 124 | | _reason = ReconstructFaultReason(info, "reason"); |
| | | 125 | | _fault = (MessageFault)info.GetValue("messageFault", typeof(MessageFault)); |
| | | 126 | | _action = (string)info.GetString("action"); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public string Action |
| | | 130 | | { |
| | | 131 | | get { return _action; } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public FaultCode Code |
| | | 135 | | { |
| | | 136 | | get { return _code; } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | private static FaultReason DefaultReason |
| | | 140 | | { |
| | | 141 | | get { return new FaultReason(SR.SFxFaultReason); } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | private static FaultCode DefaultCode |
| | | 145 | | { |
| | | 146 | | get { return new FaultCode("Sender"); } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public override string Message |
| | | 150 | | { |
| | | 151 | | get { return GetSafeReasonText(Reason); } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public FaultReason Reason |
| | | 155 | | { |
| | | 156 | | get { return _reason; } |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | internal MessageFault Fault |
| | | 160 | | { |
| | | 161 | | get { return _fault; } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | internal void AddFaultCodeObjectData(SerializationInfo info, string key, FaultCode code) |
| | | 165 | | { |
| | | 166 | | info.AddValue(key, FaultCodeData.GetObjectData(code)); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | internal void AddFaultReasonObjectData(SerializationInfo info, string key, FaultReason reason) |
| | | 170 | | { |
| | | 171 | | info.AddValue(key, FaultReasonData.GetObjectData(reason)); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | private static FaultCode CreateCode(string code) |
| | | 175 | | { |
| | | 176 | | return (code != null) ? new FaultCode(code) : DefaultCode; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | public static FaultException CreateFault(MessageFault messageFault, params Type[] faultDetailTypes) |
| | | 180 | | { |
| | | 181 | | return CreateFault(messageFault, null, faultDetailTypes); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | public static FaultException CreateFault(MessageFault messageFault, string action, params Type[] faultDetailType |
| | | 185 | | { |
| | | 186 | | if (messageFault == null) |
| | | 187 | | { |
| | | 188 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageFault)); |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | if (faultDetailTypes == null) |
| | | 192 | | { |
| | | 193 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(faultDetailTypes)); |
| | | 194 | | } |
| | | 195 | | var faultFormatter = new DataContractSerializerFaultFormatter(faultDetailTypes); |
| | | 196 | | return faultFormatter.Deserialize(messageFault, action); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | public virtual MessageFault CreateMessageFault() |
| | | 200 | | { |
| | | 201 | | if (_fault != null) |
| | | 202 | | { |
| | | 203 | | return _fault; |
| | | 204 | | } |
| | | 205 | | else |
| | | 206 | | { |
| | | 207 | | return MessageFault.CreateFault(_code, _reason); |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | private static FaultReason CreateReason(string reason) |
| | | 212 | | { |
| | | 213 | | return (reason != null) ? new FaultReason(reason) : DefaultReason; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 217 | | { |
| | | 218 | | base.GetObjectData(info, context); |
| | | 219 | | AddFaultCodeObjectData(info, "code", _code); |
| | | 220 | | AddFaultReasonObjectData(info, "reason", _reason); |
| | | 221 | | info.AddValue("messageFault", _fault); |
| | | 222 | | info.AddValue("action", _action); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | private static FaultReason GetReason(MessageFault fault) |
| | | 226 | | { |
| | | 227 | | if (fault == null) |
| | | 228 | | { |
| | | 229 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault)); |
| | | 230 | | } |
| | | 231 | | return fault.Reason; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | internal static string GetSafeReasonText(MessageFault messageFault) |
| | | 235 | | { |
| | | 236 | | if (messageFault == null) |
| | | 237 | | { |
| | | 238 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageFault)); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | return GetSafeReasonText(messageFault.Reason); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | internal static string GetSafeReasonText(FaultReason reason) |
| | | 245 | | { |
| | | 246 | | if (reason == null) |
| | | 247 | | { |
| | | 248 | | return SR.SFxUnknownFaultNullReason0; |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | try |
| | | 252 | | { |
| | | 253 | | return reason.GetMatchingTranslation(System.Globalization.CultureInfo.CurrentCulture).Text; |
| | | 254 | | } |
| | | 255 | | catch (ArgumentException) |
| | | 256 | | { |
| | | 257 | | if (reason.Translations.Count == 0) |
| | | 258 | | { |
| | | 259 | | return SR.SFxUnknownFaultZeroReasons0; |
| | | 260 | | } |
| | | 261 | | else |
| | | 262 | | { |
| | | 263 | | return SR.Format(SR.SFxUnknownFaultNoMatchingTranslation1, reason.Translations[0].Text); |
| | | 264 | | } |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | private static FaultCode EnsureCode(FaultCode code) |
| | | 269 | | { |
| | | 270 | | return code ?? DefaultCode; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private static FaultReason EnsureReason(FaultReason reason) |
| | | 274 | | { |
| | | 275 | | return reason ?? DefaultReason; |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | internal FaultCode ReconstructFaultCode(SerializationInfo info, string key) |
| | | 279 | | { |
| | | 280 | | FaultCodeData[] data = (FaultCodeData[])info.GetValue(key, typeof(FaultCodeData[])); |
| | | 281 | | return FaultCodeData.Construct(data); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | internal FaultReason ReconstructFaultReason(SerializationInfo info, string key) |
| | | 285 | | { |
| | | 286 | | FaultReasonData[] data = (FaultReasonData[])info.GetValue(key, typeof(FaultReasonData[])); |
| | | 287 | | return FaultReasonData.Construct(data); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | [Serializable] |
| | | 291 | | internal class FaultCodeData |
| | | 292 | | { |
| | | 293 | | #pragma warning disable IDE1006 // Naming Styles - class is Serializable so can't change field names |
| | | 294 | | private string _name; |
| | | 295 | | private string _ns; |
| | | 296 | | #pragma warning restore IDE1006 // Naming Styles |
| | | 297 | | |
| | | 298 | | internal static FaultCode Construct(FaultCodeData[] nodes) |
| | | 299 | | { |
| | | 300 | | FaultCode code = null; |
| | | 301 | | |
| | | 302 | | for (int i = nodes.Length - 1; i >= 0; i--) |
| | | 303 | | { |
| | | 304 | | code = new FaultCode(nodes[i]._name, nodes[i]._ns, code); |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | return code; |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | internal static FaultCodeData[] GetObjectData(FaultCode code) |
| | | 311 | | { |
| | | 312 | | FaultCodeData[] array = new FaultCodeData[GetDepth(code)]; |
| | | 313 | | |
| | | 314 | | for (int i = 0; i < array.Length; i++) |
| | | 315 | | { |
| | | 316 | | array[i] = new FaultCodeData |
| | | 317 | | { |
| | | 318 | | _name = code.Name, |
| | | 319 | | _ns = code.Namespace |
| | | 320 | | }; |
| | | 321 | | code = code.SubCode; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | if (code != null) |
| | | 325 | | { |
| | | 326 | | Fx.Assert("FaultException.FaultCodeData.GetObjectData: (code != null)"); |
| | | 327 | | } |
| | | 328 | | return array; |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | private static int GetDepth(FaultCode code) |
| | | 332 | | { |
| | | 333 | | int depth = 0; |
| | | 334 | | |
| | | 335 | | while (code != null) |
| | | 336 | | { |
| | | 337 | | depth++; |
| | | 338 | | code = code.SubCode; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | return depth; |
| | | 342 | | } |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | [Serializable] |
| | | 346 | | internal class FaultReasonData |
| | | 347 | | { |
| | | 348 | | #pragma warning disable IDE1006 // Naming Styles - class is Serializable so can't change field names |
| | | 349 | | private string _xmlLang; |
| | | 350 | | private string _text; |
| | | 351 | | #pragma warning restore IDE1006 // Naming Styles |
| | | 352 | | |
| | | 353 | | internal static FaultReason Construct(FaultReasonData[] nodes) |
| | | 354 | | { |
| | | 355 | | FaultReasonText[] reasons = new FaultReasonText[nodes.Length]; |
| | | 356 | | |
| | | 357 | | for (int i = 0; i < nodes.Length; i++) |
| | | 358 | | { |
| | | 359 | | reasons[i] = new FaultReasonText(nodes[i]._text, nodes[i]._xmlLang); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | return new FaultReason(reasons); |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | internal static FaultReasonData[] GetObjectData(FaultReason reason) |
| | | 366 | | { |
| | | 367 | | ReadOnlyCollection<FaultReasonText> translations = reason.Translations; |
| | | 368 | | FaultReasonData[] array = new FaultReasonData[translations.Count]; |
| | | 369 | | |
| | | 370 | | for (int i = 0; i < translations.Count; i++) |
| | | 371 | | { |
| | | 372 | | array[i] = new FaultReasonData |
| | | 373 | | { |
| | | 374 | | _xmlLang = translations[i].XmlLang, |
| | | 375 | | _text = translations[i].Text |
| | | 376 | | }; |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | return array; |
| | | 380 | | } |
| | | 381 | | } |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | [Serializable] |
| | | 385 | | public class FaultException<TDetail> : FaultException |
| | | 386 | | { |
| | | 387 | | public FaultException(TDetail detail) |
| | 58 | 388 | | : base() |
| | | 389 | | { |
| | 58 | 390 | | Detail = detail; |
| | 58 | 391 | | } |
| | | 392 | | |
| | | 393 | | public FaultException(TDetail detail, string reason) |
| | 0 | 394 | | : base(reason) |
| | | 395 | | { |
| | 0 | 396 | | Detail = detail; |
| | 0 | 397 | | } |
| | | 398 | | |
| | | 399 | | public FaultException(TDetail detail, FaultReason reason) |
| | 6 | 400 | | : base(reason) |
| | | 401 | | { |
| | 6 | 402 | | Detail = detail; |
| | 6 | 403 | | } |
| | | 404 | | |
| | | 405 | | public FaultException(TDetail detail, string reason, FaultCode code) |
| | 0 | 406 | | : base(reason, code) |
| | | 407 | | { |
| | 0 | 408 | | Detail = detail; |
| | 0 | 409 | | } |
| | | 410 | | |
| | | 411 | | public FaultException(TDetail detail, FaultReason reason, FaultCode code) |
| | 1 | 412 | | : base(reason, code) |
| | | 413 | | { |
| | 1 | 414 | | Detail = detail; |
| | 1 | 415 | | } |
| | | 416 | | |
| | | 417 | | public FaultException(TDetail detail, string reason, FaultCode code, string action) |
| | 0 | 418 | | : base(reason, code, action) |
| | | 419 | | { |
| | 0 | 420 | | Detail = detail; |
| | 0 | 421 | | } |
| | | 422 | | |
| | | 423 | | public FaultException(TDetail detail, FaultReason reason, FaultCode code, string action) |
| | 0 | 424 | | : base(reason, code, action) |
| | | 425 | | { |
| | 0 | 426 | | Detail = detail; |
| | 0 | 427 | | } |
| | | 428 | | |
| | | 429 | | protected FaultException(SerializationInfo info, StreamingContext context) |
| | 0 | 430 | | : base(info, context) |
| | | 431 | | { |
| | 0 | 432 | | Detail = (TDetail)info.GetValue("detail", typeof(TDetail)); |
| | 0 | 433 | | } |
| | | 434 | | |
| | 65 | 435 | | public TDetail Detail { get; } |
| | | 436 | | |
| | | 437 | | public override MessageFault CreateMessageFault() |
| | | 438 | | { |
| | 0 | 439 | | return MessageFault.CreateFault(Code, Reason, Detail); |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 443 | | { |
| | 0 | 444 | | base.GetObjectData(info, context); |
| | 0 | 445 | | info.AddValue("detail", Detail); |
| | 0 | 446 | | } |
| | | 447 | | |
| | | 448 | | public override string ToString() |
| | | 449 | | { |
| | 0 | 450 | | return SR.Format(SR.SFxFaultExceptionToString3, GetType(), Message, Detail != null ? Detail.ToString() : str |
| | | 451 | | } |
| | | 452 | | } |
| | | 453 | | } |