Forcing ASMX proxy to use XmlSerializer instead of DataContractSerializer
Asked Answered
P

1

3

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.

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.

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.

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.

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.

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?

We use .NET 4.0.

Pillion answered 23/11, 2011 at 17:40 Comment(2)
So are you using this asmx as a Service Reference or a Web Reference? Because .net treats them very different.Pfeiffer
As a web reference. Must be something about configuration, because when I try to replicate the issue in a clean solution - everything works as it should. Doesn't matter, we're using workarounds right now.Pillion
B
2

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.

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

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

Befall answered 23/11, 2011 at 17:56 Comment(6)
As I said, we are consuming external services and I have no say in how they are implemented. For some reason, wrong serializer is internally used during serialization. I'll look into XmlType and XmlRoot attributes asap. Thanks.Pillion
@Pillion how are you referencing the external service? precisely?Befall
I grab the wsdl and then run wsdl.exe on it and generate the proxy. Can't reference the services inside VS because they're inside a VPN and only one guy in the company has access to that... all he can do is save WSDL to a file and pass it on to me. Something about security.Pillion
@Vex; k - that sounds like it should work; you're just using regular asmx right? this is .... intriguingBefall
Yep, regular asmx. Perhaps someone put something somewhere in the config so that might be creating problems, I dunno. I wish I had more time to investigate this, but we're so far behind schedule that if I spend any more time on this I'll probably get an earful. They just want me to build xml data manually and post it with HttpWebRequest... Sucks.Pillion
@vex it is really odd; you could check what is bound to async in web.config including in the machine-level, this is just... OddBefall

© 2022 - 2024 — McMap. All rights reserved.