/// <summary> /// Gets a given <see cref="SoapHeader"/> by its <see cref="XName"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> with the headers.</param> /// <param name="name">The <see cref="XName"/> to search.</param> /// <returns>The <see cref="SoapHeader"/> or null if not match is found</returns> /// <exception cref="ArgumentNullException"></exception> public static IEnumerable <T> HeadersWithName <T>(this SoapEnvelope envelope, XName name) where T : SoapHeader { return (envelope.HeadersWithName(name) .Select(e => SoapClientSettings.Default.SerializationProvider.ToObject <T>(e))); }
/// <summary> /// Appends the received <see cref="SoapHeader"/> collection to the existing /// ones in the received <see cref="SoapEnvelope"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to append the headers</param> /// <param name="headers">The <see cref="SoapHeader"/> collection to append</param> /// <returns>The <see cref="SoapEnvelope"/> after changes</returns> /// <exception cref="ArgumentNullException"></exception> public static SoapEnvelope WithHeaders( this SoapEnvelope envelope, params SoapHeader[] headers) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } if (headers == null) { throw new ArgumentNullException(nameof(headers)); } if (headers.Length == 0) { return(envelope); } var xElementHeaders = new XElement[headers.Length]; for (var i = 0; i < headers.Length; i++) { xElementHeaders[i] = SoapClientSettings.Default.SerializationProvider.ToXElement(headers[i]); } return(envelope.WithHeaders(xElementHeaders)); }
/// <summary> /// Appends the received <see cref="XElement"/> collection to the existing /// ones in the received <see cref="SoapEnvelope"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to append the headers</param> /// <param name="headers">The <see cref="SoapHeader"/> collection to append</param> /// <returns>The <see cref="SoapEnvelope"/> after changes</returns> /// <exception cref="ArgumentNullException"></exception> public static SoapEnvelope WithHeaders( this SoapEnvelope envelope, params XElement[] headers) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } if (headers == null) { throw new ArgumentNullException(nameof(headers)); } if (headers.Length == 0) { return(envelope); } if (envelope.Header == null) { envelope.Header = new SoapEnvelopeHeader { Headers = headers }; } else { var envelopeHeaders = new List <XElement>(envelope.Header.Headers); envelopeHeaders.AddRange(headers); envelope.Header.Headers = envelopeHeaders.ToArray(); } return(envelope); }
/// <summary> /// Does the <see cref="SoapEnvelope.Body"/> contains a fault? /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to validate</param> /// <returns>True if a fault exists</returns> /// <exception cref="ArgumentNullException"></exception> public static bool IsFaulted(this SoapEnvelope envelope) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } return(envelope.Body?.Value != null && envelope.Body.Value.Name == SoapV1Dot1FaultXName); }
/// <summary> /// Extracts the <see cref="SoapEnvelope.Body"/> as a <see cref="SoapFault"/>. /// It will fail to deserialize if the body is not a fault. Consider to /// use <see cref="IsFaulted(SoapEnvelope)"/> first. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to be used</param> /// <returns>The <see cref="SoapFault"/></returns> /// <exception cref="ArgumentNullException"></exception> public static SoapFault Fault(this SoapEnvelope envelope) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } return(envelope.Body == null ? null : SoapClientSettings.Default.SerializationProvider.ToObject <SoapFault>(envelope.Body.Value)); }
/// <summary> /// Gets a collection of <see cref="XElement"/> headers by its <see cref="XName"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> with the headers.</param> /// <param name="name">The <see cref="XName"/> to search.</param> /// <returns>The <see cref="XElement"/> or null if not match is found</returns> /// <exception cref="ArgumentNullException"></exception> public static IEnumerable <XElement> HeadersWithName(this SoapEnvelope envelope, XName name) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } return(envelope.Header == null ? Enumerable.Empty <XElement>() : envelope.Header.Headers.Where(xElement => xElement.Name == name)); }
/// <summary> /// Extracts the <see cref="SoapEnvelope.Body"/> as an object of the given type. /// </summary> /// <typeparam name="T">The type do be deserialized.</typeparam> /// <param name="envelope">The <see cref="SoapEnvelope"/></param> /// <returns>The deserialized object</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="FaultV1Dot1Exception">Thrown if the body contains a fault</exception> public static T Body <T>(this SoapEnvelope envelope) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } envelope.ThrowIfFaulted(); return(SoapClientSettings.Default.SerializationProvider.ToObject <T>(envelope.Body.Value)); }
/// <summary> /// Gets the first <see cref="XElement"/> header by its <see cref="XName"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> with the headers.</param> /// <param name="name">The <see cref="XName"/> to search.</param> /// <returns>The <see cref="XElement"/> or null if not match is found</returns> /// <exception cref="ArgumentNullException"></exception> public static XElement HeaderWithName(this SoapEnvelope envelope, XName name) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } if (envelope.Header == null || envelope.Header.Headers.Length == 0) { return(null); } return(envelope.Header.Headers.FirstOrDefault(xElement => xElement.Name == name)); }
/// <summary> /// Sets the given <see cref="XElement"/> as the envelope body. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to be used.</param> /// <param name="body">The <see cref="XElement"/> to set as the body.</param> /// <returns>The <see cref="SoapEnvelope"/> after changes.</returns> /// <exception cref="ArgumentNullException"></exception> public static SoapEnvelope Body(this SoapEnvelope envelope, XElement body) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } if (envelope.Body == null) { envelope.Body = new SoapEnvelopeBody(); } envelope.Body.Value = body; return(envelope); }
/// <summary> /// Checks if the <see cref="SoapEnvelope.Body"/> contains a fault /// and throws an <see cref="FaultV1Dot1Exception"/> if true. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to validate.</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="FaultV1Dot1Exception">Thrown if the body contains a fault</exception> public static void ThrowIfFaulted(this SoapEnvelope envelope) { if (envelope == null) { throw new ArgumentNullException(nameof(envelope)); } if (!envelope.IsFaulted()) { return; } var fault = envelope.Fault(); throw new FaultV1Dot1Exception { Code = fault.Code, String = fault.String, Actor = fault.Actor, Detail = fault.Detail }; }
/// <summary> /// Gets a given <see cref="SoapHeader"/> by its <see cref="XName"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> with the headers.</param> /// <param name="name">The <see cref="XName"/> to search.</param> /// <returns>The <see cref="SoapHeader"/> or null if not match is found</returns> /// <exception cref="ArgumentNullException"></exception> public static T HeaderWithName <T>(this SoapEnvelope envelope, XName name) where T : SoapHeader { return(SoapClientSettings.Default.SerializationProvider.ToObject <T>(envelope.HeaderWithName(name))); }
/// <summary> /// Appends the received <see cref="SoapHeader"/> collection to the existing /// ones in the received <see cref="SoapEnvelope"/>. /// </summary> /// <param name="envelope">The <see cref="SoapEnvelope"/> to append the headers</param> /// <param name="headers">The <see cref="SoapHeader"/> collection to append</param> /// <returns>The <see cref="SoapEnvelope"/> after changes</returns> /// <exception cref="ArgumentNullException"></exception> public static SoapEnvelope WithHeaders( this SoapEnvelope envelope, IEnumerable <SoapHeader> headers) { return(envelope.WithHeaders(headers as SoapHeader[] ?? headers.ToArray())); }
/// <summary> /// Sets the given entity as the envelope body. /// </summary> /// <typeparam name="T">The object type</typeparam> /// <param name="envelope">The <see cref="SoapEnvelope"/> to be used.</param> /// <param name="body">The entity to set as the body.</param> /// <returns>The <see cref="SoapEnvelope"/> after changes.</returns> /// <exception cref="ArgumentNullException"></exception> public static SoapEnvelope Body <T>(this SoapEnvelope envelope, T body) { return(envelope.Body( SoapClientSettings.Default.SerializationProvider.ToXElement(body))); }