1
votes

I've created a WCF web service which is hosted locally through IIS. I've used the WCF test client to confirm that the service is working properly, and I am now wanting to test through a manual REST call. I'm using RESTClient 3.1 to send the REST calls. I'm able to retrieve results from methods, but my attempts to send JSON in as parameters always results in null parameters. What am I doing wrong? The return body from my request is "FAIL :(" Thanks in advance! I've spent over a day on this problem so far.

Service Contract:

[OperationContract]
    [WebInvoke(
      Method = "POST",
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.Bare)]
    public string Route2(Position start)
    {
        if (start == null)
        {
            return "FAIL :(";
        }
        else
        {
            return "SUCCESS :)";
        }
    }

** web.config: **

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd"/>
    </handlers>
    <defaultDocument>
      <files>
        <add value="help" />
      </files>
    </defaultDocument>
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
      <service name="Primordial.GroundGuidance.Service.GroundGuidanceService">
        <endpoint address="soap" binding="basicHttpBinding" contract="Primordial.GroundGuidance.Service.GroundGuidanceService" />
        <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
      name="web" contract="Primordial.GroundGuidance.Service.GroundGuidanceService"
      kind="webHttpEndpoint" endpointConfiguration="webEndpointWithHelp" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
     </services>
    <standardEndpoints>
      <webHttpEndpoint>
         <standardEndpoint name="webEndpointWithHelp" helpEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Because I'm using RESTClient the call isn't just a string/file, but for header value pairs I have: Accept: application/json contentType: application/json

The body type is set to "application/json; charset=UTF-8" body:

{
  "elevation": 0,
  "latitude": 35.31,
  "longitude": -116.41
}
1

1 Answers

1
votes

After digging at my problem for another day, it ended up being my JSON, not my use of WCF. I needed to specify the method parameter names in my JSON. For my method

[WebInvoke(
      Method = "POST",
      ResponseFormat = WebMessageFormat.Json,
      BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string Route2(Position start, Position end)

the appropriate JSON was:

{
    "start": {
        "elevation": 1,
        "latitude": 35.3,
        "longitude": -116.4
    },
    "end": {
        "elevation": 1,
        "latitude": 35.3,
        "longitude": -116.4
    }
}