2

I'm having the same issue described in this question where I am losing HttpContext.Current after an async operation. The main difference between that question and mine, is that I've applied all of the possible solutions to resolve it - which all work on my PC but don't work when we deploy to the webserver, which has .NET 4.8 installed. Another thing to note is that when we apply the settings - we can see that the SynchronizationContext is of type AspNetSynchronizationContext (and not Legacy, so we seem to have what we want).

We checked IIS App Settings and made sure there were no manually added or negating settings (like <add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" /> or something similar) and there weren't. We also tried <httpRuntime targetFramework="4.5" /> - which did not work.

Are there any other settings/configurations/possible .NET CLR explanations that would make this setting: <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> not pass the HttpContext.Current to a thread while performing async operations?

Also - if I have <httpRuntime targetFramework="4.5" /> - and I see a LegacyAspNetSynchronizationContext - what could be the root cause?

Example (locally) that works, but breaks on the webserver:

[HttpGet]
[Route("api/DelayTest")]
public async Task<int> Test()
{
    var ctx = System.Web.HttpContext.Current; // not null
    await Task.Delay(new TimeSpan(0, 0, 5));
    ctx = HttpContext.Current; //null here (which causes issues)
        
    return 0;
}

Note: We are targeting .NET 4.5:

<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxQueryStringLength="6000" />
Mark C.
  • 6,332
  • 4
  • 35
  • 71
  • If you have `AspNetSynchronizationContext` and not `LegacyAspNetSynchronizationContext`, then the `targetFramework`/`UseTaskFriendlySynchronizationContext` settings are correct. Are you *absolutely* sure the server is running this with .NET 4.8? – Stephen Cleary Apr 19 '21 at 20:01
  • @StephenCleary We did validate on the webserver registry that the value `528040` was returned, hinting that .NET 4.8 is installed. – Mark C. Apr 19 '21 at 20:07
  • Is there a reason you don't update the targetFramework to 4.8? It's been a pretty easy update in my experience. – mason Apr 19 '21 at 20:44
  • @mason this is actually a major application at my workplace that impacts every user's experience. We've tried to tread as lightly as possible at this point, but if you think it could be a potential solution, I don't see why we couldn't *try* it – Mark C. Apr 19 '21 at 20:56
  • Definitely should try it. It *may* fix your problem (I wasn't aware of anything special regarding the synchronization context or differences in tasks between 4.5 and 4.8 so ymmv) but at least you'll be up on the latest stable version, which is always a good place to be. – mason Apr 19 '21 at 20:59
  • I haven't tried setting the runtime to 4.8 **yet**, but I updated my question to also find out what could be responsible for adding `` and seeing a **Legacy** synchronization context. – Mark C. Apr 20 '21 at 17:50

0 Answers0