0

I have an asp.net webapi which sometimes crashes, or it does not give a response. I am trying to solve this problem. However, I temporarily reset the IIS until the problem is solved.

My IIS reset console application;

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Thread.Sleep(500);
            if (CheckConnection() == false)
            {
                System.Diagnostics.Process.Start(@"C:\Windows\System32\iisreset.exe");
                Console.WriteLine("Restarting");
                Thread.Sleep(30000);
            }
        }
    }

    public static bool CheckConnection(string webApiAddress = null)
    {
        try
        {
            using (var client = new WebClient())
            {
                using (var stream = client.OpenRead("http://localhost:8885/"))
                {
                    return true;
                }
            }
        }
        catch
        {
            return false;
        }
    }
}

I check per 500 milliseconds. Sometimes my reset application, resets the IIS, probably my asp.net web api crashing. But I am not sure about WebClient.OpenRead() method is best check method.

Thank you for best practice

Mehmet Topçu
  • 1
  • 1
  • 16
  • 31
  • This [post](https://stackoverflow.com/a/14906186/1050927) may help – Prisoner Oct 30 '17 at 08:54
  • 1
    2 requests per second? Not too much? :) if (CheckConnection() == false) can be rewritten like if (!CheckConnection()) but this doesn't matter... I think you should log the exception (in catch) to find out the problem and look in IIS logs/Event manager. And also maybe application pool restart is faster? Like here... https://stackoverflow.com/questions/249927/restarting-recycling-an-application-pool – Pavel Biryukov Oct 30 '17 at 10:14
  • 1
    you should probably check window's EVENT VIEWER for general info regarding why its crashing and you need to fix why it crashed – Sandip Bantawa Oct 30 '17 at 10:50

0 Answers0