views:

192

answers:

1

In .NET 3.5, I would like to create a custom attribute (say [NetDataMember]) that would switch the serialization behavior from DataContractSerializer to NetDataContractSerializer.

Basically, for a class A as illustrated below

[DataContract]
class A
{
  [DataMember]
  public int SimpleProperty { get; set; }

  [Transcient]
  public IBar ComplexProperty { get; set; }
}

I would like to obtain a serializer that would behave like DataContractSerializer by default, but that would be overriden with NetDataContractSerializer for properties marked with [NetDataMember].

Any idea how to design a serializer that would achieve such behavior?

+2  A: 

There's no "out-of-the-box" way in WCF to do this - but a lot of really smart people have already tackled that problem.

Check out Aaron Skonnard's blog post on the NetDataContractSerializer in which he present a behavior you can put on your data contracts as an attribute:

[NetDataContractFormat]

on your service interface (for all methods) or on a single method will use the NetDataContractSerializer for that call. You need to define this per operation or service - not on your data contracts.

marc_s
Thanks for the link. Actually, I can't manage to produce the override effect. I am applying the attribute, but the overriding behavior is never called at (de)serialization time. Am I missing some when instantiating the DCS?
Joannes Vermorel
When deserializing, in WCF, no constructor is ever called - that's a normal behavior.
marc_s
But to see whether or not the NetDataContractSerializer is being used, you should look at the messages being sent around - either use the WcfTestClient.exe in your Visual Studio Common7/IDE directory, or use something like Fiddler to view the traffic
marc_s