开发者

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 开发者_如何学Python 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?


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);


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


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);


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜