强制参数作为服务合同属性 [英] Force parameter as Service contract attribute

查看:23
本文介绍了强制参数作为服务合同属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为一个非常具体的 SOAP 布局(端点不受我们控制)编写服务合同.本质上,我需要生成以下soap请求:

I need to write a service contract for a very specific SOAP layout (endpoint is not under our control). In essence I need to generate the following soap request:

<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 参数被呈现为 内的一个节点,而不是一个属性.>

However, when I construct a client and call, the message-id parameter is rendered as a node inside <auth> instead of being an attribute.

<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 将简单类型的参数从节点切换到属性吗?

Any idea how I can make WCF switch simple-type parameter from node to attribute?

推荐答案

使用 XmlSerializerFormat 属性而不是 DataContract.

Work with the XmlSerializerFormat attribute instead of DataContract.

查看 MSDN 准确理解如何使用它,但基本上你用 XmlAttributeXmlElement 标记成员,所以它看起来像:

Take a look at the MSDN to understand exactly how to use it, but basically you mark members with XmlAttribute and XmlElement, so it can look something like:

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

}

这篇关于强制参数作为服务合同属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆