.NET 基本类型无法由 WCF 序列化
我正在编写一个 WCF 服务,并希望公开一些自定义配置元素(例如 Custom ConfigurationSection 和 ConnectionStringSettings),以便我可以修改服务的配置。
我的自定义配置元素之一继承自 System.Configuration.ConfigurationElementCollection。 当我尝试启动 WCF 服务时,收到以下错误消息...
无法序列化类型“System.Configuration.ConfigurationElementCollection”。 请考虑使用 DataContractAttribute 属性标记它,并使用 DataMemberAttribute 属性标记要序列化的所有成员。
有没有办法实现这种类型的 DataContract? 我的继承类标有 [DataContract] 属性。
I'm writing a WCF service and want to expose some custom configuration elements (e.g. Custom ConfigurationSection and ConnectionStringSettings) so that I can modify the service's configuration.
One of my custom configuration elements inherits from System.Configuration.ConfigurationElementCollection
. When I try to start my WCF service I get the following error message...
Type 'System.Configuration.ConfigurationElementCollection' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
Is there a way to implement the DataContract for this Type? I have my inherited class marked with the [DataContract] attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
今天刚好碰到这个问题。 这很令人困惑,因为问题出现在将项目从一台机器转移到另一台机器上。 本文似乎相关:
http://blogs.msdn.com/youssefm/archive/2009/08/10/serializing-plain-old-clr-objects-poco-types-with-datacontractserializer.aspx
总而言之,在链接失效的情况下,该问题似乎在运行时 3.5 中出现,并在运行时 3.5 SP1 中消失。
Just hit this issue today. It was confusing because the problem came up moving a project from machine to machine. This article seems relevant:
http://blogs.msdn.com/youssefm/archive/2009/08/10/serializing-plain-old-clr-objects-poco-types-with-datacontractserializer.aspx
To summarize in case of link rot, the issue seems to emerge in runtime 3.5 and go away in runtime 3.5 SP1.
好吧,最后我不得不重新构建我的解决方案。 我发现 SerializedConfigurationSection 最有用。 它位于模式和实践 EnterpriseLibrary 中。 因此,我没有尝试通过 WCF 传递自定义配置节,而是手动执行序列化/反序列化,并将配置节作为字符串通过 WCF 传递。
Ok, well in the end I had to re-architect my solution. I found the SerializableConfigurationSection most beneficial. It's in the patterns and practices EnterpriseLibrary. So rather than trying to pass my Custom Configuration Sections through WCF, I perform the seralization/deserialization manually and pass the configuration sections through WCF as a string.