sandyiit
sandyiit

Reputation: 1705

Configure WCF Client to consume a WCF Service using HTTP GET

I have a WCF Service which allows only HTTP GET requests:

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

The service is exposed 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 config 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 a simple

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

On executing this code I get the error:

The remote server returned an unexpected response: (405) Method Not Allowed.

I checked with fiddler and found that my client is sending a POST message whereas the service expects a GET hence the error.

I wish to know how to configure the client so that is sends GET request to the service.

Upvotes: 4

Views: 15093

Answers (2)

Steve
Steve

Reputation: 2343

I had a similar problem where the generated proxy service interface on the client was missing the WebGet attribute on my methods.

I added the attribute manually and it resolved the problem.

So it seems like the best current 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.

Upvotes: 0

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65461

Use WebGet instead of WebInvoke

Edit

Start by changing your method to this:

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

Make sure that webhttpbinding is specified on the server side.

This fixes it on the server side.

Take a backup of your client code.

On the client side delete the service reference. Make sure that all config is removed.

Then add the service reference again. Now it shoud be OK.

Upvotes: 1

Related Questions