Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET base type cannot be serialized by WCF

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.

like image 686
vradenburg Avatar asked Oct 30 '08 13:10

vradenburg


People also ask

What is serialization in WCF?

Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data. The DataContractSerializer is designed to serialize data contract types.

Which namespace is used for serialization in WCF?

By default WCF uses the DataContractSerializer class to serialize data types.

What is serialization and Deserialization in WCF?

WCF deserializes WCF messages into . Net objects and serializes . Net objects into WCF messages. WCF provides DataContractSerializer by default with a servicecontract. We can change this default serializer to a custom serializer like XMLSerializer.


2 Answers

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.

like image 125
quillbreaker Avatar answered Oct 20 '22 19:10

quillbreaker


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.

like image 27
vradenburg Avatar answered Oct 20 '22 19:10

vradenburg