2015-08-27 57 views
1

我正在使用.net Web Api爲我的數據網格獲取數據。呼叫是通過Ajax調用了這樣的從Web API獲取大型JSON

$.ajax({ 
 
    type: 'GET', 
 
    dataType: 'json', 
 
    contentType: "application/json; charset=utf-8", 
 
    url: ReportURL, // "api/AppData/InvoiceReport/10" 
 
    success: function (mydata) { 
 
     
 
    console.log(mydata); 
 
    createReportGrid(myData); // this function creates a KENDO grid 
 
    },     
 
    error: function (error) { 
 
    alert(error); 
 
    } 
 
});

在Web API方法看起來這

[HttpGet] 
 
public HttpResponseMessage InvoiceReport(int Id) 
 
{ 
 
    // some llogic of data retrieving 
 
    // objReportDataList is of Type List<vmReport> 
 
    // thisstructure contains a DataTable, and 2 more list type 
 
return Request.CreateResponse(HttpStatusCode.OK, objReportDataList); 
 
}

這些調用工作完美的行計數約爲100K

Web Api完美序列化。但是,當行數超過200K我得到500內部服務器錯誤 堆棧跟蹤告訴「系統OutOfMemoryException異常,發生功能於mscorlib程序,DLL」

注意 - 我不能使用服務器分頁得到只有很少的數據。這百萬行數據正在處理ASP.NET Webforms應用程序。我們已經遷移到MVC模式,並使用WebApi來獲取數據,但出現此錯誤。 PS - 我已經嘗試了很多很多解決方案,但無奈

請指導我得到這個錯誤刪除,我的報告走了

+0

檢查web表單解決方案的Web.config。我敢打賭,那裏有一個設置。這聽起來像是一個配置問題。除了這個事實,這是你已經知道的大量數據。 – beautifulcoder

+0

我懷疑web.config有任何設置,可以避免內存不足的錯誤...雖然我可能是錯的。 –

+0

web.config有JSON限制設置....我使用JSON.NET序列化對象。數據是我知道的。當它可以產生200K記錄時,爲什麼不進一步? –

回答

0

您可以在web.config文件中添加以下

<configuration> 
    <system.web.extensions> 
     <scripting> 
      <webServices> 
       <jsonSerialization maxJsonLength="50000000"/> 
      </webServices> 
     </scripting> 
    </system.web.extensions> 
</configuration> 
+0

這並不幫助.....,我使用JSON.NET –

+0

我認爲這是需要你 2.返回一些別名爲性的判定例如一兩件事, 1只返回那些列。 EmployeeName =「拉維」,迴歸爲=「拉維」因爲這是供內部使用,你知道什麼是 – Ravi

+1

或者你可以重組它使用列VS數據的方法:'{列:[「名」,」地址','計劃','年齡'],數據:[''bob','canada','sorry',32],['john','usa','fantastic',16]]} –

2

您應該傳遞迴應。那麼你可以返回數以百萬計的塊發送的行。

[HttpGet] 
public HttpResponseMessage PushStreamContent() 
{ 
    var response = Request.CreateResponse(); 

    response.Content = 
     new PushStreamContent((stream, content, context) => 
    { 
     foreach (var staffMember in _staffMembers) 
     { 
      var serializer = new JsonSerializer(); 
      using (var writer = new StreamWriter(stream)) 
      { 
       serializer.Serialize(writer, staffMember); 
       stream.Flush(); 
      } 
     } 
    }); 

    return response; 
} 

更多的信息在這裏: http://dblv.github.io/2014/07/02/streaming-web-api/