Threading Task Canceled Midway Exception

  • Thread starter Thread starter Diana4
  • Start date Start date
D

Diana4

Guest
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

Continue reading...
 
Back
Top