none
Question about DataMember RRS feed

  • Question

  • [DataContract] public class ConstMemberDefined { public ConstMemberDefined() { } private string m_HisServerName = "FTHistorianServer"; [DataMember] public string FTHISTORIAN_SERVER { get { return m_HisServerName; } } };

    if I list the upper code in WCF service , will lead the service can not started.

    but if i write in below way, the service can start normally.

    [DataContract]
        public class ConstMemberDefined
        {
            public ConstMemberDefined()
            { }
            [DataMember]
            private string m_HisServerName = "FTHistorianServer";
    
            
            public string FTHISTORIAN_SERVER
            {
                get
                {
                    return m_HisServerName;
                }            
            }
        };

    So my question is why the first code block not work?Thanks in advance.

    Tuesday, January 22, 2013 1:40 AM

Answers

  • Hi,

    basically readonly Property is not Supported over wires in WCF and FTHISTORIAN_SERVER Property is Readonky property. On the other hand, private members can be sent over the wired through WCF that's why when using private member you did not get any error and while using Readonly property you are getting error.


    One good question is equivalent to ten best answers.

    • Proposed as answer by Enrique Ferreyra Tuesday, January 22, 2013 7:11 PM
    • Marked as answer by Razer518 Wednesday, January 23, 2013 12:12 AM
    Tuesday, January 22, 2013 3:25 PM

All replies

  • Ans: Return variable should be a Datamember.

    When ever you try to execute the following code you got the following error.



    When you try to run the 2nd DataContract the return variable is a DataMember so you will not got the error.


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Tuesday, January 22, 2013 10:58 AM
  • Hi,

    basically readonly Property is not Supported over wires in WCF and FTHISTORIAN_SERVER Property is Readonky property. On the other hand, private members can be sent over the wired through WCF that's why when using private member you did not get any error and while using Readonly property you are getting error.


    One good question is equivalent to ten best answers.

    • Proposed as answer by Enrique Ferreyra Tuesday, January 22, 2013 7:11 PM
    • Marked as answer by Razer518 Wednesday, January 23, 2013 12:12 AM
    Tuesday, January 22, 2013 3:25 PM
  • Thank you, I got it.
    Wednesday, January 23, 2013 12:05 AM
  • Thank you.

    Is it ok if I put the [DataMember] on both private variable and public readonly propery?

    Wednesday, January 23, 2013 12:10 AM
  • Thank you.

    Is it ok if I put the [DataMember] on both private variable and public readonly propery?


    As i said, Readonly properties are not supported over wired in WCF so there is no way to do that and you will get error as you got earlier. So i reccomend do not apply [DataMember] attribute on ReadOnly properties.

    One good question is equivalent to ten best answers.

    Wednesday, January 23, 2013 6:33 AM