Exitos
Exitos

Reputation: 29720

Why cant I use two arguments in a WCF REST POST method?

I have the contract:

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetCategoriesGET/{userIdArg}", BodyStyle = WebMessageBodyStyle.Bare)]
    List<Video> GetVideosGET(string userIdArg);

    [WebInvoke(Method = "POST", UriTemplate = "evals")]
    [OperationContract]
    void SubmitVideoPOST(Video videoArg, string userId);

And I have the implementing methods:

public List<Video> GetVideosGET(string userIdArg)
{

  List<Video> catsToReturn = new List<Video>();

  if (Int32.Parse(userIdArg) == 1)
  {
      catsToReturn = catsForUser1;
  }
  else if (Int32.Parse(userIdArg) == 2)
  {
      catsToReturn = catsForUser2;
  }

  return catsToReturn;

  }


  public void SubmitVideoPOST(Video videoArg, string userId)
  {

  }

When I browse to:

http://localhost:52587/Api/Content/VLSContentService.svc/GetCategoriesGET/1

Im getting this error:

Server Error in '/' Application. Operation 'SubmitVideoPOST' of contract 'IVLSContentService' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.

I only started getting this error on the Get request when I added the new method for POST (which I havent tried to access), what does this mean? Cant I use more than one argument?

Upvotes: 9

Views: 26751

Answers (4)

Precious Roy
Precious Roy

Reputation: 1086

I'm somewhat new to WCF REST myself, just did my first service last week. But I had similar issues. This article started me in the right direction. The wrapper was my problem.

Upvotes: 1

Richard Blewett
Richard Blewett

Reputation: 6109

The XML will not have a single root node with two parameters which would make it non-wellformed. To introduce a single root node to have to do as the error says, "wrap" it. This makes the method expect a wrapper element around the two pieces of data

Add BodyStyle = WebMessageBodyStyle.Wrapped to the WebInvoke attribute

Upvotes: 6

ericvg
ericvg

Reputation: 3957

Did you try setting the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped like the error suggested, like this:

[WebInvoke(Method = "POST", UriTemplate = "evals", BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
void SubmitVideoPOST(Video videoArg, string userId);

Upvotes: 2

Lester
Lester

Reputation: 4413

Take a look at this link where the poster asks the same question.

The relevant part is:

WCF doesn't support more than one parameter with bare body, 
if you need pass several parameters in one post method operation, 
then we need set the BodyStyle to Wrapped.

So in your case you'd have to change your operation contract to the following:

[WebInvoke(Method = "POST", UriTemplate = "evals", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
void SubmitVideoPOST(Video videoArg, string userId);

Upvotes: 19

Related Questions