WCF DataContractSerializer 无法序列化
我有一个直接的服务,例如:
[ServiceContract]
public interface IService
{
[WebGet(UriTemplate = "/", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
List<DataContracts.MyThing> Get();
}
我的数据合同很简单,没有什么不寻常的:
[DataContract]
public class MyThing
{
[DataMember]
public string ID { get; set;}
}
我使用 WebServiceHostFactory 而不是手动绑定。
当我在 IIS 5.1(Windows XP,我的本地开发环境)上运行此程序时,我得到如下返回:
<ArrayOfMyThing>
<MyThing></MyThing>
</ArrayOfMyThing>
但是,当我在生产环境中的 IIS 6.0 上放置完全相同的代码时,我得到如下返回:
<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>
所以我的问题是双重:
- 为什么它不在我的本地开发环境上提供命名空间?
- 为什么它通过将基本路径附加到标签内的服务来创建错误的 XML?
显然,坏的 XML 节点会破坏任何解析器,所以这对我来说绝对没用。奇怪的是,这种情况只发生在特定的服务方法上,所有其他方法都工作正常,并且配置方式相同。
编辑:当我使用 JSON 时,一切看起来都很好,所以我认为这不是 WCF 的问题。一定是序列化器的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个序列化输出(没有任何 XML 命名空间属性)是使用 XmlSerializer 而不是 DataContractSerializer 时获得的输出。我的假设是 XmlSerializer 被选为 IIS 5.1 配置中选择的序列化程序。 IIS 5.1 和 IIS 6.0 包之间的配置是否不同?你们的合同有什么不同吗?您能否在整个代码库和设置中搜索“XmlSerializer”,以确保您不会意外地在某个地方找到它? (例如,您可能意外地在某处插入了 [XmlSerializerFormat] 或 [XmlSerializerOperationBehavior]。)WebServiceHostFactory 上的设置也可能在 IIS 5.1 和 IIS 6.0 实现之间有所不同,从而导致出现问题。
在 IIS 6.0 实现中,您获得的输出与使用 DataContractSerializer 时获得的输出相同。如果您可以接受 IIS 5.1 输出,但不能接受 IIS 6.0 输出,那么您可以做的是显式地使用 [XmlSerializerFormat] 修饰您的操作或服务,以便 XmlSerializer 始终被拾取......
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....