【发布时间】:2010-09-30 18:54:08
【问题描述】:
如何向 WCF 消息序列化中的字段添加 XML 前缀?
我正在从 .NET 连接到 Java Spring Web 服务,并且我使用参数传入的对象正在按您的预期进行序列化:
<MyClass>
<field1>Field 1 Value</field1>
<field2>Field 2 Value</field2>
</MyClass>
但是,网络服务要求类和字段以命名空间为前缀,比如说命名空间blah,所以我想要的是:
<blah:MyClass>
<blah:field1>Field 1 Value</blah:field1>
<blah:field2>Field 2 Value</blah:field2>
</blah:MyClass>
如何在 WCF 中实现这一点?有没有办法调整我班级的 XML 序列化属性?
编辑:此特定实体的 WSDL 如下(已编辑以删除业务特定的字段名称,但其他一切都相同):
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch0="http://www.domain.com/app/schemas/entityone" xmlns:sch1="http://www.domain.com/app/schemas/types" xmlns:sch2="http://www.domain.com/app/schemas/query" xmlns:sch3="http://www.domain.com/app/schemas/entitytwo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.domain.com/app/schemas/entityone" targetNamespace="http://www.domain.com/app/schemas/entityone">
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:types="http://www.domain.com/app/schemas/types" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://www.domain.com/app/schemas/entityone" xmlns:tns="http://www.domain.com/app/schemas/entityone">
<import namespace="http://www.domain.com/app/schemas/types" />
<element name="TheClassName">
<complexType>
<sequence>
<element name="field1" type="string" />
<element name="field2" type="string" />
<element name="field3" type="string" />
<element name="field4" type="string" />
<element name="field5" type="string" />
<element name="field6" type="string" />
<element name="field7" type="string" />
<element name="field8" type="string" />
</sequence>
</complexType>
</element>
<wsdl:binding name="NameOfBindingHere" type="tns:ReturnTypeHere">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="OperationNameHere">
<soap:operation soapAction="" />
<wsdl:output name="ResponseTypeHere">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
【问题讨论】:
-
您能否编辑该服务的 WSDL 以创建一个仅包含失败操作的 WSDL,然后发布编辑后的 WSDL?了解这是 RPC 还是 Document 服务等对我们来说很重要。
-
您需要包含 WSDL 的其余部分。这就是将说明这些在哪个命名空间中声明的部分,等等。
-
@John,添加了我认为您所指的内容-混淆了一些实际名称,但应该代表那里的内容...
-
您仍然遗漏了
message元素和portType。另外,您是如何使用这项服务的? “添加服务参考”?
标签: c# .net wcf serialization xml-namespaces