Common Language Runtime detected an invalid program error in unit testing

我怕爱的太早我们不能终老 提交于 2019-12-11 09:07:28

问题


I have the following code

 this.SafeUpdate(rate, Guid.Parse(import.myGuid), c => c.myGuid);

SafeUpdate basically takes the parsed guid value and applies it to the myGuid property on the rate object. This works fine from my front end, but throws the "CLR detected..." error when run in a unit test. What's odd is the same statement for DateTime.Parse and int.Parse works fine. It just fails for Guid and decimals. I don't believe the error is with the parsing (it has the correct parsed value when extracted into a separate variable). I don't believe it's the mocking either as the statement works fine for all other types other than guid and decimal. Any ideas?


回答1:


We experienced a similar error yesterday on our build server. Our exception was thrown by the ReadObject() method of a DataContractSerializer.

System.InvalidProgramException: Common Language Runtime detected an invalid program.
 at System.Xml.EncodingStreamWrapper..ctor(Stream stream, Encoding encoding)
 at System.Xml.XmlUTF8TextReader.SetInput(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
 at System.Xml.XmlDictionaryReader.CreateTextReader(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
 at System.Xml.XmlDictionaryReader.CreateTextReader(Stream stream, XmlDictionaryReaderQuotas quotas)
 at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream) 

We've written a console app that does just this one thing. It runs without error, but fails in a simple unit test. We are using Gallio/MbUnit 3.4.11.0 for our test framework with a target of .net 4.0.

using System;
using System.IO;
using System.Runtime.Serialization;
using MbUnit.Framework;

namespace TestApp
{
    [TestFixture]
    class Program
    {
        static void Main()
        {
            FooBar();
            Console.ReadKey();
        }

        public static void FooBar()
        {
            var type = typeof(string);
            var foo = "foo";

            using (var stream = new MemoryStream())
            {
                var serializer = new DataContractSerializer(type);
                serializer.WriteObject(stream, foo);
                stream.Seek(0, SeekOrigin.Begin);
                var deserializer = new DataContractSerializer(type);
                var bar = deserializer.ReadObject(stream);
                Console.WriteLine(bar);
            }
        }

        [Test]
        public void Test()
        {
            FooBar();
        }
    }
}

The application runs fine, but the test throws. Strangely, this test passes on my dev box but fails on our build server as well as the dev box of a coworker. Clearly, there is something different about my dev box that allows the test to pass, but I have not located that difference yet.

Update 1 The version of System.dll on my dev box is 4.0.30319.296 but on the build server and the dev box of my coworker it is 4.0.30319.1001. System.Xml.dll and System.Runtime.Serialization.dll are identical at 4.0.30319.1, however.

Update 2 A quick google search for "4.0.30319.1001" returns this security update, http://support.microsoft.com/kb/2742595, which was applied to both our build server and the dev box of my coworker, but not my dev box. I uninstalled the update on the build server, rebooted, and the issue went away! I guess Microsoft doesn't have a unit test for this one yet. :-)



来源:https://stackoverflow.com/questions/14333264/common-language-runtime-detected-an-invalid-program-error-in-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!