How consume WCF service in REST mode?

2020-05-24 06:19发布

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>

Trying consume service in web app, web.config looks like:

<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:

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.

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...

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

标签: .net rest wcf
1条回答
对你真心纯属浪费
2楼-- · 2020-05-24 07:20

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

"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?"

查看更多
登录 后发表回答