Ronald Rozema
Ronald Rozema

Reputation: 246

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?

Upvotes: 3

Views: 673

Answers (1)

NightShovel
NightShovel

Reputation: 3252

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).

Upvotes: 3

Related Questions