Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you configure a get-only property for a Silverlight-enabled WCF service

I'm not certain where the error is resulting (from silverlight, from wcf, something else...) but, I have a WCF service being called from Silverlight. The method returns a class, with a property that does not have a setter. This throws an error. If I add a setter to the property then it does not give an error.

The error is the usual impenetrable and unhelpful Silverlight error message, but...

[Serializable]  
[DataContract]  
public SomeClass {  
    DataMember]  
    public string PropertyA { get; set; }  
    public string PropertyB { get { return "Hi There"; } }  
}  

Throws an error...

But change it to:

[Serializable]  
[DataContract]  
public SomeClass {  
     [DataMember]  
     public string PropertyA { get; set; }  
     public string PropertyB { get { return "Hi There"; } set {} }  
}  

No error.

Includes the usual ISomeService.svc & SomeService.svc class, references updated in Silverlight calling the client async, etc., etc.

What is the correct way to configure the property (some attribute other than "DataMember" to allow a get-only, or a private-set property) to pass it over the wire?


1 Answers

Thanks. The private set seem to be sufficient. I don't like having the set method there when it is not needed but I can throw an error if it is accessed.

[DataMember]  
public PropertyB {  
    get {  
         return "Hi there";  
    }  
    private set {  
         throw new Exception("Empty setter for use by WCF Service.");
    }  
}      

Or whatever.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!