How to restart IIS using c#

I had a requirement whereby I needed to test the availability of XML Web service and IIS restart was part of the requirement as well.

Below is the code for restarting IIS using c#.

using System.ServiceProcess;

using (ServiceController controller = new ServiceController())
{
controller.MachineName = “My local or remote computer name”;
controller.ServiceName = “IIS Service Name”; // i.e “w3svc”

if (controller.Status != ServiceControllerStatus.Running)
{
// Start the service
controller.Start();

Log.Debug(“IIS has been started successfully, now checking again for webservice availability”);

}
else
{
// Stop the service
controller.Stop();

// Start the service
controller.Start();

Log.Debug(“IIS has been restarted successfully”);

}

}

Posted in Uncategorized | Leave a comment

Automatically logout users

There was a requirement at work that users must be logged out automatically if the system remain idle for more than 5 minutes.

To solve this, I used JavaScript. See code below

// Declare a global variable
var timer;

// Method to set the Interval
function set_interval() {
timer = setInterval(“auto_logout()”, );
}

function reset_interval() {
clearInterval(timer);
timer = setInterval(“auto_logout()”, );
}

function auto_logout() {
window.location = “/Login/SignOut”;
}

To get this code working you will need to call the method set_interval inside the html body tag, see below code :

onmousemove=”reset_interval()” onscroll=”reset_interval()” onclick=”reset_interval()” onkeypress=”reset_interval()”

I hope this will help someone out there!

Posted in Uncategorized | Leave a comment