![]() If you are debugging or logging the type of SynchronizationContext and you find out it's of type Legacy, you may want to check for a setting at the IIS or hosting Site level. If you don't explicitly include this setting but rely on to configure this setting - it will be overridden by any settings in IIS. I wanted to comment and say that the previous answers hit the nail on the head from a web.config perspective, however, there can be inherited settings from IIS that can override this functionality if you simply use. Hope this answer provides some more information about the issue. I spent a lot of time trying to find the source of the issue and Stephen´s response helped a lot. If you go to the source code ( ) you will see that the Legacy implementation of this interface lacks of asynchronous support. Instead of LegazyAspNetSynchronizationContext (Which was what I saw before adding the target framework): You should see this (AspNetSynchronizationContext): LDAP directory entries may have optional attributes RDBMS rows can have NULL column values. ![]() If you have doubts about the framework version you are running this may help:Īdd the following line on any of your Web API methods and set a breakpoint to verify what type is currently loaded at runtime and verify it is not a Legacy implementation: Why use Null Behavior Different systems handle null differently. In my case, our Web API was migrated to version 4.6.2 but the runtime target framework was never specified in the web config, so basically this was missing inside the tag: As Stephen pointed out not setting explicitly the target framework can generate this issue. TL DR: Is HttpContext.Current potentially null after an await? So what's true? Should it resolve? I think no, but I want an authoritative answer on this because async and await is new enough that I can't find anything definitive. There's even another comment on this question indicating the same. In comments in another question I was told that HttpContext.Current should resolve after an await. This is just a plain, naive use of await. Now, based on the synchronization context, it could actually be forced to go back to the same thread after the await but I'm not doing anything fancy in my test. I had hereto believed that the second exception is expected because when the await completes, it will likely be on a different thread where HttpContext.Current as a thread-static variable will no longer resolve to the appropriate value. Throw new ArgumentException("HttpContext.Current is null") Īwait Task.Run(() => ) I have the following test WebAPI code, I don't use WebAPI in production but I made this because of a discussion I had on this question: WebAPI Async questionĪnyways, here's the offending WebAPI method: public async Task Get(int id)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |