WCF RestAPI在命中端点时如何调用构造函数

时间:2015-08-13 06:59:28

标签: c# asp.net wcf wcf-rest

Rest API WCF中的终点如下

     [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
     }

其json请求如下: -

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

我想知道用户类'从Postman命中API时调用构造函数。因为我想根据是否传递某些属性或者是否发送某些值或者将其作为null等发送来为属性设置一些复杂的计算。

1 个答案:

答案 0 :(得分:1)

听起来您希望在传递给user之前访问getName。您可以在反序列化user后立即执行此操作。

这可能有所帮助:

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

MSDN - OnDeserializedAttribute Class

MSDN链接显示了设置反序列化对象成员值的示例,该示例与您描述的目标类似。