2

I'm unit testing a piece of code that uses a nested using statement. I've changed it to a using statement in a try/finally block. When I try to call the Dispose method in the finally block I get an EntryPointNotFoundException. I've tried a lot of things but I'm not sure how to solve this problem. Here is my code:

var memoryStream = new MemoryStream(message.FileContent);

try
{
    using (var sftpClient = this.GetSftpClientFromId(message.CustomerId))
    {
        return sftpClient.UploadFileAsync(memoryStream, message.FileName, true);
    }
}
finally
{
    memoryStream?.Dispose();
}

How can I solve this issue?

4

1 回答 1

1

刚发生这种情况。

问题最终是:

精简版:

程序集引用了在未来版本中实现 IDisposable 的对象,但在运行时加载了旧版本。因此,当它尝试调用旧版本中不存在的 Dispose() 时,它会出现 ummmmm EntryPointNotFoundException!

长版:

  • 在 ThirdPartyComponent 的版本 1 中,Thing没有实现 IDisposable。
  • 在 ThirdPartyComponent 的第 2 版中,Thing确实实现了 IDisposable。
  • ProjectA 是参考 ThirdPartyComponent 的第 2 版构建的。IDisposable 是有效的,并且“使用”编译得很好。
  • ProjectA 加载了 ThirdPartyComponent 的版本 1,并尝试调用“Dispose()”。它吓坏了,因为版本 1 中没有“Dispose()”。当然,它应该加载版本 2,但有时世界并不公平(在我的情况下,自定义程序集加载器搞​​砸了)。
于 2020-10-20T21:33:58.920 回答