0

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);
}

网络配置:

    <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 字符串始终为空。但是我使用提琴手来拦截请求 json 字符串正在发布但服务实现代码总是为空..

4

1 回答 1

0

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

data: JSON.stringify({ jsonString: "your_data" })
于 2013-01-02T11:06:17.270 回答