首页 > 解决方案 > 删除额外的正文参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped

问题描述

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

错误:抛出异常:System.ServiceModel.Web.dll 中的“System.InvalidOperationException” 合同“IService1”的操作“ping”指定要序列化的多个请求主体参数,而无需任何包装器元素。最多一个 body 参数可以在没有包装元素的情况下被序列化。删除额外的正文参数或将 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>

PS 服务在浏览器/邮递员以及 android 应用程序客户端中运行良好。

标签: c#web-serviceswcfsoapdesktop-application

解决方案


是的,我们已经在服务器端设置了配置。但是当我们通过客户端代理调用服务时,调用 Restful 服务和调用 SOAP 服务之间有一个共同点。也就是说,我们应该保持服务器端和客户端之间的服务契约一致。因此请考虑将以下代码段添加到客户端自动生成的服务合同中。
参考.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);

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


推荐阅读