WCF DataContractSerializer fails to serialize
I have a straight forward service like:
[ServiceContract]
public interface IService
{
[WebGet(UriTemplate = "/", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
List<DataContracts.MyThing> Get();
}
My datacontract is straightfoward, nothing unusual there:
[DataContract]
public class MyThing
{
[DataMember]
public string ID { get; set;}
}
I'm using the WebServiceHostFactory instead of manual binding.
When I run this on IIS 5.1 (Windows XP, my local dev environment), I get a return like:
<ArrayOfMyThing>
<MyThing></MyThing>
</ArrayOfMyThing>
However, when I drop the exact same code on IIS 6.0 in a production box, I get a return like:
<ArrayOfMyThing
xmlns="http://schemas.datacontract.org/2004/07/My.NameSpace.DataContracts"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"http://my.website.com/services/>
</ArrayOfMyThing>
So my question is twofold:
- Why is it not serving namespaces on my local development environment?
- Why is it creating bad XML by appending the base path to the service inside the tag?
Obviously the bad XML node breaks any parser, so this is absolutely useless to me. Oddly enough, this is only happening开发者_运维技巧 on that particular service method, all the others work fine, and are configured the same way.
EDIT: When I use JSON, everything looks good, so I don't think this is an issue with WCF. It has to be a serializer issue.
The first serialized output (without any XML namespace attributes) is the output you'd get if you were to use XmlSerializer instead of DataContractSerializer. My hypothesis is that XmlSerializer is getting picked up as the serializer of choice in your IIS 5.1 configuration. Is the config different between your IIS 5.1 and IIS 6.0 packages? Is any of your contracts different? Can you search for "XmlSerializer" in your entire codebase and settings to make sure you're not accidentally picking it up somewhere? (For example, you might have [XmlSerializerFormat] or [XmlSerializerOperationBehavior] plugged in somewhere accidentally.) There might also be setting on WebServiceHostFactory that is different between IIS 5.1 and IIS 6.0 implementations that is leading to the problem.
With your IIS 6.0 implementation, the output you're getting is what you'd get if you were using DataContractSerializer. If you are OK with the IIS 5.1 output but not OK with the IIS 6.0 output, what you could do is decorate your operations or your service with [XmlSerializerFormat] explicitly, so that XmlSerializer always gets picked up....
精彩评论