20

I'm trying to return a complex type from a service method in WCF. I'm using C# and .NET 4. This complex type is meant to be invariant (the same way .net strings are). Furthermore, the service only returns it and never receives it as an argument.

If I try to define only getters on properties I get a run time error. I guess this is because no setters causes serialization to fail. Still, I think this type should be invariant.

Example:

[DataContract]
class A 
{
   [DataMember]
   int ReadOnlyProperty {get; private set;}
}

The service fails to load due to a problem with serialization.

Is there a way to make readonly properties on a WCF DataContract? Perhaps by replacing the serializer? If so, how? If not, what would you suggest for this problem?

Thanks,
Asaf

2
  • You might be able to use readonly fields (not properties) and initialize them in the construct of your class. Also..., possible duplicate: stackoverflow.com/q/1873741/945456
    – Jeff B
    Nov 6, 2015 at 22:19
  • This is even more important today with the move to immutable classes and C#-6.0’s support for auto-implemented get-only properties.
    – binki
    Apr 2, 2016 at 3:56

8 Answers 8

16

put [DataMember] on backing field, you won't need a setter.

4
  • But it won't be readonly, and won't express that it should be readonly.
    – Asaf R
    Mar 22, 2010 at 19:35
  • 5
    it will never be readonly. Datacontract when transferred over the wire is just an XML. you can't make piece of XML readonly. You can't make whoever is on the other end of the wire not to change its value. It just does not work like that. Mar 22, 2010 at 19:54
  • You could simply ignore the value if it is passed to the service? By read only do you mean that the service only returns it? Mar 22, 2010 at 20:32
  • @Bryan: yep. The service only returns it and I want to express exactly that.
    – Asaf R
    Mar 23, 2010 at 8:19
11

Make your setter public to satisfy the serializer, but just don't do anything on the setter. Not 'purist' but gets it done

public string MyProperty 
{
    get {
        return this._myProperty
    }
    set {}
}
5

DataMember Field can't be readonly, because wcf dont serialize object as-is and every time befor deserialization starts creates new object instance, using default constructor. Dispatchers use setters to set field values after deserialization.

But all upper text can be a big mistake :)

To make them realy readonly, make the server logic, validating thiss field values.

2
4

You can't make the properties readonly, however you can get close to readonly by making the fields part of your contract instead of the properties:

[DataContract]
public class A 
{
   public class A(){}
   public class A(int readonlyproperty){ _readonlyproperty = readonlyproperty}

   [DataMember(Name = "ReadOnlyProperty")]
   internal int _readonlyproperty;

   public int ReadOnlyProperty {
      get {return _readonlyproperty;}
      private set {_readonlyproperty = value;}
}

next make your internals accesable in wcf:

[assembly: InternalsVisibleTo("System.Runtime.Serialization")]

Some samples on how to use this to your advantage: wcf-and-datacontract-serialization-internals-and-tips

1

Actually, you can make the read-only fields serializable. You need to set the 'SerializeReadOnlyTypes' property of the DataContractSerializerSettings to 'True', when constructing the DataContractSerializer, as below:

var xmlSerializer = new DataContractSerializer(typeof(yourTypeHere),  new DataContractSerializerSettings {SerializeReadOnlyTypes=true});

See the MSDN description and details, here: https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializersettings(v=vs.110).aspx

0

Like others have said, WCF needs getters and setters. That being said, I came here with the same question and the answers made me ask why I needed a read-only property. If you use the MVVM pattern, the class returned by the service is the Model and so should not be exposed directly by the UI. You can easily make the ViewModel read-only. Alternatively you could use a non UI-targetted facade class.

0

WCF needs to able to serialize / deserialize the property, which is not possible with a private property. To have a readonly property (within a complex type):

  • Decorate class as [DataContract(IsReference = true)]
  • Decorate each / the property as [DataMember]
  • Public Get, Internal Set
    [DataContract(IsReference = true)]
    public class Format : ValueObject<Format>
    {
        [DataMember]
        public int Height { get; internal set; }
    }
-2

Have you tried making the setter private?

Something like:

public string MyProperty
{
get;
private set;
}
5
  • Yep. That's exactly my problem.
    – Asaf R
    Mar 22, 2010 at 19:34
  • So this fixed it? Or this is what you were trying? Mar 22, 2010 at 20:30
  • It doesn't work. If I make MyProperty a [DataMember] I get a runtime error caused, to my best understanding, from the serialization. It happens as soon as the service loads - not even during a client operation.
    – Asaf R
    Mar 23, 2010 at 8:20
  • @AsafR: Correct, it seems that you must have a get/set for all fields for the serialization/deserialization to work. I don't think there is a way around this from everything I've read :( Mar 23, 2010 at 12:55
  • 2
    If you want to put your conclusion in your above answer, I'll be happy to make it the accepted answer.
    – Asaf R
    Mar 23, 2010 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.