2

I am using the MVC5 for an web application. The web app runs in IIS7 or greater.

In the Global.asax on application_start, the number of licenses will be set:

protected void Application_Start()
{
    try
    {
        MyApp.cNumberOfLicenses = COM.GetNumberOfLicenses();
    }
    catch(Exception e)
    {
        // log exception
        // stop web site.
    }
}

If any expection will be thrown in this context, the web site should shut down as you can do that in the IIS-Manager:

enter image description here

How can I stop the current web site in my Application_Start ?

Simon
  • 4,157
  • 2
  • 46
  • 87
  • 1
    I don't know what the people who marked this question as duplicate of [How can I programmatically stop or start a website in IIS (6.0 and 7.0) using MsBuild?](https://stackoverflow.com/questions/4958799/how-can-i-programmatically-stop-or-start-a-website-in-iis-6-0-and-7-0-using-ms) were smoking at the time, but this question doesn't even remotely match, aside from a single answer, which only works if the site is running with Administrator privileges. – Tom Lint Jul 13 '15 at 12:11

3 Answers3

1

You can do it with the help of "Microsoft.Web.Administration.dll"

using Microsoft.Web.Administration;

After adding the reference of "Microsoft.Web.Administration.dll" write below code in Global.asax

  protected void Application_Start(object sender, EventArgs e)
        {
            try
            {
                MyApp.cNumberOfLicenses = COM.GetNumberOfLicenses();
            }
            catch (Exception e)
            {
                // get the web site name
                var lWebSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();

                // log exception
                // stop web site.
                using (ServerManager smg = new ServerManager())
                {
                    var site = smg.Sites.FirstOrDefault(s => s.Name == lWebSiteName);
                    if (site != null)
                    {
                        //stop the site...
                        site.Stop();
                    }
                }
            }
        }
Simon
  • 4,157
  • 2
  • 46
  • 87
Deepak Joshi
  • 1,036
  • 7
  • 17
1

I will go not with stop it, but to show some message if you do not have license.

This is an example, and an idea.

You can use this code on global.asax where if you do not have licenses the moment you start, you open a flag, and after that you do not allow any page to show, and you send a page that you can keep on an html file.

private static bool fGotLicense = true;

protected void Application_Start()
{
    try
    {
        MyApp.cNumberOfLicenses = COM.GetNumberOfLicenses();
    }
    catch(Exception e)
    {
        // log exception
        // stop web site.
        fGotLicense = false;
    }
}

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;

    // if not have license - let show some infos
    if (!fGotLicens)
    {
        // the file we look now is the app_offline_alt.htm
        string cOffLineFile = HttpRuntime.AppDomainAppPath + "app_offline_alt.htm";

        // if exist on root
        if (System.IO.File.Exists(cOffLineFile))
        {
            using (var fp = System.IO.File.OpenText(cOffLineFile))
            {
                // read it and send it to the browser
                app.Response.Write(fp.ReadToEnd());
                fp.Close();
            }    
        }

       // and stop the rest of processing
       app.Response.End();
       return;
    }
}
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • 1
    This. In order to stop a site, you need to be administrator. So to stop a site from within the site itself, the application pool must run under an administrative user. You do not want that, not to mention a site taking itself offline directly after starting is confusing for the administrator and gives a bad user experience. – CodeCaster Apr 24 '15 at 10:05
0

You can have a file named app_offline.htm with content say This website is offline now in web server and copy that to root directoy of website you want for any event.

It will directly show that message, but yes, App pool will be still ON, when you need to start , you just need to rename that to something else.

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48