Results 1 to 9 of 9

Thread: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unable to use ReadOnly Properties in WCF DataContract

    Ahh I see, that makes sense I guess. thanks
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract

    Does VB.NET have a private setter?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    New Member
    Join Date
    Mar 2015
    Posts
    2

    Re: [RESOLVED] Unable to use ReadOnly Properties in WCF DataContract

    Quote Originally Posted by mendhak View Post
    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.

  9. #9
    New Member
    Join Date
    Mar 2015
    Posts
    2

    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
  •  



Click Here to Expand Forum to Full Width