问题
is in WCF DataContract attribute is required?
I have a class that class is not marked with DataContract or property with DataMember.
When i deploy wcf service on 4 server, out 4 service works fine on 3 server and failed on 1 server with message "Type 'Class' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute."
So is there any settings in web.config or machine.config or in IIS where i can mandate to use serializer in code? or
i can specify to use default serializer if no attribute is specified ?
Thanks, krishna
回答1:
No, the DataContractAttribute is not required - WCF will infer serialization rules. The only thing I can think of to check is that the version of .NET is the same on all of your servers.
More info: WCF Data Contract / Serialization
回答2:
As stated by MSDN
[the DataContractAttribute] Specifies that the type defines or implements a data contract and is serializable by a serializer, such as the DataContractSerializer. To make their type serializable, type authors must define a data contract for their type
You must decorate your classes with a DataContract
and DataMember
attributes to be used in a WCF service.
回答3:
Yes, will need the [DataContract]
attribute on all classes that will be send between the WCF service and any clients.
All properties of that class that may contain data that you want to send will need to be marked with the [DataMember]
attribute. This makes it possible to define a class that contains both serializable and non-serializable data - but I wouldn't recommend it.
Please be aware that you can only serialize data members such, e.g. public
fields or properties, but you cannot serialize methods. Therefore my recommendation is to use plain classes that do nothing else but contain the data to be communicated between service and client as data contracts.
来源:https://stackoverflow.com/questions/11259915/datacontract-attribute-is-required-in-wcf