2012-05-03 34 views
6

Trong ASP.NET MVC 4.0 WebAPI (phiên bản beta), tôi trả về một đối tượng được tuần tự hóa thành JSON lớn hơn 65kb. Như bạn có thể thấy từ theo dõi ngăn xếp bên dưới, khung công tác đang ném một ngoại lệ vì điều này vượt quá một số giới hạn nội bộ.ASP.NET MVC 4 WebAPI (beta) - cách thay đổi kích thước bộ đệm phản hồi tối đa

System.Net.Http.HttpRequestException: Cannot write more bytes to the buffer than the configured maximum buffer size: 65536. 
    at System.Net.Http.HttpContent.LimitMemoryStream.CheckSize(Int32 countToAdd) 
    at System.Net.Http.HttpContent.LimitMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) 
    at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) 
    at System.IO.StreamWriter.Write(Char value) 
    at Newtonsoft.Json.JsonWriter.AutoComplete(JsonToken tokenBeingWritten) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonWriter.cs:line 634 
    at Newtonsoft.Json.JsonTextWriter.WritePropertyName(String name) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonTextWriter.cs:line 204 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty(JsonWriter writer, Object memberValue, JsonProperty property, JsonContract contract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 209 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 336 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty(JsonWriter writer, Object memberValue, JsonProperty property, JsonContract contract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 209 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 336 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 445 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty(JsonWriter writer, Object memberValue, JsonProperty property, JsonContract contract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 209 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 336 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty(JsonWriter writer, Object memberValue, JsonProperty property, JsonContract contract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 209 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 336 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 445 
    at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 59 
    at xxx.Api.JsonNetFormatter.<>c__DisplayClass5.<OnWriteToStreamAsync>b__4() in c:\xxx\xxx\xxx.Api\JsonNetFormatter.cs:line 79 
    at System.Threading.Tasks.Task.Execute() 

Tôi không thể tìm thấy cách nào để thay đổi mặc định này. Có ai giúp được không?

nhờ

+0

Bạn đã thử cách này: http://forums.asp.net/t/1795106.aspx/1?Set+length+of+web+response – syneptody

+0

@syneptody Tôi không sử dụng trình tuần tự json mặc định để tôi ' m không chắc chắn nếu điều này áp dụng. cảm ơn cho liên kết anyway, sẽ có một fiddle dựa trên các đề xuất của nó. – Andrew

+0

Bạn có thể đăng các phần liên quan của lớp 'JsonNetFormatter' của bạn không? – nemesv

Trả lời

3

Theo System.Net.Http.dll phát hành với nguồn hàng đêm đang xây dựng, giá trị của kích thước bộ đệm tối đa được quy định tại HttpContent như:

internal const long MaxBufferSize = 0x7fffffffL; // 2147483647 

Vì vậy, nếu bạn chuyển với phiên bản mới (thay vì trình cài đặt beta), sự cố sẽ biến mất.

Tôi đề nghị để chuyển đổi nào bất kể vấn đề này vì có khá một vài thay đổi phá vỡ và bất cứ điều gì bạn đang xây dựng tại là bảo đảm không làm việc với việc phát hành chính thức.

+0

cảm ơn - có vẻ như đây là những gì tôi sẽ phải làm, than ôi. – Andrew

+0

Đã chuyển đổi công việc chưa? Tôi thấy chính xác theo dõi ngăn xếp này nhưng tôi không sử dụng bất kỳ phiên bản beta nào - theo như tôi biết. – PandaWood

Các vấn đề liên quan