【问题标题】:WCF RestAPI how a constructor is called when endpoint is hitWCF RestAPI在命中端点时如何调用构造函数
【发布时间】:2015-08-13 06:59:27
【问题描述】:

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 时如何调用 User 类的构造函数。因为我想根据是否传递某些属性或是否发送某些值或发送为 null 等在 get set 中进行一些复杂的计算。

【问题讨论】:

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


    【解决方案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 链接显示了设置反序列化对象成员值的示例,这似乎与您描述的目标相似。

    【讨论】:

    • 是的,我感觉它可以帮助我,非常感谢,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2018-01-31
    • 1970-01-01
    相关资源
    最近更新 更多