【问题标题】:Data contract serialization for WCF web service requestWCF Web 服务请求的数据协定序列化
【发布时间】:2014-02-27 14:46:51
【问题描述】:

我的 WCF Web 服务请求有一个 DataContract 元素,如下所示:

    [DataContract]
    public sealed class Request
    {
       public int EventID { get; set; }
    }

一旦我的 Web 服务运行,我从 wsdl 生成的请求如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
      <soapenv:Body>
          <tem:EventDismissRequest>
              <tem:request/>
          </tem:EventDismissRequest>
      </soapenv:Body>
   </soapenv:Envelope>

但是,我希望它如下所示,这意味着我的事件 ID 嵌套在没有 tem 前缀的请求元素中。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
      <soapenv:Header/>
      <soapenv:Body>
          <tem:EventDismissRequest>
             <request>
               <EventID>?</EventID>
             <request>
          </tem:EventDismissRequest>
      </soapenv:Body>
</soapenv:Envelope>

我想我必须在我的数据合同中做一些 xml 属性,但是有很多选择,我尝试了很多方法但没有成功。

【问题讨论】:

  • 你需要用[DataMember]属性来装饰EventID。

标签: c# wcf datacontractserializer


【解决方案1】:

我相信您需要的属性是 [Serializable] 如下,以及您希望序列化的每个项目的 [DataMember]。

[DataContract]
[Serializable]
public sealed class Request
{
   [DataMember]
   public int EventID { get; set; }
}

【讨论】:

  • 谢谢 Andrew,您的建议很有效,但我看到 ,我可以省略 tem 并带着 离开吗?
  • 您使用什么版本的 WCF?我没有看到我的
  • 可能与我在 SOAPUi 中看到的信封中的 xmlns:tem="tempuri.org" 有关
  • 在四处寻找之后,我让它与 [ServiceContract(Namespace = "")] 一起工作
猜你喜欢
  • 2010-10-30
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
相关资源
最近更新 更多