8

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: enter image description here

Ian Vink
  • 66,960
  • 104
  • 341
  • 555

1 Answers1

20

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; }
}
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Fantastic. That worked like a dream. After a day of frustration, I finally have a solution. – Ian Vink Jul 26 '11 at 21:39
  • Fantastic, i was struggling with null data passed into service and your suggestion resolved the issue. Thanks for sharing. – Signcodeindie Dec 17 '12 at 10:24
  • I think the magic line `BodyStyle = WebMessageBodyStyle.Bare` was what had been stumping me for past *don't want to count* hours! Thanks for posting this snippet! – Christopher Feb 20 '18 at 12:22