# .NET and More > WPF, WCF, WF >  [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract

## chris128

This took me sooo long to figure out but now that I have finally found that changing my Read Only properties to normal properties with a Get and Set allows them to be used as DataMembers in a WCF DataContract, I am a little confused as to why this is the case... and if there is a way around this. I guess there is no real harm in them not being readonly but it just seems odd that you dont get any warning in VS that this is invalid when you add the <DataMember> tag to the properties, its just that when you try and query the MEX endpoint of the service it fails saying that it cannot resolve a reference. As soon as you change the properties from ReadOnly it works fine.
Puzzling.

----------


## mendhak

The properties need to be serialized which is why you run into problems if they are readonly - it needs to be get/set so that the values can be assigned when it is being transported across application boundaries.  A good workaround for this is to set the 'set' accessor as private.



```
private string MyProperty
{
get;
private set;
}
```

Now, if you're using VB.NET, I don't know if it has a private setter, but if it doesn't, you can always take the value given and do nothing with it.

----------


## chris128

Ahh I see, that makes sense I guess. thanks

----------


## mendhak

Does VB.NET have a private setter?

----------


## mendhak

Oh, looks like it does.

Public Property TransactionType() As String
    Get
    End Get
    Private Set(ByVal value As String)
    End Set
End Property


Right, end of talking to myself. kthxbai

----------


## chris128

Get used to it, Im not talking to you anymore after your insults in this thread!  :Big Grin:

----------


## chris128

I havent used WCF for a while, just started a new project using it and spent the last 3 hours re-figuring out what I had already learnt at the start of this thread! grr!

----------


## MacTwist

> Oh, looks like it does.
> 
> Public Property TransactionType() As String
>     Get
>     End Get
>     Private Set(ByVal value As String)
>     End Set
> End Property
> 
> ...



Well, the Private Set is possible in VB.NET, but I just tried it, it doesn't help to fill in the property throught WCF... 
It keeps being empty.

So I am still looking for an other solution.

----------


## MacTwist

Well I found a way to make it work (and keep my readonly property)

The solution is to keep our private variable, keep our read-only property and Add a read-write property with the option : 
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>

So when deserialize the object, the system will be able to edit the private data using the new read-write property
and the new read-write property is invisible from the Intelisense (using the option), so it keeps our way of having a read-only property.

It is not perfect, but it works.



```
Private _ID as string

Public ReadOnly Property ID As String
    Get
          Return Me._ID
    End Get
End Property


<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>
Public Property _ID_edit As String
     Get
          Return Me._ID
     End Get
     Set(value As Boolean)
          Me._ID = value
     End Set
End Property
```

----------

