删除额外的主体参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性 设置为 Wrapped

remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped

我收到这个错误,要求我添加 BodyStyle 属性,而我已经在我的 webinvoke 中添加了它。

错误: 抛出异常:System.ServiceModel.Web.dll 中的 'System.InvalidOperationException' 合约 'IService1' 的操作 'ping' 指定要序列化的多个请求主体参数,没有任何包装元素。在没有包装器元素的情况下,最多可以序列化一个主体参数。要么删除额外的主体参数,要么将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性 设置为 Wrapped。 这是我的服务合同:

[OperationContract]
    [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "ping/{pNo},{cno}",
        RequestFormat = WebMessageFormat.Json)]
    string ping(string pNo,string cno);

我在我的 c# 桌面应用程序中这样调用它:

    Greenlines.Service1Client service = new Greenlines.Service1Client();
            service.ping("1", "2");

我的 app.config ServiceModel 是:

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingWithJsonP" />
  </webHttpBinding>
</bindings> 
<client>
  <endpoint 
    address="http://192.168.100.116/WcfService/Service1.svc"
    binding="webHttpBinding" 
    bindingConfiguration="webHttpBindingWithJsonP"
    contract="Greenlines.IService1" 
    behaviorConfiguration="MyWebb"
    name="webHttpBindingWithJsonP" />
</client>
<behaviors>

<endpointBehaviors>

<behavior name="MyWebb">

<webHttp />

</behavior>

</endpointBehaviors>

</behaviors> 
</system.serviceModel>

P.S。服务在 browser/postman 和 android 应用程序客户端中工作正常。

是的,我们已经在服务器端设置了配置。但是,当我们通过客户端代理调用服务时,对 Restful 服务的调用与对 SOAP 服务的调用之间存在共同点。也就是说,我们应该保持服务器端和客户端之间的服务契约一致。因此,请考虑将以下代码段添加到客户端自动生成的服务合约中。
Reference.cs.

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
        [WebGet(UriTemplate = "ping/{value1},{value2}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        string GetData(string value1, string value2);

如果问题仍然存在,请随时告诉我。