programatically recycle pool from asp.net application in same pool

前端 未结 2 1052
闹比i
闹比i 2021-02-04 21:37

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 (shou

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 22:07

    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());
            }
        }
    }
    

提交回复
热议问题