Configuring a WCF client to use a WCF service using HTTP GET

I have a WCF service that only allows HTTP GET requests:

[WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json)] public string GetAppData() 

Service is opened using webHttpBinding

 <system.serviceModel> <bindings> <webHttpBinding> <binding name="AppSvcBinding"> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Windows" /> </security> </binding> </webHttpBinding> </bindings> <behaviors> 

I have my client whose configuration looks like

 <system.serviceModel> <client> <endpoint address="http://localhost/AppService/Service.svc" binding="webHttpBinding" bindingConfiguration="webHttpBindingConfig" contract="AppSvc.IService" behaviorConfiguration="AppSvcBehavior" name="AppSvcClient"> <identity> <dns value="localhost"/> </identity> </endpoint> </client> <bindings> <webHttpBinding> <binding name="webHttpBindingConfig"> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Windows" /> </security> </binding> </webHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="AppSvcBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> 

My client code is simple

 ServiceClient client = new ServiceClient("AppSvcClient"); String result = client.GetAppData(); 

When executing this code, I get an error message:

The remote server returned an unexpected response: (405) Method not allowed.

I checked with a violinist and found that my client was sending a POST message, while the service was expecting a GET, hence an error.

I want to know how to configure the client to send a request to the GET service.

+4
source share
2 answers

Use WebGet instead of WebInvoke

Edit

Start by changing the method as follows:

 [WebInvoke(Method="GET", ResponseFormat=WebMessageFormat.Json,UriTemplate = "/")] public string GetAppData() 

Make sure webhttpbinding is specified on the server side.

This fixes it on the server side.

Back up your client code.

On the client side, delete the service link. Make sure all settings are deleted.

Then add the service link again. Everything will be fine now.

+1
source

I had a similar problem when the client WebGet not have the WebGet attribute for my methods on the created proxy service interface on the client.

I added the attribute manually and it solved the problem.

Thus, the best solution is to extract the service interfaces into a separate assembly, and then share this assembly between the server and its clients.

The automatic proxy generator seems to be buggy.

0
source

Source: https://habr.com/ru/post/1339240/


All Articles