开发者

WCF REST POST of JSON: Parameter is empty

开发者 https://www.devze.com 2023-03-23 06:34 出处:网络
Using Fiddler I post a JSON message to my WCF service. The service uses System.ServiceModel.Activation.WebServiceHostFactory

Using Fiddler I post a JSON message to my WCF service. The service uses System.ServiceModel.Activation.WebServiceHostFactory

[OperationContract]
[WebInvoke
(UriTemplate = "/authenticate",
       Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
  开发者_如何学编程     BodyStyle = WebMessageBodyStyle.WrappedRequest
       )]
String Authorise(String usernamePasswordJson);

When the POST is made, I am able to break into the code, but the parameter usernamePasswordJson is null. Why is this?

Note: Strangly when I set the BodyStyle to Bare, the post doesn't even get to the code for me to debug.

Here's the Fiddler Screen:

WCF REST POST of JSON: Parameter is empty


You declared your parameter as type String, so it is expecting a JSON string - and you're passing a JSON object to it.

To receive that request, you need to have a contract similar to the one below:

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

精彩评论

暂无评论...
验证码 换一张
取 消