|
-
Mar 11th, 2009, 07:18 PM
#1
[RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
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.
-
Mar 13th, 2009, 05:48 AM
#2
Re: Unable to use ReadOnly Properties in WCF DataContract
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.
Code:
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.
-
Mar 13th, 2009, 06:14 AM
#3
Re: Unable to use ReadOnly Properties in WCF DataContract
Ahh I see, that makes sense I guess. thanks
-
Mar 13th, 2009, 12:40 PM
#4
Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
Does VB.NET have a private setter?
-
Mar 13th, 2009, 12:41 PM
#5
Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
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
-
Mar 13th, 2009, 01:33 PM
#6
Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
Get used to it, Im not talking to you anymore after your insults in this thread!
-
Jun 17th, 2009, 07:40 PM
#7
Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
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!
-
Mar 4th, 2015, 03:34 AM
#8
New Member
Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
 Originally Posted by 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
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.
-
Apr 10th, 2015, 03:23 AM
#9
New Member
Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract
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.
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|