Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wcf

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?

like image 798
Exitos Avatar asked Jun 17 '11 13:06

Exitos


2 Answers

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);
like image 61
Lester Avatar answered Sep 17 '22 08:09

Lester


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

like image 41
Richard Blewett Avatar answered Sep 17 '22 08:09

Richard Blewett