none
Threading Task Canceled Midway Exception RRS feed

  • Question

  • Hi, I have developed a C# ASP.NET Web Application. In that application there is an .aspx page, in which in the Page_Load routine, I select data out of SQL Server 2012 database, check for certain alarm/warning signal, and if the signal is detected in the data, code is used to send out outlook email to email addresses, which are looked up the database again. Also updates are made to the SQL Server tables.

    I want to launch this .aspx web page from a c# console program using HttpClient(). The code is attached, shown below. When I execute this console program, after some time, I get an Unhandled exception of type 'System.Threading.Tasks.TaskCanceledException' occured in mscorlib.dll.

    But even after the exception is thrown, I can see that the task seems to keep on running in the background for several more minutes and I find that it goes to completion.

    The exception details and code are shown below. Can you please help me fix this ? Thanks

    Unhandled exception; System.Threading.Tasks.TaskCanceledException: A task was canceled.
    at System.Runtime.CompilerService.TaskAwaiter.ThrowForNonSuccess<Task task>
    at Syste.Runtime.CompilerService.TaskAwaiter.HandleNonSuccessAndDebuggerNotification<Task task>
    at ConsoleApplication2.Program.<DumDum>d__3.MoveNext,> in c....

    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.Net.Http;
    
    namespace ConsoleApplication2
    {
    	class Program
    	{
            static System.IO.Stream Response;
    		static void Main(string[] args)
    		{
    			DumDum().GetAwaiter().GetResult();
    		}
    
            static async Task DumDum()
            {
                // Call asynchronous network methods in a try/catch block to handle exceptions
                try
                {
                    HttpClient client = new HttpClient();
                    HttpResponseMessage response = await client.GetAsync("http://webserver_name/mywebpage.aspx");
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    // Above three lines can be replaced with new helper method below
                    // string responseBody = await client.GetStringAsync(uri);
    
                    Console.WriteLine(responseBody);
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                }
            }
    
    	}
    }


    diana4

    Saturday, July 6, 2019 11:20 PM

Answers

  • The obvious question is, are you sending a response from your .aspx page?  When you have a page doing a bunch of stuff in the background, it's not too difficult to forget to send a response.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Diana4 Wednesday, July 10, 2019 4:50 AM
    Sunday, July 7, 2019 4:19 AM
  • Hi Diana4,

    Thank you for posting here.

    >Can I do without sending a response from the .aspx page ? I really do not care about the response. I just want the .aspx page to do the background work. 

    Sure.  But we don’t know how to you call the aspx page to do background work, you should make sure your background work will continue work if it is fired.

    >Is there a timeout involved here too ? does the response need to be sent from the .aspx web page within a certain time period ? can that be extended ?

    Yes. If you want to modify it you could refer to below setting.

    <system.web>
        <httpRuntime executionTimeout="180" />
    </system.web>
    >Can i send the response after the background work has been completed ? if yes, can you please tell how ?

    It based on what kinds of background work you have used.

    I noted that you posted the same thread in sap.net forum. Therefore, If you want to follow up the case, you could ask it in that thread in asp.net forum.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 9, 2019 5:46 AM
    Moderator
  • Thank you for responding. I think I have a final solution, obtained by trying out stuff, and from insight gained by both your responses.
    What indeed appears to be happening is a timeout. The time out can be set by shown example statements below.
                    HttpClient client = new HttpClient();
                    client.Timeout = TimeSpan.FromMinutes(15);

    However, I could not get the following method of setting timeout in the previous response to work (when I use it in the web.config file)
    <system.web>
        <httpRuntime executionTimeout="180" />
    </system.web>

    Now the .aspx webpage has to return the response, by the time the timeout is reached. No new or extra code is needed to return a response. It happens by default. If the timeout is reached and webpage is still busy doing the background work, it won't be sending the response and then the exception occurs.

    If the timeout is set sufficiently large, then the background work will complete before timeout, after which the .aspx webpage will send the normal response and thus there will be no exception.


    diana4

    • Marked as answer by Diana4 Wednesday, July 10, 2019 4:50 AM
    Wednesday, July 10, 2019 4:49 AM

All replies

  • The obvious question is, are you sending a response from your .aspx page?  When you have a page doing a bunch of stuff in the background, it's not too difficult to forget to send a response.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Diana4 Wednesday, July 10, 2019 4:50 AM
    Sunday, July 7, 2019 4:19 AM
  • Can I do without sending a response from the .aspx page ? I really do not care about the response. I just want the .aspx page to do the background work. 

    Is there a timeout involved here too ? does the response need to be sent from the .aspx web page within a certain time period ? can that be extended ?

    Can i send the response after the background work has been completed ? if yes, can you please tell how ?

    Thank you for your help


    diana4

    • Marked as answer by Diana4 Wednesday, July 10, 2019 4:49 AM
    • Unmarked as answer by Diana4 Wednesday, July 10, 2019 4:49 AM
    Sunday, July 7, 2019 4:28 AM
  • Hi Diana4,

    Thank you for posting here.

    >Can I do without sending a response from the .aspx page ? I really do not care about the response. I just want the .aspx page to do the background work. 

    Sure.  But we don’t know how to you call the aspx page to do background work, you should make sure your background work will continue work if it is fired.

    >Is there a timeout involved here too ? does the response need to be sent from the .aspx web page within a certain time period ? can that be extended ?

    Yes. If you want to modify it you could refer to below setting.

    <system.web>
        <httpRuntime executionTimeout="180" />
    </system.web>
    >Can i send the response after the background work has been completed ? if yes, can you please tell how ?

    It based on what kinds of background work you have used.

    I noted that you posted the same thread in sap.net forum. Therefore, If you want to follow up the case, you could ask it in that thread in asp.net forum.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, July 9, 2019 5:46 AM
    Moderator
  • Thank you for responding. I think I have a final solution, obtained by trying out stuff, and from insight gained by both your responses.
    What indeed appears to be happening is a timeout. The time out can be set by shown example statements below.
                    HttpClient client = new HttpClient();
                    client.Timeout = TimeSpan.FromMinutes(15);

    However, I could not get the following method of setting timeout in the previous response to work (when I use it in the web.config file)
    <system.web>
        <httpRuntime executionTimeout="180" />
    </system.web>

    Now the .aspx webpage has to return the response, by the time the timeout is reached. No new or extra code is needed to return a response. It happens by default. If the timeout is reached and webpage is still busy doing the background work, it won't be sending the response and then the exception occurs.

    If the timeout is set sufficiently large, then the background work will complete before timeout, after which the .aspx webpage will send the normal response and thus there will be no exception.


    diana4

    • Marked as answer by Diana4 Wednesday, July 10, 2019 4:50 AM
    Wednesday, July 10, 2019 4:49 AM