Nunit-console: NUnit.Engine 3.12.0 .NET Core and Binary serialization

Created on 27 Apr 2021  路  2Comments  路  Source: nunit/nunit-console

Hello,

When using NUnit.Engine 3.12.0 to run tests (targeting .NET Core 3.1) doing binary serialization, tests fail with System.Runtime.Serialization.SerializationException.

How to reproduce

Sample code:

[Serializable]
public class MyCoolClass
{
    public MyCoolClass(string name)
    {
        Name = name;
    }
    public string Name { get; }
    public override bool Equals(object? obj)
    {
        return obj is MyCoolClass;
    }
    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}
private static T SerializeAndDeserialize<T>(T t)
{
    var formatter = new BinaryFormatter();
    byte[] array;
    using (var ms = new MemoryStream(16 * 1084))
    {
        formatter.Serialize(ms, t);
        array = ms.ToArray();
    }
    using (var ms = new MemoryStream(array))
    {
        return (T)formatter.Deserialize(ms);
    }
}
[Test] public void TestSerialization()
{
    var instance = new MyCoolClass("Class1");
    var deserialize = SerializeAndDeserialize(instance);
    Assert.AreEqual(instance, deserialize);
}

Then run test above using NUnit.ConsoleRunner.NetCore (which is using NUnit.Engine 3.12.0). For my case it was:

%userprofile%\.nuget\packages\nunit.consolerunner.netcore\3.12.0-beta1\tools\nunit3-console.exe D:\ForNUnit\bin\Debug\netcoreapp3.1\UnitTests.dll

Expected behavior

image

Actual behavior

Error : UnitTests.Test.TestSerialization
System.Runtime.Serialization.SerializationException : Unable to find assembly 'UnitTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
at System.Runtime.Serialization.Formatters.Binary.BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
at System.Runtime.Serialization.Formatters.Binary.BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(BinaryParser serParser, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, Boolean check)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at UnitTests.Test.SerializeAndDeserializeT in D:ForNUnitUnitTestsTest.cs:line 50
at UnitTests.Test.TestSerialization() in D:ForNUnitUnitTestsTest.cs:line 57

I think it is related to using a costume AssemblyLoadContext for NUnit; UnitTests.dll is indeed loaded in NUnit AssemblyLoadContext and not in the Default, but it seems like System.Runtime.Serialization.FormatterServices.Desirialize is trying to load it from the Default.

What would you advise for this use case?

Thank you in advance.

Most helpful comment

HI @bouchraRekhadda - believe you're seeing https://github.com/nunit/nunit-console/issues/828. That issue essentially is just waiting for someone to pick it up and investigate - we'd love some help, if you'd be able?

I'll close this issue as a duplicate, but thanks for providing another repro. 馃檪

All 2 comments

Any update on this issue please?

HI @bouchraRekhadda - believe you're seeing https://github.com/nunit/nunit-console/issues/828. That issue essentially is just waiting for someone to pick it up and investigate - we'd love some help, if you'd be able?

I'll close this issue as a duplicate, but thanks for providing another repro. 馃檪

Was this page helpful?
0 / 5 - 0 ratings