Travis Ingram
Travis Ingram

Reputation: 397

WCF method being called via webclient - HTTP 400 - Bad Request

I am consuming a WCF service and receiving the error message "The remote server returned an error: (400) Bad Request." when consuming via WebClient

public class WebClientService : WebClient
            ...
            base.Credentials = CredentialCache.DefaultCredentials;
            base.OpenReadCompleted += new OpenReadCompletedEventHandler(ReadJsonStringAsyncComplete);
            base.OpenReadAsync(new Uri("http://localhost/xxxx.svc/GetData"));
            ...

The WCF service to be consumed is as follows (note that both exist within the same solution (different projects)

Contract/Interface:-

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
IList<MyType> GetData();

Implementation:-

public IList<MyType> GetData()
{
    return (new MyTypeRepository()).All.ToList();
}

The configuration:-

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
  <service name="xxxx.Service.MyService" behaviorConfiguration="DataExtractBehavior">
    <endpoint address="http://localhost:1422/MyService.svc" binding="webHttpBinding" bindingConfiguration="DataExtractBinding" behaviorConfiguration="MyEndpointBehavior" contract="xxxx.Service.Common.IMyService"></endpoint>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="MyEndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceAuthorization principalPermissionMode="UseAspNetRoles" />
      <serviceCredentials>
        <windowsAuthentication allowAnonymousLogons="false" includeWindowsGroups="true" />
      </serviceCredentials>
    </behavior>
    <behavior name="DataExtractBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <etwTracking profileName="EndToEndMonitoring Tracking Profile" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <etwTracking profileName="EndToEndMonitoring Tracking Profile" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="DataExtractBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>

I can view the service definition page via http://localhost:1422/xxxx.svc however I can't seem to call the method GetData (added onto the end of the URI - http://localhost:1422/xxxx.svc/GetData)

Does anyone know why I would get a HTTP 400 - Bad Request

Many thanks Travis

Upvotes: 0

Views: 2485

Answers (2)

Travis Ingram
Travis Ingram

Reputation: 397

The problem was entirely my fault - I was making a "Post" request to the above service which is contracted as a "Get" request

Upvotes: 0

Prasad Kanaparthi
Prasad Kanaparthi

Reputation: 6563

Same code is working fine for me. Change your config setting like below,

  <endpointBehaviors>
    <behavior name="MyEndpointBehavior">
        <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>

And browse your service with http://localhost:1422/xxxx.svc/help you can GetData operation. Now try with http://localhost:1422/xxxx.svc/GetData it will work.

Also, It mat be the case that, Your application is running on another port (Other than 1422) if it so, Go to your Application(Project)->Properties->Web->Specific Port (Enter 1422) and now try this browse.

Hope this helps.

Upvotes: 2

Related Questions