Im using SOAP/XML with MessageContracts and have to return a specified format similar to the following:
If validation is successful:
<env:Envelope>
<env:Body>
<Success xmlns="http://tempuri.org"></Success>
</env:Body>
</env:Envelope>
If it is unsuccessful:
<env:Envelope>
<env:Body>
<Failure xmlns="http://tempuri.org"></Failure>
</env:Body>
</env:Envelope>
Take for instance, the following method. It returns a type of MyResponse. So how can I define MyResponse to return a Success or Failure XML Element?
public MyResponse SaveMessage(MyRequest request)
{
return new MyResponse();
}
I can return a success message just fine, like this. It's unwrapped and has an XML element of Success. But I need to also be able to return an XML element of Failure and I can't do that using the same MyResponse type.
[MessageContract(IsWrapped=false)]
public class MyResponse
{
[MessageBodyMember(Namespace = "http://tempuri.org")]
public Success Success { get; set; }
}
I ended up using untyped messages and returned the message as follows (similar for failure):
MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes("<Success xmlns=\"http://tempuri.org\"></Success>"));
XmlDictionaryReader xdr = XmlDictionaryReader.CreateTextReader(stream,new XmlDictionaryReaderQuotas());
return Message.CreateMessage(MessageVersion.Soap11, "xxx/xxx/xxxResponse", xdr);