简体   繁体   English

无法使用两个或多个参数调用 WCF 服务合同

[英]Can't call WCF service contract with two or more parameters

This is my WCF contracts:这是我的 WCF 合同:

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "{type}/sports")]
    List<SportsList> GetSportsList(string type);

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "{type}/sports/{sport}/championships")]
    ChampsCountries GetChampsBySport(string type, string sport);

When I try to call it from my WPF application:当我尝试从我的 WPF 应用程序调用它时:

                Service1Client client = new Service1Client();
                SportsList[] sports = client.GetSportsList("line");

As you see I am calling the first contract, but it gives an Error regarding my second contract:如您所见,我正在调用第一个合同,但它给出了关于我的第二个合同的错误:

Operation 'GetChampsBySport' of contract 'IService1' 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 Operation 'GetChampsBySport' of contract 'IService1' 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 . Operation 'GetChampsBySport' of contract 'IService1' 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 already have changed BodyStyle to Wrapped and what else should I do?我已经将BodyStyle更改为Wrapped ,我还应该做什么? I am able to call that contract from browser with no problem.我可以毫无问题地从浏览器调用该合同。 Maybe something to do with my config?也许与我的配置有关? This is my WPF application config:这是我的 WPF 应用程序配置:

<client>
  <endpoint address="http://localhost:1001/Services/Service1.svc" behaviorConfiguration="webBehavior"
    binding="webHttpBinding" contract="MainService.IService1" />
</client>

When you add WCF reference in your Visual Studio project, it doesn't fully support all WCF REST methods.在 Visual Studio 项目中添加 WCF 引用时,它并不完全支持所有 WCF REST 方法。 I suggest to open your Reference.cs file and find yor 'GetChampsBySport' operation.我建议打开您的Reference.cs文件并找到您的“GetChampsBySport”操作。 Then add the following:然后添加以下内容:

[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]

If you can't add WebInvoke then add a reference to System.ServiceModel.Web extension from References.如果您无法添加WebInvoke则从 References 添加对System.ServiceModel.Web扩展的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM