locked
How to return 202 status code async from C# RRS feed

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.

    • Try running Windows Network Diagnostics.

    Friday, May 10, 2019 4:25 AM
  • User-1188570427 posted

    This site can’t be reached

    http’s server IP address could not be found.

    • Try running Windows Network Diagnostics.

    This was an issue with the URL I was copying into the web address.

    Still testing now.

    Friday, May 10, 2019 4:43 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.

    • Try running Windows Network Diagnostics.

    THIS FIXED MY ISSUE!

    I can now run a long running task via schedule job in Azure!

    Friday, May 10, 2019 5:25 AM