如何在REST模式下使用WCF服务?

bru*_*uto 5 .net rest wcf

部署了wcf服务(.net 4.0).服务端配置如下所示:

<endpoint address="" 
          binding="webHttpBinding" 
          bindingNamespace="https://mydomain/myservice/services"     
          behaviorConfiguration="WebBehavior"
          contract="MyService" />

<endpointBehaviors>
  <behavior name="WebBehavior">
    <webHttp />
  </behavior>
</endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

在Web应用程序中尝试使用服务,web.config如下所示:

<system.serviceModel>
    <client>      
        <endpoint name="MyServiceEndpointBasicHttp" 
                  address="http://myDomain/myService"
                  binding="webHttpBinding" behaviorConfiguration="webBehavior" 
                  contract="MyNamespace.IMyService" />
     </client>
     <behaviors>
         <endpointBehaviors>
             <behavior name="webBehavior">
                 <webHttp />       
             </behavior>     
          </endpointBehaviors>
     </behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

我在拨打服务时遇到异常:

合同'IMyService'的操作'Method1'指定要序列化的多个请求体参数,而不包含任何包装元素.最多可以在没有包装元素的情况下序列化一个body参数.删除额外的body参数或将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为Wrapped.

经过一些谷歌搜索,我们已经开始[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Xml)]采用方法,但没有成功......

一件有趣的事情:异常中始终存在相同的方法名称,即使我正在调用其他方法......

通过输入方法名称和必要的参数,使用浏览器进行测试时,服务在REST模式下正常工作......

ami*_*mit 3

看来您遇到了与此线程类似的问题:WCF Service Proxy throws exceptionwhen more oneparameter is using in [OperationContract] method

“看来您已经使用 VS 中的“添加服务引用”对话框创建了代理代码。VS ASR 对话框不完全支持 WCF REST,因此,代理代码缺少 [WebInvoke] 属性。您可以尝试添加 [WebInvoke(BodyStyle = WebMessageBodyStyle. Wrapped)] 客户端代理中操作的属性?”