简体   繁体   中英

Entry point was not found on calling Dispose

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?

Just had this happen.

The problem ended up being:

Short version:

An assembly had a reference to an object that implemented IDisposable in a future version, but an old version was loaded at runtime. So when it tried to call Dispose(), which didn't exist in the old version, it goes ummmmm EntryPointNotFoundException!

Long version:

  • In version 1 of ThirdPartyComponent, Thing did not implement IDisposable.
  • In version 2 of ThirdPartyComponent, Thing did implement IDisposable.
  • ProjectA was built referencing version 2 of ThirdPartyComponent. IDisposable is valid, and the "using" gets compiled just fine.
  • ProjectA loaded version 1 of ThirdPartyComponent, and tries to call "Dispose()". It freaks out because there is no "Dispose()" in version 1. Of course, it should have loaded version 2, but sometimes the world isn't fair (in my case, a custom assembly loader messed up).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM