1

I have a WCF service that needs to be called via SOAP (working) and via JSON (not working).

My C# code:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(ResponseFormat=WebMessageFormat.Json)]
    string Foo();
}

public class Service1 : IService1
{
    public string Foo()
    {
        return "woohooo";
    }
}

And here's my config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="Service1">
        <endpoint address="soap" binding="basicHttpBinding" contract="DummyService.IService1"/>
        <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="DummyService.IService1"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
          <enableWebScript/>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

And here's my Javascript:

$.getJSON("/Service1.svc/Foo")
    .success(function (result) {
        alert("success! " + result);
    })
    .fail(function (result) {
        console.log("failed!");
        console.log(result);
    });

The response I get is "400 Bad Request". What am I doing wrong? I've followed the advice in REST / SOAP endpoints for a WCF service, but it still-a-no-worksies. :-) Help!

Community
  • 1
  • 1
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212

4 Answers4

1

I'm thinking it's because the URL you are using is an absolute URL, not a relative one.

If you are running this in the ASP.NET debugger, the service would be available on the following address:

http://localhost:52200/Web/Service1.svc

Because you are preceeding the URL with a forward slash, the service query is actually being sent to

http://localhost:52200/Service1.svc

So you need to pass in the appropriate relative address. If your service and the script file are in the same directory, change it to:

$.getJSON("./Service1.svc/Foo")
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
1

Try including the endpoint address in the service URL. Something like:

$.getJSON("/Service1.svc/json/Foo")

Also, you should enable WCF tracing to see what error is being returned. You will need the Service Trace Viewer to view the log information.

UPDATE:

Below is an example using your code that works.

The configuration file contains

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

    <services>
      <service behaviorConfiguration="jsonServiceBehavior" name="DummyService.Service1">
        <endpoint address="soap" binding="basicHttpBinding" contract="DummyService.IService1"/>
        <endpoint address="json" 
                  behaviorConfiguration="jsonBehavior" 
                  binding="webHttpBinding" contract="DummyService.IService1"/>
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="jsonServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

I created an empty web project and added the following HTML page to test the service.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.getJSON("/Service1.svc/json/Foo")
            .success(function (result) {
                alert("success! " + result);
            })
            .fail(function (result) {
                console.log("failed!");
                console.log(result);
            });
        });
    </script>
</head>
<body>    

</body>
</html>

And my Service1.svc has the following:

<%@ ServiceHost Language="C#" Service="DummyService.Service1" %>
Garett
  • 16,632
  • 5
  • 55
  • 63
1

Try taking out the <webHttp /> element from the endpoint behavior.

I have a web service that is invoked by Ajax, and it doesn't have this element.

When I tried putting the element in, it stopped working.

(Admittedly, this was to do with not recognizing the date formats, so it's not an exact match for your problem).

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
1

(My third answer. I know that ONE of these answers is going to be right :-)

Try setting aspNetCompatibilityEnabled to true on the serviceHostingEnvironment.

   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

This is just a guess. When I turn this off on my implementation the call still arrives, but HttpContext.Current returns NULL.

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205