Answered by:
How to return 202 status code async from C#

Question
-
User-1188570427 posted
I need to run an async status call to start exeuction, but then return http code 202 so the scheduled task knows that it is working in the background.
How would I do that?
This post is what is generating the question:
Thursday, May 9, 2019 10:32 PM
Answers
-
User-1174608757 posted
Hi tvb2727,
According to your description, to accomplish your requirement,you could firstly send the request for checking the status of async task , then if it is in processing , you could change the status code as 202 so that scheduled task would know that is working in background.
Here is the demo , I hope it could help you.
public partial class excel : System.Web.UI.Page { private Task<int> DoWorkAsync() // No async because the method does not need await { return Task.Run(() => { return 1 + 2; }); } protected void Page_Load(object sender, EventArgs e) { Window_PreviewKeyDown(); } private void Window_PreviewKeyDown() { //check goes here - abort if running if (DoWorkAsync() != null && !DoWorkAsync().IsCompleted) { // Your code here -- change the status as 202 Response.StatusCode = 202; } } }
You could see :
Best Regards
Wei
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 10, 2019 3:30 AM
All replies
-
User-1174608757 posted
Hi tvb2727,
According to your description, to accomplish your requirement,you could firstly send the request for checking the status of async task , then if it is in processing , you could change the status code as 202 so that scheduled task would know that is working in background.
Here is the demo , I hope it could help you.
public partial class excel : System.Web.UI.Page { private Task<int> DoWorkAsync() // No async because the method does not need await { return Task.Run(() => { return 1 + 2; }); } protected void Page_Load(object sender, EventArgs e) { Window_PreviewKeyDown(); } private void Window_PreviewKeyDown() { //check goes here - abort if running if (DoWorkAsync() != null && !DoWorkAsync().IsCompleted) { // Your code here -- change the status as 202 Response.StatusCode = 202; } } }
You could see :
Best Regards
Wei
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 10, 2019 3:30 AM -
User-1188570427 posted
Wei Zhang
Hi tvb2727,
According to your description, to accomplish your requirement,you could firstly send the request for checking the status of async task , then if it is in processing , you could change the status code as 202 so that scheduled task would know that is working in background.
Here is the demo , I hope it could help you.
public partial class excel : System.Web.UI.Page { private Task<int> DoWorkAsync() // No async because the method does not need await { return Task.Run(() => { return 1 + 2; }); } protected void Page_Load(object sender, EventArgs e) { Window_PreviewKeyDown(); } private void Window_PreviewKeyDown() { //check goes here - abort if running if (DoWorkAsync() != null && !DoWorkAsync().IsCompleted) { // Your code here -- change the status as 202 Response.StatusCode = 202; } } }
You could see :
Best Regards
Wei
I did what you said and I get this error when I call the page:
This site can’t be reached
http’s server IP address could not be found.
Friday, May 10, 2019 4:25 AM -
-
User-1188570427 posted
Wei Zhang
Hi tvb2727,
According to your description, to accomplish your requirement,you could firstly send the request for checking the status of async task , then if it is in processing , you could change the status code as 202 so that scheduled task would know that is working in background.
Here is the demo , I hope it could help you.
public partial class excel : System.Web.UI.Page { private Task<int> DoWorkAsync() // No async because the method does not need await { return Task.Run(() => { return 1 + 2; }); } protected void Page_Load(object sender, EventArgs e) { Window_PreviewKeyDown(); } private void Window_PreviewKeyDown() { //check goes here - abort if running if (DoWorkAsync() != null && !DoWorkAsync().IsCompleted) { // Your code here -- change the status as 202 Response.StatusCode = 202; } } }
You could see :
Best Regards
Wei
I did what you said and I get this error when I call the page:
This site can’t be reached
http’s server IP address could not be found.
THIS FIXED MY ISSUE!
I can now run a long running task via schedule job in Azure!
Friday, May 10, 2019 5:25 AM