JSON的WCF REST POST:参数为空

Ian*_*ink 8 wcf json fiddler

使用Fiddler我将JSON消息发布到我的WCF服务.该服务使用System.ServiceModel.Activation.WebServiceHostFactory

[OperationContract]
[WebInvoke
(UriTemplate = "/authenticate",
       Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest
       )]
String Authorise(String usernamePasswordJson);
Run Code Online (Sandbox Code Playgroud)

进行POST后,我可以进入代码,但参数usernamePasswordJsonnull.为什么是这样?

注意:当我将BodyStyle设置为Bare时,该帖子甚至没有找到我调试的代码.

这是小提琴画面: 在此输入图像描述

car*_*ira 20

您将参数声明为String类型,因此它需要一个JSON字符串 - 并且您将JSON对象传递给它.

要收到该请求,您需要签订类似下面的合同:

[ServiceContract]
public interface IMyInterface
{
    [OperationContract]
    [WebInvoke(UriTemplate = "/authenticate",
           Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Bare)]
    String Authorise(UserNamePassword usernamePassword);
}

[DataContract]
public class UserNamePassword
{
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public string Password { get; set; }
}
Run Code Online (Sandbox Code Playgroud)