WCF RestAPI 如何在命中端点时调用构造函数
WCF RestAPI how a constructor is called when endpoint is hit
Rest中的一个结束点APIWCF如下
[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 class' 构造函数。因为我想根据是否传递某些属性或者是否发送某些值或将某些值作为 null 等在属性的 get set 中进行一些复杂的计算
听起来您想在 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 link 展示了设置反序列化对象成员值的示例,这似乎与您描述的目标相似。
Rest中的一个结束点APIWCF如下
[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 class' 构造函数。因为我想根据是否传递某些属性或者是否发送某些值或将某些值作为 null 等在属性的 get set 中进行一些复杂的计算
听起来您想在 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 link 展示了设置反序列化对象成员值的示例,这似乎与您描述的目标相似。