Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF service using Json Bad Request

Tags:

json

c#

wcf

I can not for the life of me figure out whats going on and who I can't post to my service using json. I've tried reading every comment under the sun from google on the issues I have but everything is currently bringing me to a dead end. Please help!

I pass the postback service off to a third party by a callback URL in a post to a service. The Third party then posts back in Json back to my wcf service using the call back url. I have no problem with the initial post but they and myself are not able to hit the callback service. I tried yet Fiddler returns a 400 error but i'm not sure why. I need a little more than web links and such to fix this problem. please help!

Web.config file

<system.serviceModel>
<services>
  <service behaviorConfiguration="serviceBehavior" name="IBVWebService.InstantBankVerificationPostBack">
    <endpoint address="http://localhost:64337/InstantBankVerificationPostBack.svc" behaviorConfiguration="web" binding="webHttpBinding" contract="IBVWebService.IInstantBankVerificationPostBack"></endpoint>
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Web Interface

    [OperationContract]
    [WebInvoke(
    Method = "POST",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json)]
    void PostBack(String json);

Test Client

        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";
        client.Encoding = System.Text.Encoding.UTF8;
        string jsonInput = "{'data':'testvalue'}";
        client.UploadString("http://localhost:64337/InstantBankVerificationPostBack.svc/PostBack", jsonInput);

current trace log. Tracelog

like image 434
Tyler Buchanan Avatar asked Jun 12 '15 17:06

Tyler Buchanan


1 Answers

I have replicated your scenario, with a simple wcf service with the configurations you have and a simple test client :

WCF

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    void PostBack(String json);

Client :

string jsonInput = "{\"json\":\"testvalue\"}";
using (var client = new WebClient())
{
     client.Headers["Content-type"] = "application/json";
     client.Encoding = System.Text.Encoding.UTF8;
     client.UploadString("http://localhost:51175/Service1.svc/PostBack", "POST", jsonInput); 
}

In the client make sure you match the signature of your WCF method, that is the object you are expecting is called json, so when you call the method from your client send json:'value'

Also, consider using a using statement for the webclient, to assure disposal after using it.

like image 161
Mihai Tibrea Avatar answered Oct 05 '22 10:10

Mihai Tibrea