C# – How to detect when the connection breaks when a web service is returning a result

c++connectiondetectexceptionweb-services

In a C# ASP .NET application I have a web service that receives a DataSet, processes it, and returns it.

Returning it can take up to a minute over a slow connection. What happens in the background if that connection breaks after the last line of the web service method has run but while the data is still being sent?

I tried wrapping my web service's code in a catch block and disconnecting the connection while it was downloading, but no exception was thrown in the web service. I'd to detect the error somehow like this:

[WebMethod(Description = "Process data")]
public DataSet Process(DataSet data)
{
   bool success = true;
   StartDatabaseTransaction();
   DataSet result = Process(data);
   try
   {
      return result; //quickly pull out cable after running this
   }
   catch
   {
      success = false;  //never gets here
   }
   finally 
   {
      if (success) CommitTransaction(); else RollBackTransaction();
   }
   return null;
}

Best Solution

If I read your question and code correctly, you will never hit the catch because of the way webmethods work. If you are looking to trap a timeout error it needs to be in the receiving method of the dataset. At the point of the return, it is the webserver that is working to transform and transmit the data.

Related Question