.NET WCF序列化问题序列化、问题、NET、WCF

2023-09-04 02:29:04 作者:失心失人

情况:系统中的应该是公开的系统B调用SOAP 1.2的Web服务。为了让知道的信息是这样的,B发送一个XSD到一个描述消息中的内容。生成的使用.NET xsd.exe工具的存根,创造了解决这个问题的简单的互联网服务和作业已经完成。

The situation: system A is supposed to expose SOAP 1.2 web service for system B to call. In order for A to know what the message looks like, B sent an XSD to A describing the contents of the message. A generated the stubs using the .NET xsd.exe tool, created the simple webservice around this and the job was done.

不过,当B调用此Web服务,A拒绝序列化SOAP / XML内容生成的代理类实例。我知道这一定是下降到序列化对系统中的web服务端定义的办法,但一直无法找到到底是怎么回事错了。

However; when B calls this webservice, A refuses to serialize the SOAP/XML content to the generated proxy class instances. I realize this must be down to the way the serialization is defined on the webservice side on system A, but have been unable to locate exactly what is going wrong.

什么从B中的信息是这样的(匿名) - 失败:

<ns2:Set_Out xmlns:ns2="http://a.a/1.0" xmlns:ns1="http://b.b/1.0" xmlns:ns0="http://c.c">
<Context xmlns="">
    <Foo>test</Foo>
    <Bar>test</Bar>
    ...
</Context>

什么从测试客户端(基于WSDL)测试消息的样子 - 作品:

<Set_Out xmlns="http://a.a/1.0">
<Context xmlns:b="http://schemas.datacontract.org/2004/07/x.x" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <b:Foo>TEST</b:Foo>
    <b:Bar>test</b:Bar>
    ...
</Context>

在通过内置的Web服务从B接收到的消息,它不会在所有序列化的消息。代理(如上下文)的对象是。当它收到来自测试客户端的消息,一切都正确生成。

When the webservice built by A receives the messages from B, it doesn't serialize the message at all. The objects of the proxy (for instance Context) are null. When it receives the messages from a test client, everything is generated correctly.

序列化的代理属性:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://a.a/1.0")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://a.a/1.0", IsNullable=false)]
[System.ServiceModel.MessageContract]
public partial class Set_Out {
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://a.a/1.0", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]    [System.ServiceModel.MessageBodyMember(Order=0)]
    public ContextType Context;
    ...

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://a.a/1.0")]
public partial class ContextType {
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Foo;

我觉得奇怪的事情:

Things I find odd:

当生成WSDL此服务,则WSDL都将自己的命名空间的WSDL定义(的xmlns:乙在上面的例子,例如) 的WSDL不尊重不合格在代理定义的注释,并让一切合格。 When generating the WSDL for this service, the WSDL adds its own namespaces to the WSDL definition (xmlns:b in the example above, for instance). The WSDL doesn't respect the unqualified annotations defined in the proxy, and makes everything qualified.

我已经试过注解的各种组合,但似乎没有任何帮助;从B的邮件不正确反序列化由A.产生的代理任何想法/指针/帮助将非常AP preciated!

I've tried various combinations of annotations, but nothing seems to help; the messages from B aren't correctly deserialized to the proxies generated by A. Any ideas/pointers/help would be much appreciated!

推荐答案

在的典型案例问这样的问题,解决它自己在5分钟后我已经成功地解决我的问题。

In a typical case of "ask the question, solve it yourself 5 minutes later" I've managed to solve my problem.

的问题是,很显然,使用WCF .NET时使用默认序列化中涉及的数据合同的类。这个默认的序列化(我相信这是的DataContractSerializer )显然并没有真正让太多的配置方式。我不得不告诉WCF使用的XmlSerializer 序列化的消息。要做到这一点,我不得不添加 [XmlSerializerFormat()] 注释的服务合同接口(显然它的意思是对运营工作以及)。例如:

The issue was that, apparently, when using WCF .NET uses a default serializer for the classes involved in the data contract. This default serializer (I believe it's DataContractSerializer) apparently doesn't really allow much in the way of configuration. I had to tell WCF to use XmlSerializer to serialize the message. To do this, I had to add the [XmlSerializerFormat()] annotation to the service contract interface (apparently it's meant to work on operations as well). Example:

[ServiceContract(Namespace = "http://a.a/1.0")]
[XmlSerializerFormat()]
public interface IMyWebService
{
    [OperationContract()]
    void DoStuff(Set_Out message);
}

添加在 [XmlSerializerFormat()] 批注,则WSDL显著(正确的命名空间)改变,消息被正确序列化。

After adding the [XmlSerializerFormat()] annotation, the WSDL changed significantly (proper namespaces) and the messages are serialized correctly.

论坛帖子,让我在正确的轨道:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c7fede29-56c2-4ff3-bd02-48e3e0b2cec1/

Forum post that set me on the right track: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c7fede29-56c2-4ff3-bd02-48e3e0b2cec1/