如何使用WCF服务REST模式? [英] How consume WCF service in REST mode?

查看:110
本文介绍了如何使用WCF服务REST模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部署WCF服务(.NET 4.0)。服务端配置如下:

Deployed wcf service (.net 4.0). Service side config looks like:

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

<endpointBehaviors>
  <behavior name="WebBehavior">
    <webHttp />
  </behavior>
</endpointBehaviors>

尝试消费服务的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>

我打电话给服务时,得到的异常:

I'm getting exception when calling service:

运行方法一合同   IMyService'指定多个   请求体参数要   序列化的,没有任何包装   元素。在大多数一种身体参数   无需包装序列化   元素。无论是删除多余的体   参数或设置BodyStyle   对物业   WebGetAttribute / WebInvokeAttribute到   包裹。

Operation 'Method1' of contract 'IMyService' 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.

一些google搜索后,我们已经设置 [WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest,ResponseFormat = WebMessageFormat.Xml)] 上的方法,但没有成功...

After some googling, we've set [WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Xml)] on methods, but no success...

一个有趣的事:总是有异常相同的方法名,连我打电话等方法...

One interesting thing: There is always the same method name in exception, even I'm calling other methods...

服务工作正常,在REST模式,而与浏览器输入法的名称,必要paramaters测试...

Service works fine in REST mode while testing with browser by entering method name and necessary paramaters...

推荐答案

看来你正面临着​​类似的问题,因为在这个线程:<一href="http://stackoverflow.com/questions/4346554/wcf-service-proxy-throws-exception-when-more-than-one-parameter-is-used-in-opera/4347005#4347005">WCF服务代理抛出异常时,多个参数用在[OperationContract的]方法。

It seems you're facing similar issue as in this thread: WCF Service Proxy throws exception when more than one parameter is used in [OperationContract] method.

看来你已经使用添加服务引用对话框中VS. VS ASR对话框不支持WCF REST完全创建代理code,因此,代理code丢失[WebInvoke]属性。你能尝试添加[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]属性在客户端代理业务?

"It seems you have created Proxy code using Add Service Reference dialog in VS. VS ASR dialog doesn't support WCF REST fully, so, the proxy code is missing [WebInvoke] attribute. Can you try adding [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] attribute on operations in client proxy?"

这篇关于如何使用WCF服务REST模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆