WCF Rest POST方法获取null json字符串

时间:2013-01-02 04:50:30

标签: json wcf rest

WCF Rest POST始终获取空字符串。我花了一个多星期的时间去探索但到目前为止没有运气......

我的服务代码:

[ServiceContract]
public interface ISaveSurvey
{   
    [WebInvoke(Method = "POST",
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,               
           BodyStyle = WebMessageBodyStyle.Wrapped,
           UriTemplate = "/SSS")]        
    bool InsertSiteSurveyInfo(string jsonString);
}

Web配置:

    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Honeywell.HBS.SiteSurvey.SaveSurvey" behaviorConfiguration="SaveSurvey">
        <endpoint address="" binding="webHttpBinding" contract="Honeywell.HBS.SiteSurvey.ISaveSurvey" behaviorConfiguration="wcfRestBehavior"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SaveSurvey">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
       <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="wcfRestBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
      <bindings>
          <!-- Customizations for REST service -->
          <webHttpBinding>
              <binding name="ApiExportBinding" maxReceivedMessageSize="10485760"
                              maxBufferPoolSize="10485760" maxBufferSize="10485760">
                  <readerQuotas maxDepth="32" maxStringContentLength="10485760"
                                  maxArrayLength="10485760" maxBytesPerRead="10485760" />
                  <security mode="None" />
              </binding>
          </webHttpBinding>
      </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

客户代码:

<script type="text/javascript" language="javascript">
    function testRest() {

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "http://ie22ltjpsd4bs/SSTest/SaveSurvey.svc/SSS",
            contentType: "application/json",
            data: "{\"siteInformation\":{\"siteName\":\"Malar\",\"siteNumber\":\"123344555\",\"siteContactPerson\":\"test\",\"siteAddress\":\"test\",\"siteCity\":\"test\",\"sitePostalZipCode\":\"test\",\"siteTelephone\":1234576788,\"siteContractNumber\":\"asdfasdf\",\"siteHoneywellBranch\":\"asdfasdf\",\"siteFieldSiteLeader\":\"asdfasdf\",\"siteTeamLead\":\"asdfasdf\",\"siteTechnician\":\"asdfasdf\",\"equipments\":[{\"equipmentID\":\"BRANCH TOOL INVENTORY\",\"equipSiteContactTelephone\":12345678,\"equipDescription\":\"asdfasdf\",\"equipLocation\":\"asdfasdf\",\"equipSupervisionOrFrontEnd\":\"asdfasdf\",\"equipNumberOfStations\":12,\"equipMakeOrTypePC\":\"asdfasdf\",\"equipWindowsVersion\":\"asdfasdf\",\"equipManufacturer\":\"A C MOTOR\",\"equipManufacturerDateOrAge\":\"1979-12-31T18:30:00.000Z\",\"equipProductNameOrVersion\":\"asdfasdf\",\"equipGraphicSoftwareIncl\":\"No\",\"equipExistingSaveBackup\":\"Yes\",\"equipBackupSaveFormat\":\"\",\"equipLocalAccess\":\"asdfasdf\",\"equipPassword\":\"asdfasdfasdf\",\"equipRemoteAccess\":\"Modem\",\"equipAccessInfo\":\"asdfasdf\",\"equipSystemArchitecture\":\"Yes\",\"equipArchivedInAdept\":\"Yes\",\"equipOtherInformation\":\"asdfasdf\",\"controllersInformation\":[{\"controllerName\":\"asdfasdf\",\"controllerQuantity\":\"121\",\"controllerManufacturer\":\"asdfasdf\",\"controllerSoftwareProgram\":\"Yes\",\"controllerProductNameOrVersion\":\"asdfasdf\",\"controllerTypeOfProgram\":\"Interpreted\",\"controllerExistingSaveBackup\":\"Yes\",\"controllerBackUpSaveFormat\":\"\",\"controllerLocalAccess\":\"asdf\",\"controllerPassword\":\"asdf\",\"controllerRemoteAccess\":\"Modem\",\"controllerAccessInfo\":\"asdf\",\"controllerControlDrawings\":\"Yes\",\"controllerOperatingSequence\":\"Yes\",\"controllerOtherInformation\":\"asdfasddf\"}]}]}}",
            success: function(result)
            {
                alert("Sucess");
            }, 
            failure: function(result)
            {
                alert("Failure");
            }
        });
    }
    function (result) {     
    }
</script>

按钮点击:

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" OnClientClick="testRest()" Text="Button" />

我附加了w3wp进程并尝试调试服务,json字符串始终为null。但是我使用fiddler来拦截json字符串被发布的请求,服务实现代码总是变为null ..

1 个答案:

答案 0 :(得分:0)

这是计划行为。您的服务不知道如何映射您传递的数据。 如果你要把你的json东西作为字符串传递,你应该像这样包装它:

data: JSON.stringify({ jsonString: "your_data" })