tags:

views:

194

answers:

1

Have an ASP.net web app which works fine for a few days, but then randomly throws some database connection string exception and as a result 0 records are listed in a table (should show hundreds). I've spent many weeks debugging, memory is fine, the database exists and it's fixed by doing anything which would cause the application to recycle. It takes many days of waiting to even reproduce.

So I was thinking since I know there should never be 0 records, how can I force the application pool running the web app to recycle (when I get this database exception or 0 records). At least this way the web site will work for the next user and I don't have to manually restart it.

A: 

Hi in this article you can find relevant code to restart application pool from Asp.net

Restart IIS application pool from ASP.NET page

using System;
using System.Web;
using System.Web.UI;
using System.Management;
using System.DirectoryServices;
using System.Web.UI.WebControls;

public partial class iis : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(System.Environment.MachineName);
        status();
    }

    protected void status()
    {
        string appPoolName = "dev.somesite.com";
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        int intStatus = 0;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            intStatus = (int)w3svc.InvokeGet("AppPoolState");
            switch (intStatus)
            {
                case 2:
                    lblStatus.Text = "Running";
                    break;
                case 4:
                    lblStatus.Text = "Stopped";
                    break;
                default:
                    lblStatus.Text = "Unknown";
                    break;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    protected void stopAppPool(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string appPoolName = btn.CommandArgument;
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            w3svc.Invoke("Stop", null);
            status();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    protected void startAppPool(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string appPoolName = btn.CommandArgument;
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            w3svc.Invoke("Start", null);
            status();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}
Braveyard
does this work if called by the same app pool I wish to recycle?
Tuviah
@Tuviah : Could you please try the code out, it should do the same thing to the same app pool you are running on.
Braveyard
@P.s : If it is not working in the same app pool, you can create a page running under different application pool and run that page to recycle the application pool you want.It becomes like a separate worker to handle your works.
Braveyard