1

I have a windows service application which sends some message to another webservice (which intern are sent out as email) and downloads response from the server.

Recently, service is getting stopped once in couple of days. (Eventough exception handlings are included still its not getting caught and service is crashing).

Exception getting logged in Event viewer is as below:

Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.Net.WebException Stack: at System.Net.ServicePointManager.FindServicePoint(System.Uri, System.Net.IWebProxy, System.Net.ProxyChain ByRef, System.Net.HttpAbortDelegate ByRef, Int32 ByRef) at System.Net.HttpWebRequest.FindServicePoint(Boolean) at System.Net.HttpWebRequest.get_ServicePoint() at System.Net.AuthenticationState.PrepareState(System.Net.HttpWebRequest) at System.Net.AuthenticationState.ClearSession(System.Net.HttpWebRequest) at System.Net.HttpWebRequest.ClearAuthenticatedConnectionResources()
at System.Net.HttpWebRequest.Abort(System.Exception, Int32) at System.Net.HttpWebRequest.AbortWrapper(System.Object) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

I have searched a lot but could not find the proper answer. Any help would be appreciated. Code used :

public class DataAPIWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest w = base.GetWebRequest(uri);
        w.Timeout = 60000;
        return w;
    }
}

public string Send(string path)
{
    try
    {
        using (DataAPIWebClient webClient = new DataAPIWebClient())
        {
            webClient.Credentials = new NetworkCredential("XXX", "YYY");
            string reply = webClient.DownloadString(path);
            return reply;
        }
    }
    catch (WebException)
    {
        throw;
    }
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Pramod Ray
  • 11
  • 2

1 Answers1

4

This is your problem

catch (WebException)
{
    throw;
}

You are just rethrowing your exception and it never gets caught, you never know what the error is, so the service has no option but to crash

May i suggest

catch (WebException ex)
{
    Log.Error(ex);
    // Or write it to a file or something
}

Edit : As noted in the comments, this is not the solution as such, however you are missing the actual exception message in that stack trace due to the uncaught exception, however its the uncaught exception that is crashing your service

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks a lot for the quick response. The "Send" method is being called from different methods and all other method codes are wrapped around Try, catch block also the exceptions are being logged to Textfile as well as database. Still the exception related to ServicePointManager.FindServicePoint is not being caught. – Pramod Ray Dec 26 '17 at 12:08