public async Task <ActionResult> Fire_and_forget() { ViewBag.ExampleType = "Asynchronous examples - set off an 11-second call in a different context."; var asyncExamples = new AsyncExamples(); // We will leave the request context AND we won't await the result. // This means the code will keep running even after we return a response to the browser. asyncExamples.Leave_The_Request_Context_In_All_Places(); return(View("AsyncExamples", 10)); }
public async Task <ActionResult> Leave_The_Request_Context_In_All_Places() { ViewBag.ExampleType = "Asynchronous examples - Use ConfigureAwait(false) to leave the request context in all places"; var asyncExamples = new AsyncExamples(); // At this point the current http context and sychronization contexts are both populated. ContextHelper.OutputRequestAndSynchronizationContexts("Before ConfigureAwait(false): "); // In Leave_The_Request_Context_In_All_Places, we will also leave the request context var result = await asyncExamples.Leave_The_Request_Context_In_All_Places().ConfigureAwait(false); // We have now left the main request context (because we use ConfigureAwait(false) in the line above). // At this point the current http context and sychronization contexts are no longer populated. ContextHelper.OutputRequestAndSynchronizationContexts("After ConfigureAwait(false): "); // Note that even though we are no longer in the request context, the base controller class has kept // track of the response in its Response property, so we can still access the response in this class. Response.StatusCode = (int)HttpStatusCode.BadRequest; return(View("AsyncExamples", result)); }