【问题标题】:Why cant I use two arguments in a WCF REST POST method?为什么我不能在 WCF REST POST 方法中使用两个参数?
【发布时间】:2011-09-17 05:00:55
【问题描述】:

我有合同:

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

我有实现方法:

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

  }

当我浏览到:

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

我收到此错误:

“/”应用程序中的服务器错误。 操作 'SubmitVideoPOST' 的 合同“IVLSContentService” 指定多个请求正文 要序列化的参数 任何包装元素。最多一具尸体 参数可以不被序列化 包装元素。要么删除 额外的身体参数或设置 BodyStyle 属性 WebGetAttribute/WebInvokeAttribute 到 包裹起来。

当我为 POST 添加新方法(我没有尝试访问)时,我才开始在 Get 请求上收到此错误,这是什么意思?我不能使用多个参数吗?

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    看看这个link,张贴者提出了同样的问题。

    相关部分是:

    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.
    

    因此,在您的情况下,您必须将您的运营合同更改为以下内容:

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

    【讨论】:

    • 干杯,是的,抱歉应该仔细看一下,只是对整个事情完全感到困惑,但现在明白了,谢谢!
    • 我也做了同样的事情,但它不起作用。我删除了 BodyStyle 然后它工作了
    【解决方案2】:

    您是否尝试将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped,就像提示的错误一样,如下所示:

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

    【讨论】:

      【解决方案3】:

      XML 不会有一个带有两个参数的根节点,这会使其格式不正确。要引入单个根节点,必须按照错误所说的做,“包装”它。这使得该方法期望在两条数据周围有一个包装元素

      将 BodyStyle = WebMessageBodyStyle.Wrapped 添加到 WebInvoke 属性

      【讨论】:

        【解决方案4】:

        我自己对 WCF REST 有点陌生,上周刚刚完成了我的第一次服务。但我有类似的问题。 This article 让我朝着正确的方向前进。包装器是我的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-07
          • 1970-01-01
          • 1970-01-01
          • 2021-08-02
          • 2012-02-08
          • 1970-01-01
          • 1970-01-01
          • 2021-08-15
          相关资源
          最近更新 更多