locked
How to Define method in DataContract class RRS feed

  • Question

  • I want to add some method in class that i define as a 'DataContract' class. I need that the client will call those method in some cases. Is it possible? How can i do it ? Thanks for any help.
    Sunday, April 3, 2011 7:34 AM

Answers

  • Hi 

    Thats not the objective of a data contract class. Data Contract is a formal agreement between client and service what data is being exchanged between them. 

    In service implementation you expose method using service interface and implementing it; and marked the method as [OperationContract]. 

    Thats how you define your Service Contract. 

     


    Tanvir Huda
    • Proposed as answer by Tanvir Huda Monday, April 4, 2011 1:27 AM
    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:55 AM
    Sunday, April 3, 2011 9:01 AM
  • As Tanvir pointed out in the above it's not a good practice, however you can try creating few public methods in the DC and try accessing them.
    • Proposed as answer by madhugilla Tuesday, April 5, 2011 2:42 PM
    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:55 AM
    Sunday, April 3, 2011 4:19 PM
  • There is no magic in this. Just add methods. Example

     

    [DataMember]
    public string FirstName {get; set;}

    [DataMember]
    public string LastName {get; set;}


    public string GetFullName{

    return Firstname + " " + LastName;

    }

    This require of course the service contracts and datacontracts is in a shared dll between the service and the client. (if you have a aspx portal that calls a service application)

     

    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:55 AM
    Sunday, April 3, 2011 5:17 PM

All replies

  • Hi 

    Thats not the objective of a data contract class. Data Contract is a formal agreement between client and service what data is being exchanged between them. 

    In service implementation you expose method using service interface and implementing it; and marked the method as [OperationContract]. 

    Thats how you define your Service Contract. 

     


    Tanvir Huda
    • Proposed as answer by Tanvir Huda Monday, April 4, 2011 1:27 AM
    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:55 AM
    Sunday, April 3, 2011 9:01 AM
  • As Tanvir pointed out in the above it's not a good practice, however you can try creating few public methods in the DC and try accessing them.
    • Proposed as answer by madhugilla Tuesday, April 5, 2011 2:42 PM
    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:55 AM
    Sunday, April 3, 2011 4:19 PM
  • There is no magic in this. Just add methods. Example

     

    [DataMember]
    public string FirstName {get; set;}

    [DataMember]
    public string LastName {get; set;}


    public string GetFullName{

    return Firstname + " " + LastName;

    }

    This require of course the service contracts and datacontracts is in a shared dll between the service and the client. (if you have a aspx portal that calls a service application)

     

    • Marked as answer by Yi-Lun Luo Friday, April 8, 2011 9:55 AM
    Sunday, April 3, 2011 5:17 PM