SimpleSOAPClient
SimpleSOAPClient copied to clipboard
Why Does SoapEnveloper.Header<T> Require The element name?
Why not just use the attributes attached to the class? It seems redundant.
[XmlRoot("MeowHeader", Namespace= "http://meow.com/")]
public class MeowResponseHeader : SimpleSOAPClient.Models.SoapHeader
{
public string Status { get; set; }
public string Message { get; set; }
}
var header = responseEnvelope
.Header<MeowResponseHeader >("{http://meow.com/}MeowHeader");
I kept looking for:
var header = responseEnvelope.Header<MeowResponseHeader >();
but had to write it myself...
(using .net core - was getting weirdness with just GetType())
public static class SoapEnvelopeExtensions
{
public static T Header<T>(this SoapEnvelope envelope) where T : SoapHeader
{
var typeInfo = envelope.GetType().GetTypeInfo();
var attribute = typeInfo.GetCustomAttribute<XmlRootAttribute>();
var name = $"{attribute.Namespace}{attribute.ElementName}";
return envelope.Header<T>(name);
}
}
Great suggestion! You are completely right, never thought about that. I'll include it in the next release.
Note: yes, they made some changes in .NET Core about reflection (https://blogs.msdn.microsoft.com/dotnet/2016/02/10/porting-to-net-core/). I'll have to use different code by framework compilation.