Search code examples
c#asp.netwcfwcf-rest

WCF RestAPI how a constructor is called when endpoint is hit


A end point in Rest API WCF is as follows

     [OperationContract]
     [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json, UriTemplate = "/getName")]
     string  getName(User user);

     public string getName(User user)
     {
         //do what ever
     }

Its json request is as follows:-

   {
    "user":
         {
           "FirstName":"nuser18",
           "LastName":"nuser18" ,
           ......
           ......
           ......      
         }
    }

I want to know how the User class' constructor is called when the API is hit from Postman. As I want to do some complex calculations in get set for properties based on if some properties are passed or not or if some values are sent or not or sent as null etc.


Solution

  • It sounds like you want to access user before it is passed to getName. You could do that immediately after deserialization of user.

    This could help:

    How to use Custom Serialization or Deserialization in WCF to force a new instance on every property of a datacontact

    MSDN - OnDeserializedAttribute Class

    The MSDN link shows example where it sets the value of a member of the deserialized object, which seems similar to your described goal.