强制ASMX代理使用XmlSerializer的,而不是DataContractSerializer的 [英] Forcing ASMX proxy to use XmlSerializer instead of DataContractSerializer

查看:189
本文介绍了强制ASMX代理使用XmlSerializer的,而不是DataContractSerializer的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们给予,我们要消耗我们的项目的外部SOAP服务。所有这些提供WSDL的数据,但他们中的很多都没有.NET服务(其中大多数是用Java编写的)。我们已经产生了一些客户端代理与Wsdl.exe用的工具。这个工具做什么是应该做的,它创建代理我们的消费。

We were given external SOAP services that we have to consume in our project. All of these provide WSDL data, but a lot of them are not .NET services (most of them were written in Java). We have generated a number of client proxies with wsdl.exe tool. This tool does what it's supposed to do, it creates proxies for us to consume.

的问题,一旦我们尝试调用这些服务的使用所产生的代理方式出现。我们截取的日志记录所有的SOAP请求和XML数据的外观从WSDL模式中指定的不同。

The problem appears once we try to call methods on these services using generated proxies. We intercept all SOAP requests for logging purposes and XML data looks different from the one specified in WSDL schema.

例如,如果一个字段被称为名,我们的代理将其序列化为为nameField。我想这是因为所谓的名的属性使用名为为nameField一个支持字段。在另一边服务显然不能跨preT这种命名约定的。

For instance, if a field is called "Name", our proxies will serialize it as "nameField". I guess this is because the property called "Name" uses a backing field called "nameField". Services on the other side obviously can't interpret this kind of naming convention.

这不会发生,如果我们的ASMX代理使用的旧XmlSerializer的,但出于某种原因,他们选择的DataContractSerializer,这完全打乱了客户端和服务之间的系列化和休息的兼容性。

This wouldn't be happening if our ASMX proxies used the old XmlSerializer, but for some reason they opt for DataContractSerializer, which completely messes up serialization and breaks compatibility between clients and services.

我的同事们都使出手动构造XML数据,然后与HttpWebRequest类发送。我认为这是在2011年完全不能接受的,这是自动代理生成是。

My colleagues have resorted to manually constructing XML data and then sending it with HttpWebRequest class. I think this is completely unacceptable in 2011, this is what automated proxy generation is for.

我的问题是:为什么会这样?为什么用我们代理的DataContractSerializer,从而忽略了过程中的所有XML序列化的属性?有没有办法来迫使他们使用XmlSerializer的一次?

My question is: why is this happening? Why are our proxies using DataContractSerializer and thus ignoring all xml serialization attributes in the process? Is there a way to force them to use XmlSerializer once again?

我们使用.NET 4.0。

We use .NET 4.0.

推荐答案

如果您正在使用WCF默认为的DataContractSerializer 。而且,如果类型没有明确的 [DataContract] / [数据成员] 标记,然后 DataContractSerializer的将使用字段,这听起来像正在发生的事情。

If you are using WCF the default is DataContractSerializer. And if the types do not have explicit [DataContract]/[DataMember] markers, then DataContractSerializer will use the fields, which sounds like what is happening.

要使用的XmlSerializer ,而不是添加 [XmlSerializerFormat] 来您的服务。请参见 MSDN

To use XmlSerializer instead, add [XmlSerializerFormat] to your service. See MSDN.

您也可以尝试加入 [XmlType将] [XmlRoot] 来你的类(如果它不已经在那里了)。

You could also try adding [XmlType] or [XmlRoot] to your classes (if it isn't already there).

这篇关于强制ASMX代理使用XmlSerializer的,而不是DataContractSerializer的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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