Json.Net仅在Visual Studio中抛出OutOfMemoryException

Ric*_*Net 4 c# json.net deserialization visual-studio-2015

我有一些奇怪的行为,我无法弄清楚.

我正在使用WCF服务将文件保存到某个数据库表.WCF服务有一个方法,它将JSON字符串作为参数.在这种情况下,JSON是一个序列化命令,其中包含List<FileData>其他属性.WCF服务反序列化JSON并运行CommandHandler此特定命令.

最终用户在尝试上传大小为52 MB的文件时遇到了错误.WCF服务返回404错误.

我能够在Visual Studio中重现这一点.根据这篇文章更改配置文件后,404消失了.

但是现在出现了一个新的异常:当命令成功序列化客户端,由WCF成功处理后,反序列化抛出了一个OutOfMemoryException.这是堆栈跟踪的顶部:

在Newtonsoft.Json.JsonTextReader.ReadData(布尔追加,的Int32 charsRequired)在Newtonsoft.Json.JsonTextReader.ReadData(布尔附加)在Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer(CHAR引号)在Newtonsoft.Json.JsonTextReader.ParseString(CHAR报价)Newtonsoft.Json.JsonTextReader.ParseValue()at Newtonsoft.Json.JsonTextReader.ReadInternal()at Newtonsoft.Json.JsonReader.ReadAsBytesInternal()at Newtonsoft.Json.JsonTextReader.ReadAsBytes()at Newtonsoft.Json.Serialization.JsonSerializerInternalReader. ReadForType(JsonReader阅读器,JsonContract契约,布尔hasConverter)

我写了一个单元测试来证明这个bug.但是,不顾一切,这个测试通过,换句话说,没有OutOfMemoryException被抛出.

为完整性而进行的测试:

    [TestMethod]
    public void LoadBigFile_SerializeDeserialize_DoesntThrowOutOfMemoryException()
    {
        // Arrange
        byte[] bytes = new byte[80000000];
        Random r = new Random(23);
        r.NextBytes(bytes);

        var command = new SomeCommand(new List<FileData>
        {
            new FileData(
                fileFullName: @"D:\SomePdfFile.pdf",
                modifyDate: DateTime.MaxValue,
                data: bytes
                )
        });

        var data = JsonConvert.SerializeObject(command);

        // Act
        var deserializedCommand = 
              JsonConvert.DeserializeObject<SomeCommand>(data);

        // Assert
        Assert.AreEqual(bytes.Length, deserializedCommand.Files.First().Data.Length);
    }
Run Code Online (Sandbox Code Playgroud)

所以,我抓住了机会,改变了生产中的配置文件,并尝试上传相同的文件.这才有效!!! 不OutOfMemoryException!

现在我的问题是,为什么OutOfMemoryException只在Visual Studio中发生,而VS的同一个实例中的单元测试却没有?感觉有点奇怪,我无法测试在Visual Studio中上传大文件,而它在生产中工作.请注意,我也尝试在Release模式下运行Debug.

一些细节:

  • 使用Json.Net 7.0.1
  • Visual Studio 2015,更新2
  • WCF在本地IIS Express中托管,IIS在生产中
  • Windows 10最新版本为64位
  • 生产服务器Windows server 2008 R2 64位
  • .Net Framework 4.5.2

Ale*_* L. 5

我已经再现的OutOfMemoryException单元测试通过改变byte[] bytes = new byte[80000000];byte[] bytes = new byte[52000000];与在环(2次)运行.测试运行器是32位.

回到IIS Express - 我认为你使用的是32位版本.你可以改变它

工具| 选项| 项目和解决方案| 网站项目| 使用64位版本的IIS Express

  • 奇迹般有效!Thanx,从来不知道默认情况下IIS运行32位,总是认为它遵循构建配置. (2认同)