我有一个类文件,该文件是从我们的客户从第三方提供的XML架构文档生成的。我应该能够将这个生成的类用于客户的 SOAP Web 服务,但我遇到了一些问题。
我创建了一个ServiceContract
接口,以便可以使用 WCF ChannelFactory
连接到 Web 服务,如下所示:
[ServiceContract(Namespace = "http://theircompany.co.uk/theirapp/v1")]
[XmlSerializerFormat]
public interface IWebService
{
[OperationContract]
EPSStatus serviceNotifyDataEventSet(
[XmlElement(Namespace = "http://www.thirdparty.org/thirdapp")] DataEventSet dataSet
);
}
EPSStatus
和DataEventSet
都在我生成的类文件中。DataEventSet
的重要部分:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.thirdparty.org/thirdapp")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.thirdparty.org/thirdapp", IsNullable=false)]
public partial class DataEventSet {
//...
}
当我现在尝试调用IWebService.serviceNotifyDataEventSet
时,我得到以下 SOAP 正文(在其服务器上启用了 WCF 跟踪的情况下找到):
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<serviceNotifyDataEventSet xmlns="http://theircompany.co.uk/theirapp/v1">
<dataSet>
<dataEvents xsi:type="q1:DAInt" xmlns="" xmlns:q1="http://www.thirdparty.org/thirdapp">
<id>47245361157</id>
<time>
<tick_time>141728877218</tick_time>
<time>2012-06-28T10:07:57.218+01:00</time>
<time_type>OSACBM_TIME_MIMOSA</time_type>
</time>
<value>42</value>
</dataEvents>
<id xmlns="">0</id>
<site xmlns="">
<category>SITE_SPECIFIC</category>
</site>
<time xmlns="">
<tick_time>141728877218</tick_time>
<time>2012-06-28T10:07:57.218+01:00</time>
<time_type>OSACBM_TIME_MIMOSA</time_type>
</time>
</dataSet>
</serviceNotifyDataEventSet>
</s:Body>
因此,我能够调用 Web 服务,看起来我的数据正在正确序列化,但在服务器端dataSet
显示为空。我还从客户端获得了一个跟踪,该跟踪确实适用于以下正文:
<soap:Body>
<serviceNotifyDataEventSet xmlns="http://theircompany.co.uk/theirapp/v1">
<dataSet xmlns="http://www.thirdparty.org/thirdapp">
<dataEvents xmlns:q1="http://www.thirdparty.org/thirdapp" xsi:type="q1:DAReal" xmlns="">
<id>47245361408</id>
<time>
<tick_time>141730618844</tick_time>
<time>2012-06-28T10:36:58.843+01:00</time>
<time_type>OSACBM_TIME_MIMOSA</time_type>
</time>
<value>12.34</value>
</dataEvents>
<id xmlns="">0</id>
<site xmlns="">
<category>SITE_SPECIFIC</category>
</site>
<time xmlns="">
<tick_time>141730618843</tick_time>
<time>2012-06-28T10:36:58.843+01:00</time>
<time_type>OSACBM_TIME_MIMOSA</time_type>
</time>
</dataSet>
</serviceNotifyDataEventSet>
</soap:Body>
我能看到的唯一区别是根命名空间是在工作数据包上的dataSet
上设置的:<dataSet xmlns="http://www.thirdparty.org/thirdapp">
.在我的数据包上,根本没有指定命名空间。
问题是,我的分析听起来合理吗,如果是这样,有什么方法可以让根 xmlns 在我的dataSet
上正确输出?
我现在设法使用一种相对简单的方法使其工作。幸运的是,xsd
从 XML 架构生成的代码将所有类标记为没有构造函数的分类。我添加了自己的分部类来定义重写命名空间的默认构造函数,如下所示:
public partial class DataEventSet
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces _xmlns;
/// <summary>
/// Constructor for DataEventSet that sets up default namespaces
/// </summary>
public DataEventSet()
{
_xmlns = new XmlSerializerNamespaces();
_xmlns.Add("", "http://www.thirdparty.org/thirdapp");
_xmlns.Add("o", "http://www.thirdparty.org/thirdapp");
}
}
现在序列化如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Body xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<serviceNotifyDataEventSet xmlns="http://theircompany.co.uk/theirapp/v1">
<dataSet xmlns="http://www.thirdparty.org/thirdapp" xmlns:o="http://www.thirdparty.org/thirdapp">
<dataEvents xsi:type="o:DABool" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>47245361157</id>
<value>true</value>
</dataEvents>
<id xmlns="">0</id>
<site xmlns="">
<category>SITE_SPECIFIC</category>
</site>
<time xmlns="">
<tick_time>396106152171</tick_time>
<time>2012-07-20T13:29:12.171Z</time>
<time_type>OSACBM_TIME_MIMOSA</time_type>
</time>
</dataSet>
</serviceNotifyDataEventSet>
</s:Body>
你的分析听起来很合理。在查看您发布的代码时,我质疑DataEventSet
类是否是您应该查看的有关<dataSet>
元素的类。使用 System.Xml.Serialization.XmlRootAttribute
应该允许您为元素定义/应用正确的命名空间。我的猜测是,您需要在不同的类上使用此属性才能正确输出<dataSet>
元素。