StephenCleary / AsyncEx

A helper library for async/await.
MIT License
3.49k stars 358 forks source link

Could not load type 'Nito.AsyncEx.SynchronizationContextSwitcher' from assembly 'Nito.AsyncEx.Tasks, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null'.":"Nito.AsyncEx.SynchronizationContextSwitcher #261

Closed connorivy closed 2 years ago

connorivy commented 2 years ago

Hi, I am trying to use the Nuget package (version 5.1.2) but whenever I call "AsyncContext.Run(MainAsync);", I get the error in the title of this issue. Have you ever encountered this?

My AsyncContext function is very simple and is never being reached

static async Task MainAsync()
{
  await Task.Delay(1000);
}

I'm using .NET Framework 4.8

connorivy commented 2 years ago

Just checked the bin folder of my project and it looks like it contains the correct version of the Tasks.dll image

Is this an issue about the target framework being 4.6.1 when I'm actually using 4.8? I have a different project where this is working for v4.8

connorivy commented 2 years ago

Another update on this. I have one program where it is running fine and one where it is not. The structure of both programs is very similar, I can't find a difference that would cause the following difference in behavior. I cloned the repo to try to debug my way through it.

When the program is working correctly, it goes from breakpoint 1. image

to breakpoint 2. image

to breakpoint 3. image

and so on.

On my other program where it isn't working, it goes from breakpoint 1 directly to breakpoint 3. It then breaks out of the try statement that originally called the AsyncContext.Run(MainAsync) and the exception in the title shows up. It's strange because I'm passing in the same function to the same method in both programs, so you would think they would act the same way.

StephenCleary commented 2 years ago

This is almost certainly not a problem with the AsyncEx library. "Could not load type" problems are due to how the application has loaded its modules. In particular, it seems to be trying to use a very old version (1.0.1.0) instead of the one you think it is. You may want to check the Modules view in the debugger; it should tell you where on disk it's getting AsyncEx.Tasks 1.0.1.0 from.

connorivy commented 2 years ago

Yeah I figured it's a error with my setup but I can't for the life of me find the old package that it's referencing instead of the one in the project directory. I'm going to close this issue, as I've found an alternate solution to my problem and won't be trying to fix this issue. Thanks for your reply