| | | 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.Globalization; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Diagnostics; |
| | | 10 | | using CoreWCF.Dispatcher; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels |
| | | 13 | | { |
| | | 14 | | public abstract class MessageFault |
| | | 15 | | { |
| | | 16 | | internal static MessageFault CreateFault(FaultCode code, string reason) |
| | | 17 | | { |
| | 2 | 18 | | return CreateFault(code, new FaultReason(reason)); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | internal static MessageFault CreateFault(FaultCode code, FaultReason reason) |
| | | 22 | | { |
| | 142 | 23 | | return CreateFault(code, reason, null, null, "", ""); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | internal static MessageFault CreateFault(FaultCode code, FaultReason reason, object detail) |
| | | 27 | | { |
| | 4 | 28 | | return CreateFault(code, reason, detail, DataContractSerializerDefaults.CreateSerializer( |
| | 4 | 29 | | (detail == null ? typeof(object) : detail.GetType()), int.MaxValue/*maxItems*/), "", ""); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public static MessageFault CreateFault(FaultCode code, FaultReason reason, object detail, XmlObjectSerializer se |
| | | 33 | | { |
| | 334 | 34 | | if (code == null) |
| | | 35 | | { |
| | 0 | 36 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(code)); |
| | | 37 | | } |
| | | 38 | | |
| | 334 | 39 | | if (reason == null) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reason)); |
| | | 42 | | } |
| | | 43 | | |
| | 334 | 44 | | if (actor == null) |
| | | 45 | | { |
| | 0 | 46 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(actor)); |
| | | 47 | | } |
| | | 48 | | |
| | 334 | 49 | | if (node == null) |
| | | 50 | | { |
| | 0 | 51 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(node)); |
| | | 52 | | } |
| | | 53 | | |
| | 334 | 54 | | return new XmlObjectSerializerFault(code, reason, detail, serializer, actor, node); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public static MessageFault CreateFault(Message message, int maxBufferSize) |
| | | 58 | | { |
| | 2 | 59 | | if (message == null) |
| | | 60 | | { |
| | 0 | 61 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 62 | | } |
| | | 63 | | |
| | 2 | 64 | | XmlDictionaryReader reader = message.GetReaderAtBodyContents(); |
| | 2 | 65 | | using (reader) |
| | | 66 | | { |
| | | 67 | | try |
| | | 68 | | { |
| | 2 | 69 | | EnvelopeVersion envelopeVersion = message.Version.Envelope; |
| | | 70 | | MessageFault fault; |
| | 2 | 71 | | if (envelopeVersion == EnvelopeVersion.Soap12) |
| | | 72 | | { |
| | 2 | 73 | | fault = ReceivedFault.CreateFault12(reader, maxBufferSize); |
| | | 74 | | } |
| | 0 | 75 | | else if (envelopeVersion == EnvelopeVersion.Soap11) |
| | | 76 | | { |
| | 0 | 77 | | fault = ReceivedFault.CreateFault11(reader, maxBufferSize); |
| | | 78 | | } |
| | 0 | 79 | | else if (envelopeVersion == EnvelopeVersion.None) |
| | | 80 | | { |
| | 0 | 81 | | fault = ReceivedFault.CreateFaultNone(reader, maxBufferSize); |
| | | 82 | | } |
| | | 83 | | else |
| | | 84 | | { |
| | 0 | 85 | | throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.EnvelopeVersionUn |
| | | 86 | | } |
| | 2 | 87 | | message.ReadFromBodyContentsToEnd(reader); |
| | 2 | 88 | | return fault; |
| | | 89 | | } |
| | 0 | 90 | | catch (InvalidOperationException e) |
| | | 91 | | { |
| | 0 | 92 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 93 | | SR.SFxErrorDeserializingFault, e)); |
| | | 94 | | } |
| | 0 | 95 | | catch (FormatException e) |
| | | 96 | | { |
| | 0 | 97 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 98 | | SR.SFxErrorDeserializingFault, e)); |
| | | 99 | | } |
| | 0 | 100 | | catch (XmlException e) |
| | | 101 | | { |
| | 0 | 102 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( |
| | 0 | 103 | | SR.SFxErrorDeserializingFault, e)); |
| | | 104 | | } |
| | | 105 | | } |
| | 2 | 106 | | } |
| | | 107 | | |
| | | 108 | | public virtual string Actor |
| | | 109 | | { |
| | | 110 | | get |
| | | 111 | | { |
| | 0 | 112 | | return ""; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | public abstract FaultCode Code { get; } |
| | | 117 | | public virtual string Node |
| | | 118 | | { |
| | | 119 | | get |
| | | 120 | | { |
| | 0 | 121 | | return ""; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public abstract bool HasDetail { get; } |
| | | 126 | | |
| | | 127 | | public abstract FaultReason Reason { get; } |
| | | 128 | | |
| | | 129 | | public T GetDetail<T>() |
| | | 130 | | { |
| | 4 | 131 | | return GetDetail<T>(DataContractSerializerDefaults.CreateSerializer(typeof(T), int.MaxValue/*maxItems*/)); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public T GetDetail<T>(XmlObjectSerializer serializer) |
| | | 135 | | { |
| | 4 | 136 | | if (serializer == null) |
| | | 137 | | { |
| | 0 | 138 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 139 | | } |
| | | 140 | | |
| | 4 | 141 | | XmlDictionaryReader reader = GetReaderAtDetailContents(); |
| | 4 | 142 | | T value = (T)serializer.ReadObject(reader); |
| | 4 | 143 | | if (!reader.EOF) |
| | | 144 | | { |
| | 4 | 145 | | reader.MoveToContent(); |
| | 4 | 146 | | if (reader.NodeType != XmlNodeType.EndElement && !reader.EOF) |
| | | 147 | | { |
| | 0 | 148 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.ExtraContentIsPrese |
| | | 149 | | } |
| | | 150 | | } |
| | 4 | 151 | | return value; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public XmlDictionaryReader GetReaderAtDetailContents() |
| | | 155 | | { |
| | 4 | 156 | | if (!HasDetail) |
| | | 157 | | { |
| | 0 | 158 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.FaultDoesNotH |
| | | 159 | | } |
| | | 160 | | |
| | 4 | 161 | | return OnGetReaderAtDetailContents(); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | protected virtual void OnWriteDetail(XmlDictionaryWriter writer, EnvelopeVersion version) |
| | | 165 | | { |
| | 259 | 166 | | OnWriteStartDetail(writer, version); |
| | 259 | 167 | | OnWriteDetailContents(writer); |
| | 259 | 168 | | writer.WriteEndElement(); |
| | 259 | 169 | | } |
| | | 170 | | |
| | | 171 | | protected virtual void OnWriteStartDetail(XmlDictionaryWriter writer, EnvelopeVersion version) |
| | | 172 | | { |
| | 259 | 173 | | if (version == EnvelopeVersion.Soap12) |
| | | 174 | | { |
| | 184 | 175 | | writer.WriteStartElement(XD.Message12Dictionary.FaultDetail, XD.Message12Dictionary.Namespace); |
| | | 176 | | } |
| | 75 | 177 | | else if (version == EnvelopeVersion.Soap11) |
| | | 178 | | { |
| | 75 | 179 | | writer.WriteStartElement(XD.Message11Dictionary.FaultDetail, XD.Message11Dictionary.FaultNamespace); |
| | | 180 | | } |
| | | 181 | | else |
| | | 182 | | { |
| | 0 | 183 | | writer.WriteStartElement(XD.Message12Dictionary.FaultDetail, XD.MessageDictionary.Namespace); |
| | | 184 | | } |
| | 0 | 185 | | } |
| | | 186 | | |
| | | 187 | | protected abstract void OnWriteDetailContents(XmlDictionaryWriter writer); |
| | | 188 | | |
| | | 189 | | protected virtual XmlDictionaryReader OnGetReaderAtDetailContents() |
| | | 190 | | { |
| | 2 | 191 | | XmlBuffer detailBuffer = new XmlBuffer(int.MaxValue); |
| | 2 | 192 | | XmlDictionaryWriter writer = detailBuffer.OpenSection(XmlDictionaryReaderQuotas.Max); |
| | 2 | 193 | | OnWriteDetail(writer, EnvelopeVersion.Soap12); // Wrap in soap 1.2 by default |
| | 2 | 194 | | detailBuffer.CloseSection(); |
| | 2 | 195 | | detailBuffer.Close(); |
| | 2 | 196 | | XmlDictionaryReader reader = detailBuffer.GetReader(0); |
| | 2 | 197 | | reader.Read(); // Skip the detail element |
| | 2 | 198 | | return reader; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | internal void WriteTo(XmlWriter writer, EnvelopeVersion version) |
| | | 202 | | { |
| | 0 | 203 | | WriteTo(XmlDictionaryWriter.CreateDictionaryWriter(writer), version); |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | internal void WriteTo(XmlDictionaryWriter writer, EnvelopeVersion version) |
| | | 207 | | { |
| | 329 | 208 | | if (writer == null) |
| | | 209 | | { |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 211 | | } |
| | | 212 | | |
| | 329 | 213 | | if (version == null) |
| | | 214 | | { |
| | 0 | 215 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(version)); |
| | | 216 | | } |
| | | 217 | | |
| | 329 | 218 | | if (version == EnvelopeVersion.Soap12) |
| | | 219 | | { |
| | 225 | 220 | | WriteTo12(writer); |
| | | 221 | | } |
| | 104 | 222 | | else if (version == EnvelopeVersion.Soap11) |
| | | 223 | | { |
| | 104 | 224 | | WriteTo11(writer); |
| | | 225 | | } |
| | 0 | 226 | | else if (version == EnvelopeVersion.None) |
| | | 227 | | { |
| | 0 | 228 | | WriteToNone(writer); |
| | | 229 | | } |
| | | 230 | | else |
| | | 231 | | { |
| | 0 | 232 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Env |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private void WriteToNone(XmlDictionaryWriter writer) |
| | | 237 | | { |
| | 0 | 238 | | WriteTo12Driver(writer, EnvelopeVersion.None); |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | private void WriteTo12Driver(XmlDictionaryWriter writer, EnvelopeVersion version) |
| | | 242 | | { |
| | 225 | 243 | | writer.WriteStartElement(XD.MessageDictionary.Fault, version.DictionaryNamespace); |
| | 225 | 244 | | writer.WriteStartElement(XD.Message12Dictionary.FaultCode, version.DictionaryNamespace); |
| | 225 | 245 | | WriteFaultCode12Driver(writer, Code, version); |
| | 225 | 246 | | writer.WriteEndElement(); |
| | 225 | 247 | | writer.WriteStartElement(XD.Message12Dictionary.FaultReason, version.DictionaryNamespace); |
| | 225 | 248 | | FaultReason reason = Reason; |
| | 1080 | 249 | | for (int i = 0; i < reason.Translations.Count; i++) |
| | | 250 | | { |
| | 315 | 251 | | FaultReasonText text = reason.Translations[i]; |
| | 315 | 252 | | writer.WriteStartElement(XD.Message12Dictionary.FaultText, version.DictionaryNamespace); |
| | 315 | 253 | | writer.WriteAttributeString("xml", "lang", XmlUtil.XmlNs, text.XmlLang); |
| | 315 | 254 | | writer.WriteString(text.Text); |
| | 315 | 255 | | writer.WriteEndElement(); |
| | | 256 | | } |
| | 225 | 257 | | writer.WriteEndElement(); |
| | 225 | 258 | | if (Node.Length > 0) |
| | | 259 | | { |
| | 0 | 260 | | writer.WriteElementString(XD.Message12Dictionary.FaultNode, version.DictionaryNamespace, Node); |
| | | 261 | | } |
| | | 262 | | |
| | 225 | 263 | | if (Actor.Length > 0) |
| | | 264 | | { |
| | 0 | 265 | | writer.WriteElementString(XD.Message12Dictionary.FaultRole, version.DictionaryNamespace, Actor); |
| | | 266 | | } |
| | | 267 | | |
| | 225 | 268 | | if (HasDetail) |
| | | 269 | | { |
| | 182 | 270 | | OnWriteDetail(writer, version); |
| | | 271 | | } |
| | 225 | 272 | | writer.WriteEndElement(); |
| | 225 | 273 | | } |
| | | 274 | | |
| | | 275 | | private void WriteFaultCode12Driver(XmlDictionaryWriter writer, FaultCode faultCode, EnvelopeVersion version) |
| | | 276 | | { |
| | 269 | 277 | | writer.WriteStartElement(XD.Message12Dictionary.FaultValue, version.DictionaryNamespace); |
| | | 278 | | string name; |
| | 269 | 279 | | if (faultCode.IsSenderFault) |
| | | 280 | | { |
| | 42 | 281 | | name = version.SenderFaultName; |
| | | 282 | | } |
| | 227 | 283 | | else if (faultCode.IsReceiverFault) |
| | | 284 | | { |
| | 4 | 285 | | name = version.ReceiverFaultName; |
| | | 286 | | } |
| | | 287 | | else |
| | | 288 | | { |
| | 223 | 289 | | name = faultCode.Name; |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | string ns; |
| | 269 | 293 | | if (faultCode.IsPredefinedFault) |
| | | 294 | | { |
| | 225 | 295 | | ns = version.Namespace; |
| | | 296 | | } |
| | | 297 | | else |
| | | 298 | | { |
| | 44 | 299 | | ns = faultCode.Namespace; |
| | | 300 | | } |
| | | 301 | | |
| | 269 | 302 | | string prefix = writer.LookupPrefix(ns); |
| | 269 | 303 | | if (prefix == null) |
| | | 304 | | { |
| | 42 | 305 | | writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns); |
| | | 306 | | } |
| | | 307 | | |
| | 269 | 308 | | writer.WriteQualifiedName(name, ns); |
| | 269 | 309 | | writer.WriteEndElement(); |
| | | 310 | | |
| | 269 | 311 | | if (faultCode.SubCode != null) |
| | | 312 | | { |
| | 44 | 313 | | writer.WriteStartElement(XD.Message12Dictionary.FaultSubcode, version.DictionaryNamespace); |
| | 44 | 314 | | WriteFaultCode12Driver(writer, faultCode.SubCode, version); |
| | 44 | 315 | | writer.WriteEndElement(); |
| | | 316 | | } |
| | 269 | 317 | | } |
| | | 318 | | |
| | | 319 | | private void WriteTo12(XmlDictionaryWriter writer) |
| | | 320 | | { |
| | 225 | 321 | | WriteTo12Driver(writer, EnvelopeVersion.Soap12); |
| | 225 | 322 | | } |
| | | 323 | | |
| | | 324 | | private void WriteTo11(XmlDictionaryWriter writer) |
| | | 325 | | { |
| | 104 | 326 | | writer.WriteStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace); |
| | 104 | 327 | | writer.WriteStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace); |
| | | 328 | | |
| | 104 | 329 | | FaultCode faultCode = Code; |
| | 104 | 330 | | if (faultCode.SubCode != null) |
| | | 331 | | { |
| | 32 | 332 | | faultCode = faultCode.SubCode; |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | string name; |
| | 104 | 336 | | if (faultCode.IsSenderFault) |
| | | 337 | | { |
| | 72 | 338 | | name = "Client"; |
| | | 339 | | } |
| | 32 | 340 | | else if (faultCode.IsReceiverFault) |
| | | 341 | | { |
| | 0 | 342 | | name = "Server"; |
| | | 343 | | } |
| | | 344 | | else |
| | | 345 | | { |
| | 32 | 346 | | name = faultCode.Name; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | string ns; |
| | 104 | 350 | | if (faultCode.IsPredefinedFault) |
| | | 351 | | { |
| | 72 | 352 | | ns = Message11Strings.Namespace; |
| | | 353 | | } |
| | | 354 | | else |
| | | 355 | | { |
| | 32 | 356 | | ns = faultCode.Namespace; |
| | | 357 | | } |
| | | 358 | | |
| | 104 | 359 | | string prefix = writer.LookupPrefix(ns); |
| | 104 | 360 | | if (prefix == null) |
| | | 361 | | { |
| | 32 | 362 | | writer.WriteAttributeString("xmlns", "a", XmlUtil.XmlNsNs, ns); |
| | | 363 | | } |
| | | 364 | | |
| | 104 | 365 | | writer.WriteQualifiedName(name, ns); |
| | 104 | 366 | | writer.WriteEndElement(); |
| | 104 | 367 | | FaultReasonText translation = Reason.Translations[0]; |
| | 104 | 368 | | writer.WriteStartElement(XD.Message11Dictionary.FaultString, XD.Message11Dictionary.FaultNamespace); |
| | 104 | 369 | | if (translation.XmlLang.Length > 0) |
| | | 370 | | { |
| | 0 | 371 | | writer.WriteAttributeString("xml", "lang", XmlUtil.XmlNs, translation.XmlLang); |
| | | 372 | | } |
| | | 373 | | |
| | 104 | 374 | | writer.WriteString(translation.Text); |
| | 104 | 375 | | writer.WriteEndElement(); |
| | 104 | 376 | | if (Actor.Length > 0) |
| | | 377 | | { |
| | 0 | 378 | | writer.WriteElementString(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace, Acto |
| | | 379 | | } |
| | | 380 | | |
| | 104 | 381 | | if (HasDetail) |
| | | 382 | | { |
| | 75 | 383 | | OnWriteDetail(writer, EnvelopeVersion.Soap11); |
| | | 384 | | } |
| | 104 | 385 | | writer.WriteEndElement(); |
| | 104 | 386 | | } |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | internal class XmlObjectSerializerFault : MessageFault |
| | | 390 | | { |
| | | 391 | | private readonly FaultCode _code; |
| | | 392 | | private readonly FaultReason _reason; |
| | | 393 | | private readonly string _actor; |
| | | 394 | | private readonly string _node; |
| | | 395 | | private readonly object _detail; |
| | | 396 | | private readonly XmlObjectSerializer _serializer; |
| | | 397 | | |
| | | 398 | | public XmlObjectSerializerFault(FaultCode code, FaultReason reason, object detail, XmlObjectSerializer serialize |
| | | 399 | | { |
| | | 400 | | _code = code; |
| | | 401 | | _reason = reason; |
| | | 402 | | _detail = detail; |
| | | 403 | | _serializer = serializer; |
| | | 404 | | _actor = actor; |
| | | 405 | | _node = node; |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | public override string Actor |
| | | 409 | | { |
| | | 410 | | get |
| | | 411 | | { |
| | | 412 | | return _actor; |
| | | 413 | | } |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | public override FaultCode Code |
| | | 417 | | { |
| | | 418 | | get |
| | | 419 | | { |
| | | 420 | | return _code; |
| | | 421 | | } |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | public override bool HasDetail |
| | | 425 | | { |
| | | 426 | | get |
| | | 427 | | { |
| | | 428 | | return _serializer != null; |
| | | 429 | | } |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | public override string Node |
| | | 433 | | { |
| | | 434 | | get |
| | | 435 | | { |
| | | 436 | | return _node; |
| | | 437 | | } |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | public override FaultReason Reason |
| | | 441 | | { |
| | | 442 | | get |
| | | 443 | | { |
| | | 444 | | return _reason; |
| | | 445 | | } |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | private object ThisLock |
| | | 449 | | { |
| | | 450 | | get |
| | | 451 | | { |
| | | 452 | | return _code; |
| | | 453 | | } |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | protected override void OnWriteDetailContents(XmlDictionaryWriter writer) |
| | | 457 | | { |
| | | 458 | | if (_serializer != null) |
| | | 459 | | { |
| | | 460 | | lock (ThisLock) |
| | | 461 | | { |
| | | 462 | | _serializer.WriteObject(writer, _detail); |
| | | 463 | | } |
| | | 464 | | } |
| | | 465 | | } |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | internal class ReceivedFault : MessageFault |
| | | 469 | | { |
| | | 470 | | private readonly FaultCode _code; |
| | | 471 | | private readonly FaultReason _reason; |
| | | 472 | | private readonly string _actor; |
| | | 473 | | private readonly string _node; |
| | | 474 | | private readonly XmlBuffer _detail; |
| | | 475 | | private readonly bool _hasDetail; |
| | | 476 | | private readonly EnvelopeVersion _receivedVersion; |
| | | 477 | | |
| | | 478 | | private ReceivedFault(FaultCode code, FaultReason reason, string actor, string node, XmlBuffer detail, EnvelopeV |
| | | 479 | | { |
| | | 480 | | _code = code; |
| | | 481 | | _reason = reason; |
| | | 482 | | _actor = actor; |
| | | 483 | | _node = node; |
| | | 484 | | _receivedVersion = version; |
| | | 485 | | _hasDetail = InferHasDetail(detail); |
| | | 486 | | _detail = _hasDetail ? detail : null; |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | public override string Actor |
| | | 490 | | { |
| | | 491 | | get |
| | | 492 | | { |
| | | 493 | | return _actor; |
| | | 494 | | } |
| | | 495 | | } |
| | | 496 | | |
| | | 497 | | public override FaultCode Code |
| | | 498 | | { |
| | | 499 | | get |
| | | 500 | | { |
| | | 501 | | return _code; |
| | | 502 | | } |
| | | 503 | | } |
| | | 504 | | |
| | | 505 | | public override bool HasDetail |
| | | 506 | | { |
| | | 507 | | get |
| | | 508 | | { |
| | | 509 | | return _hasDetail; |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | |
| | | 513 | | public override string Node |
| | | 514 | | { |
| | | 515 | | get |
| | | 516 | | { |
| | | 517 | | return _node; |
| | | 518 | | } |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | public override FaultReason Reason |
| | | 522 | | { |
| | | 523 | | get |
| | | 524 | | { |
| | | 525 | | return _reason; |
| | | 526 | | } |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | private bool InferHasDetail(XmlBuffer detail) |
| | | 530 | | { |
| | | 531 | | bool hasDetail = false; |
| | | 532 | | if (detail != null) |
| | | 533 | | { |
| | | 534 | | XmlDictionaryReader reader = detail.GetReader(0); |
| | | 535 | | if (!reader.IsEmptyElement && reader.Read()) // check if the detail element contains data |
| | | 536 | | { |
| | | 537 | | hasDetail = (reader.MoveToContent() != XmlNodeType.EndElement); |
| | | 538 | | } |
| | | 539 | | |
| | | 540 | | reader.Dispose(); |
| | | 541 | | } |
| | | 542 | | return hasDetail; |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | protected override void OnWriteDetail(XmlDictionaryWriter writer, EnvelopeVersion version) |
| | | 546 | | { |
| | | 547 | | using (XmlReader r = _detail.GetReader(0)) |
| | | 548 | | { |
| | | 549 | | // Start the element |
| | | 550 | | base.OnWriteStartDetail(writer, version); |
| | | 551 | | |
| | | 552 | | // Copy the attributes |
| | | 553 | | while (r.MoveToNextAttribute()) |
| | | 554 | | { |
| | | 555 | | if (ShouldWriteDetailAttribute(version, r.Prefix, r.LocalName, r.Value)) |
| | | 556 | | { |
| | | 557 | | writer.WriteAttributeString(r.Prefix, r.LocalName, r.NamespaceURI, r.Value); |
| | | 558 | | } |
| | | 559 | | } |
| | | 560 | | r.MoveToElement(); |
| | | 561 | | |
| | | 562 | | r.Read(); |
| | | 563 | | |
| | | 564 | | // Copy the contents |
| | | 565 | | while (r.NodeType != XmlNodeType.EndElement) |
| | | 566 | | { |
| | | 567 | | writer.WriteNode(r, false); |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | // End the element |
| | | 571 | | writer.WriteEndElement(); |
| | | 572 | | } |
| | | 573 | | } |
| | | 574 | | |
| | | 575 | | protected override void OnWriteStartDetail(XmlDictionaryWriter writer, EnvelopeVersion version) |
| | | 576 | | { |
| | | 577 | | using (XmlReader r = _detail.GetReader(0)) |
| | | 578 | | { |
| | | 579 | | // Start the element |
| | | 580 | | base.OnWriteStartDetail(writer, version); |
| | | 581 | | |
| | | 582 | | // Copy the attributes |
| | | 583 | | while (r.MoveToNextAttribute()) |
| | | 584 | | { |
| | | 585 | | if (ShouldWriteDetailAttribute(version, r.Prefix, r.LocalName, r.Value)) |
| | | 586 | | { |
| | | 587 | | writer.WriteAttributeString(r.Prefix, r.LocalName, r.NamespaceURI, r.Value); |
| | | 588 | | } |
| | | 589 | | } |
| | | 590 | | } |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | protected override void OnWriteDetailContents(XmlDictionaryWriter writer) |
| | | 594 | | { |
| | | 595 | | using (XmlReader r = _detail.GetReader(0)) |
| | | 596 | | { |
| | | 597 | | r.Read(); |
| | | 598 | | while (r.NodeType != XmlNodeType.EndElement) |
| | | 599 | | { |
| | | 600 | | writer.WriteNode(r, false); |
| | | 601 | | } |
| | | 602 | | } |
| | | 603 | | } |
| | | 604 | | |
| | | 605 | | protected override XmlDictionaryReader OnGetReaderAtDetailContents() |
| | | 606 | | { |
| | | 607 | | XmlDictionaryReader reader = _detail.GetReader(0); |
| | | 608 | | reader.Read(); // Skip the detail element |
| | | 609 | | return reader; |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | private bool ShouldWriteDetailAttribute(EnvelopeVersion targetVersion, string prefix, string localName, string a |
| | | 613 | | { |
| | | 614 | | // Handle fault detail version conversion from Soap12 to Soap11 -- scope tightly to only conversion from Soa |
| | | 615 | | // SOAP 1.1 specifications allow an arbitrary element within <fault>, hence: |
| | | 616 | | // transform this IFF the SOAP namespace specified will affect the namespace of the <detail> element, |
| | | 617 | | // AND the namespace specified is exactly the Soap12 Namespace. |
| | | 618 | | bool shouldSkip = _receivedVersion == EnvelopeVersion.Soap12 // original incoming version |
| | | 619 | | && targetVersion == EnvelopeVersion.Soap11 // version to serialize to |
| | | 620 | | && string.IsNullOrEmpty(prefix) // attribute prefix |
| | | 621 | | && localName == "xmlns" // only transform namespace attributes, |
| | | 622 | | && attributeValue == XD.Message12Dictionary.Namespace.Value; |
| | | 623 | | |
| | | 624 | | return !shouldSkip; |
| | | 625 | | } |
| | | 626 | | |
| | | 627 | | public static ReceivedFault CreateFaultNone(XmlDictionaryReader reader, int maxBufferSize) |
| | | 628 | | { |
| | | 629 | | return CreateFault12Driver(reader, maxBufferSize, EnvelopeVersion.None); |
| | | 630 | | } |
| | | 631 | | |
| | | 632 | | private static ReceivedFault CreateFault12Driver(XmlDictionaryReader reader, int maxBufferSize, EnvelopeVersion |
| | | 633 | | { |
| | | 634 | | reader.ReadStartElement(XD.MessageDictionary.Fault, version.DictionaryNamespace); |
| | | 635 | | reader.ReadStartElement(XD.Message12Dictionary.FaultCode, version.DictionaryNamespace); |
| | | 636 | | FaultCode code = ReadFaultCode12Driver(reader, version); |
| | | 637 | | reader.ReadEndElement(); |
| | | 638 | | List<FaultReasonText> translations = new List<FaultReasonText>(); |
| | | 639 | | if (reader.IsEmptyElement) |
| | | 640 | | { |
| | | 641 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.AtLeastOneFaultReasonMu |
| | | 642 | | } |
| | | 643 | | else |
| | | 644 | | { |
| | | 645 | | reader.ReadStartElement(XD.Message12Dictionary.FaultReason, version.DictionaryNamespace); |
| | | 646 | | while (reader.IsStartElement(XD.Message12Dictionary.FaultText, version.DictionaryNamespace)) |
| | | 647 | | { |
| | | 648 | | translations.Add(ReadTranslation12(reader)); |
| | | 649 | | } |
| | | 650 | | |
| | | 651 | | reader.ReadEndElement(); |
| | | 652 | | } |
| | | 653 | | |
| | | 654 | | string actor = ""; |
| | | 655 | | string node = ""; |
| | | 656 | | if (reader.IsStartElement(XD.Message12Dictionary.FaultNode, version.DictionaryNamespace)) |
| | | 657 | | { |
| | | 658 | | node = reader.ReadElementContentAsString(); |
| | | 659 | | } |
| | | 660 | | |
| | | 661 | | if (reader.IsStartElement(XD.Message12Dictionary.FaultRole, version.DictionaryNamespace)) |
| | | 662 | | { |
| | | 663 | | actor = reader.ReadElementContentAsString(); |
| | | 664 | | } |
| | | 665 | | |
| | | 666 | | XmlBuffer detail = null; |
| | | 667 | | if (reader.IsStartElement(XD.Message12Dictionary.FaultDetail, version.DictionaryNamespace)) |
| | | 668 | | { |
| | | 669 | | detail = new XmlBuffer(maxBufferSize); |
| | | 670 | | XmlDictionaryWriter writer = detail.OpenSection(reader.Quotas); |
| | | 671 | | writer.WriteNode(reader, false); |
| | | 672 | | detail.CloseSection(); |
| | | 673 | | detail.Close(); |
| | | 674 | | } |
| | | 675 | | reader.ReadEndElement(); |
| | | 676 | | FaultReason reason = new FaultReason(translations); |
| | | 677 | | return new ReceivedFault(code, reason, actor, node, detail, version); |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | private static FaultCode ReadFaultCode12Driver(XmlDictionaryReader reader, EnvelopeVersion version) |
| | | 681 | | { |
| | | 682 | | reader.ReadStartElement(XD.Message12Dictionary.FaultValue, version.DictionaryNamespace); |
| | | 683 | | XmlUtil.ReadContentAsQName(reader, out string localName, out string ns); |
| | | 684 | | reader.ReadEndElement(); |
| | | 685 | | if (reader.IsStartElement(XD.Message12Dictionary.FaultSubcode, version.DictionaryNamespace)) |
| | | 686 | | { |
| | | 687 | | reader.ReadStartElement(); |
| | | 688 | | FaultCode subCode = ReadFaultCode12Driver(reader, version); |
| | | 689 | | reader.ReadEndElement(); |
| | | 690 | | return new FaultCode(localName, ns, subCode); |
| | | 691 | | } |
| | | 692 | | return new FaultCode(localName, ns); |
| | | 693 | | } |
| | | 694 | | |
| | | 695 | | public static ReceivedFault CreateFault12(XmlDictionaryReader reader, int maxBufferSize) |
| | | 696 | | { |
| | | 697 | | return CreateFault12Driver(reader, maxBufferSize, EnvelopeVersion.Soap12); |
| | | 698 | | } |
| | | 699 | | |
| | | 700 | | private static FaultReasonText ReadTranslation12(XmlDictionaryReader reader) |
| | | 701 | | { |
| | | 702 | | string xmlLang = XmlUtil.GetXmlLangAttribute(reader); |
| | | 703 | | string text = reader.ReadElementContentAsString(); |
| | | 704 | | return new FaultReasonText(text, xmlLang); |
| | | 705 | | } |
| | | 706 | | |
| | | 707 | | public static ReceivedFault CreateFault11(XmlDictionaryReader reader, int maxBufferSize) |
| | | 708 | | { |
| | | 709 | | reader.ReadStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace); |
| | | 710 | | reader.ReadStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace); |
| | | 711 | | XmlUtil.ReadContentAsQName(reader, out string name, out string ns); |
| | | 712 | | FaultCode code = new FaultCode(name, ns); |
| | | 713 | | reader.ReadEndElement(); |
| | | 714 | | |
| | | 715 | | string xmlLang = reader.XmlLang; |
| | | 716 | | reader.MoveToContent(); // Don't do IsStartElement. FaultString is required, so let the reader throw. |
| | | 717 | | string text = reader.ReadElementContentAsString(XD.Message11Dictionary.FaultString.Value, XD.Message11Dictio |
| | | 718 | | FaultReasonText translation = new FaultReasonText(text, xmlLang); |
| | | 719 | | |
| | | 720 | | string actor = ""; |
| | | 721 | | if (reader.IsStartElement(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace)) |
| | | 722 | | { |
| | | 723 | | actor = reader.ReadElementContentAsString(); |
| | | 724 | | } |
| | | 725 | | |
| | | 726 | | XmlBuffer detail = null; |
| | | 727 | | if (reader.IsStartElement(XD.Message11Dictionary.FaultDetail, XD.Message11Dictionary.FaultNamespace)) |
| | | 728 | | { |
| | | 729 | | detail = new XmlBuffer(maxBufferSize); |
| | | 730 | | XmlDictionaryWriter writer = detail.OpenSection(reader.Quotas); |
| | | 731 | | writer.WriteNode(reader, false); |
| | | 732 | | detail.CloseSection(); |
| | | 733 | | detail.Close(); |
| | | 734 | | } |
| | | 735 | | reader.ReadEndElement(); |
| | | 736 | | FaultReason reason = new FaultReason(translation); |
| | | 737 | | return new ReceivedFault(code, reason, actor, actor, detail, EnvelopeVersion.Soap11); |
| | | 738 | | } |
| | | 739 | | } |
| | | 740 | | } |