使用/ Parameters到WCF服务测试POST请求

时间:2013-01-22 18:13:50

标签: json wcf rest

我已经创建了一个通过IIS本地托管的WCF Web服务。我已经使用WCF测试客户端来确认服务是否正常工作,我现在想要通过手动REST调用进行测试。我正在使用RESTClient 3.1发送REST调用。我能够从方法中检索结果,但是我尝试在参数中发送JSON总是会导致null参数。我究竟做错了什么?我的请求的返回主体是“FAIL :(”提前感谢!到目前为止,我已经花了一天多的时间解决这个问题。

服务合同:

[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>

因为我正在使用RESTClient,所以调用不仅仅是一个字符串/文件,但对于标题值对,我有: 接受:application / json contentType:application / json

正文类型设置为“application / json; charset = UTF-8” 体:

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

1 个答案:

答案 0 :(得分:1)

在我的问题再挖掘一天后,它最终成为了我的JSON,而不是我对WCF的使用。我需要在我的JSON中指定方法参数名称。对于我的方法

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

适当的JSON是:

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