none
Adding get property in Datacontract in wcf RRS feed

  • Question

  • Hi I have created a datacontract as follows,

       [DataContract]
        public class PCATemperatureInfo
        {
            [DataMember] public PCAAndControllers PCA;

            [DataMember] public const int MaxValues = 5;

            [DataMember] public int[] Values = new int[MaxValues];

            public PCATemperatureInfo(PCAAndControllers pcaAndControllers, int[] values)
            {
                PCA = pcaAndControllers;
                Values = values;
            }

            #region Accessors for Values

            #region SCON

            [DataMember]
            public int SCON_OnBoardTemperatureSensorValue { get; set; }>>No issues

            #endregion

            #region HVAget;

            [DataMember]
            public int HVA_OnBoardTemperatureSensorValue
            {
                get { return Values[0]; }
            }>>Throwing error

            [DataMember]
            public int HVA_MotorTemperatureSensorValue
            {
                get { return Values[1]; }
            }

            [DataMember]
            public int HVA_IGBTTemperatureSensorValue
            {
                get { return Values[2]; }
            }

            #endregion

            #region LVA

            [DataMember]
            public int LVA_OnBoardTemperatureSensorValue
            {
                get { return Values[0]; }
            }

            #endregion

            #endregion
        }

    My application is a windows application ,wcf service is hosted in a console application also m using netTcpBinding.

    Here i am filling my dataconctract class using a parameterized constructor.But while consuming this datacontract in the client side i am getting one exception "

    An unhandled exception of type 'System.ServiceModel.CommunicationObjectFaultedException' occurred in mscorlib.dll

    Additional information: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state"

    I identified the cause of issue. if i am using a default get,set property implementation its working fine,but i f i am using a get accessor only it is throwing exception .Can anyone help me to resolve this issue.


    Latheesh K Contact No:+91-9747369936


    • Edited by La07K Monday, May 8, 2017 1:03 PM
    Monday, May 8, 2017 1:02 PM

Answers

  • Hello Latheesh K,

    A readonly or in this case, a calculated field, is not supported in the data contract.  The DataMember attribute is used to tell WCF that this should be serialized.  WCF is just used to send content and not behavior.

    You can achieve this by not marking those properties as DataMembers but this needs to be on either the client and/or the  server (depending on where you want this functionality to run).


    Cheers, Jeff

    • Marked as answer by La07K Thursday, May 11, 2017 1:07 PM
    Monday, May 8, 2017 8:32 PM

All replies

  • Hello Latheesh K,

    A readonly or in this case, a calculated field, is not supported in the data contract.  The DataMember attribute is used to tell WCF that this should be serialized.  WCF is just used to send content and not behavior.

    You can achieve this by not marking those properties as DataMembers but this needs to be on either the client and/or the  server (depending on where you want this functionality to run).


    Cheers, Jeff

    • Marked as answer by La07K Thursday, May 11, 2017 1:07 PM
    Monday, May 8, 2017 8:32 PM
  • When adding Service Reference, the client will create a new separate class from the XML schema of the service, it did not use the server-side class directly.

    For more information about this, I suggest you refer the link below:

    #WCF: Exposing readonly DataMember properties without set?

    http://stackoverflow.com/questions/1873741/wcf-exposing-readonly-datamember-properties-without-set

    For a workaround, I suggest you refer the suggestion from stakx which shared DataContract between client and service.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 9, 2017 2:17 AM
  • Thanks Jeff for ur inputs

    Latheesh K Contact No:+91-9747369936

    Thursday, May 11, 2017 1:07 PM
  • Thanks Edward

    Latheesh K Contact No:+91-9747369936

    Thursday, May 11, 2017 1:08 PM