Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUnit.Engine 3.12.0 .NET Core and Binary serialization #939

Closed
bouchraRekhadda opened this issue Apr 27, 2021 · 3 comments
Closed

NUnit.Engine 3.12.0 .NET Core and Binary serialization #939

bouchraRekhadda opened this issue Apr 27, 2021 · 3 comments

Comments

@bouchraRekhadda
Copy link

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.SerializeAndDeserialize[T](T t) in D:\ForNUnit\UnitTests\Test.cs:line 50
at UnitTests.Test.TestSerialization() in D:\ForNUnit\UnitTests\Test.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.

@bouchraRekhadda
Copy link
Author

Any update on this issue please?

@ChrisMaddock
Copy link
Member

HI @bouchraRekhadda - believe you're seeing #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. 🙂

@CharliePoole
Copy link
Collaborator

@bouchraRekhadda
Since you use NUnit.ConsoleRunner.NetCore I'd like to ask for your comments on #1045

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants