1

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; }

}
4

1 回答 1

0

我最终使用了无类型的消息并返回了如下消息(类似失败):

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);
于 2012-10-22T16:10:11.173 回答