[Route("/hello")]
public class Hello
{
}
public class HelloService : Service
{
public async Task<object> Any(Hello request)
{
await Task.Delay(10);
throw new NotImplementedException("async");
}
}
// In the AppHost I have this exception handler
ServiceExceptionHandlers.Add((req, request, exception) =>
{
Debug.WriteLine($"ServiceExceptionHandlers - HttpContext is null: {System.Web.HttpContext.Current == null}");
return DtoUtils.CreateErrorResponse(req, exception);
});
I’m expecting the HttpContext to be available in the error handler like it would be if it had been synchronous service, but it’s not. ResponseFilters are affected as well for async services.
The task is awaited with .ContinueWith() wich doesn’t capture the syncronization context by default, so when the continuation is executed the SynchronizationContext.Current is null when it should be AspNetSynchronizationContext.
If TaskScheduler.FromCurrentSynchronizationContext() is passed to ContinueWith it works as expected.
I found one more spot where the continuations aren’t executed in the corret context. When validation rules are executed asynchronously and they fail, the continuation doesn’t capture the context and when the ServiceExceptionHandler is called the HttpContext is null as well.
It doesn’t use the AsyncContext you mentioned above, so it’s not possible to override the ContinueWith behavior.