WCF BodyStyle WrappedRequest doesn't work for incoming JSON param?

13,146

Solution 1

The name of the wrapper is not the parameter type, but the parameter name. If you send it as {"data":{"TokenId":"some guid"}} it should work.

Or if you want to use some name other than the parameter name, you can use the [MessageParameter] attribute:

[OperationContract]
[WebInvoke(
    Method="POST",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "Authenticate")]
public AuthResponse Authenticate([MessageParameter(Name = "AuthRequest")] AuthRequest data)

Solution 2

This is a late reply, but I hope it helps somebody.

I was also trying to get a JSON "POST" web service to work, but it's parameter was always being set as null. Forget about trying to deserialize any JSON data, there was never anything there to work on!

public string CreateNewSurvey(string JSONdata)
{
    if (JSONdata == null)
        return "JSON received: NULL, damn.";
    else
        return "You just sent me: " + JSONdata;
}

My GET web services worked great, but this "POST" one drove me nuts.

My solution, oddly, was to change the parameter type from string to Stream.

public string CreateNewSurvey(Stream JSONdataStream)
{
    StreamReader reader = new StreamReader(JSONdataStream);
    string JSONdata = reader.ReadToEnd();

    //  Finally, I can add my JSON deserializer code in here!

    return JSONdata;
}

...and in my web.config...

  [OperationContract(Name = "createNewSurvey")]
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "createNewSurvey")]
  string CreateNewSurvey(Stream JSONdata);   

With this in place, finally my iPad application could call my WCF Service. I was a happy man! Hope this helps.

Share:
13,146
adamwtiko
Author by

adamwtiko

Developer, currently on the API team at Esendex.

Updated on June 08, 2022

Comments

  • adamwtiko
    adamwtiko almost 2 years

    I've been working on getting a RESTful WCF service to both accept a JSON as a parameter and return some JSON.

    This is my service:

        [OperationContract]
        [WebInvoke(
            Method="POST",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "Authenticate")]
        public AuthResponse Authenticate(AuthRequest data)
        {
            AuthResponse res = new AuthResponse();
            if (data != null)
            {
                Debug.WriteLine(data.TokenId);
                res.TokenId = new Guid(data.TokenId);
            }
            return res;
        }
    

    The above will set data to be null when I pass { AuthRequest: { TokenId = "some guid"} }.

    If I set the BodyStyle of the method to be Bare then data is set correctly but I must remove { AuthRequest } from the JSON (which I don't really want to do). Is there any way to get WrappedRequests to work with { AuthRequest: { TokenId = "some guid"} as the JSON?

    Thanks.

  • adamwtiko
    adamwtiko over 12 years
    Perfect answer :) No idea that its the parameter name not type, and the message parameter name option is rather good to know!
  • user1429595
    user1429595 almost 11 years
    Lets say you want to call this using POSTMAN, SoapUI how would you do it?
  • Thirisangu Ramanathan
    Thirisangu Ramanathan over 9 years
    @ Mike Gledhill ... and in my web.config...? :( IService.cs
  • NealWalters
    NealWalters over 7 years
    Saved my day! Almost same question, but I didn't find yours just because of different keywords: stackoverflow.com/questions/39048349/…