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