我需要为非常特定的SOAP布局编写服务合同(端点不在我们的控制之下)。本质上,我需要生成以下肥皂请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<auth message-id="1">
<login>
<UserName>goLocalTeam</UserName>
<Password>yeay!</Password>
</login>
</auth>
</soapenv:Body>
</soapenv:Envelope>
我创建了以下合同:
[ServiceContract(Namespace="", SessionMode=SessionMode.NotAllowed)]
public interface IPrototype
{
[OperationContract(Action="auth")]
string auth([MessageParameter(Name = "message-id")]int messageId, Login login);
}
[DataContract(Namespace="")]
public class Login
{
[DataMember]
public string userName { get; set; }
[DataMember]
public string Password { get; set; }
}
但是,当我构造一个客户端并调用时,该message-id
参数被呈现为内部的一个节点,<auth>
而不是一个属性。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">auth</Action>
</s:Header>
<s:Body>
<auth>
<message-id>0</message-id>
<login>
<userName>yay</userName>
<Password>yoy</Password>
</login>
</auth>
</s:Body>
</s:Envelope>
知道如何使WCF将简单类型的参数从节点切换到属性吗?
使用XmlSerializerFormat
属性而不是DataContract
。
看一下MSDN,以确切地了解如何使用它,但是基本上,您使用XmlAttribute
和标记成员XmlElement
,因此它看起来像:
[ServiceContract(Namespace="", SessionMode=SessionMode.NotAllowed)]
public interface IPrototype
{
[OperationContract(Action="auth")]
string Auth(AuthRequest authRequest);
}
[MessageContract(IsWrapped = false)] // notice the unwrapping
public class AuthRequest
{
[XmlAttribute]
public int message-id { get; set; }
[XmlElement]
public Login Login { get; set; }
}
[MessageContract]
public class Login
{
[XmlElement]
public string userName { get; set; }
[XmlElement]
public string Password { get; set; }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句