ASP.NET, SaveAsync() and HttpContext possible bug?

ASP.NET, SaveAsync() and HttpContext possible bug?

Old forum URL:

geordiepaul posted on Thursday, July 03, 2014

I raised a previous post about SaveAsync() not working. I boiled this down to a configuration setting..."CslaAutoCloneOnUpdate" if this is false then SaveAsync doesn't work. I have test cases to validate. I know this is a legacy setting but there are aspects of our code that require this until they can be fixed.

Secondly if calling SaveAsync() in an Web API controller in my test case. After the call HttpContext is null. Before the call it's fine. This pretty much breaks everything as the user has no identity.

I've had issues with threading before with CSLA background workers but never where HttpContext becomes null?

ajj3085 replied on Thursday, July 03, 2014

As a work around, can you use use the sync version of Save in your controller?


For your HttpContext becoming null, is that in your controller method or is it when you try to use HttpContext from your BO classes?  If its the latter, its because HttpContext is specific to the request thread I believe, and your BO is now on a different thread, so I wouldn't expect it to transition.  You'd have to set your async code to bring along the principal or do it manually.  I think there was a thread here about this recently.

geordiepaul replied on Monday, July 07, 2014

Yes, I should have realised that HttpContext would become null when the await returned.

Yes I can do sync save without problem. And I can do Async save if I remove the AutoCloneOnUpdate=false.

DrLik replied on Friday, July 04, 2014 - try set ASP.NET "task friendly synchronization context"

Copyright (c) Marimer LLC