3

I'm trying to use the HashLib library in a Windows 10 app, but it throws an unhandled exception (System.MethodAccessException):

Attempt by method 'HashLib.Hash.TransformStream(System.IO.Stream, Int64)' to access method 'System.Collections.Concurrent.ConcurrentQueue`1..ctor()' failed.

with no further information. The exact line that throws the exception is in the HashLib's source file named Hash.cs at line 380:

System.Collections.Concurrent.ConcurrentQueue<byte[]> queue = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();

I can't find any clue regarding this issue on MSDN. I just saw that it is supported even in a portable class library so I would think it should work in a normal Windows 10 app, too. The exact same code was successfully used and tested within a WPF application and a Windows 8.1 app without any problems.

markus
  • 177
  • 1
  • 13

1 Answers1

0

The workaround is to convert the stream to byte[], it solves the problem.

    public static string MakeHashForFile(Stream fileStream)
    {
        //HashResult hashResult = hashImplementation.ComputeStream(fileStream);
        byte[] bytes = GetBytesFromStream(fileStream);

        HashResult hashResult = hashImplementation.ComputeBytes(bytes);

        return hashResult.ToString().Replace("-", String.Empty).ToLowerInvariant();
    }

    private static byte[] GetBytesFromStream(Stream stream)
    {
        byte[] result;
        using (MemoryStream reader = new MemoryStream())
        {
            stream.CopyTo(reader);
            result = reader.ToArray();
        }
        return result;
    }
iwanlenin
  • 210
  • 4
  • 11