Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Applications isolation using application pools in IIS - Part 83

Suggested Videos
Part 80 - Writing custom asp.net tracing messages
Part 81 - Tracing in asp.net - A real time example
Part 82 - Application pools in IIS

In this video we will discuss about achieving isolation between applications, by associating with different application pools. Create an asp.net web application with name WebApplication1. Drag and drop a button control on webform1.aspx. Copy and paste the following code in webform1.aspx.cs



protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Application1_Data"] = "Application 1 Data";
    }
    Response.Write("Identity used = " + System.Security.Principal.WindowsIdentity.GetCurrent().Name + "<br/>");
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["Application1_Data"] != null)
    {
        Response.Write("Application1_Data = " + Session["Application1_Data"]);
    }
    else
    {
        Response.Write("Session Data not available");
    }
}



protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Application2_Data"] = "Application 2 Data";
    }
    Response.Write("Identity used = " + System.Security.Principal.WindowsIdentity.GetCurrent().Name + "<br/>");
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["Application2_Data"] != null)
    {
        Response.Write("Application2_Data = " + Session["Application2_Data"]);
    }
    else
    {
        Response.Write("Session Data not available");
    }
}

Create an application pool in IIS with name "WebApplication1Pool".
1. Open IIS (Type INETMGR in RUN window, and press ENTER key)
2. Expand root node in IIS 
3. Right click on "Application Pools" and select Add "Application Pool"
4. Enter "WebApplicationPool1" as the name and click OK.

Associate WebApplication1, with "WebApplication1Pool" we just created.
1. In IIS, right click on "Default Web Site" and Select "Add Application"
2. Set Alias="WebApplication1" and Select "WebApplication1Pool" as the application pool.
3. Set the physical path to the directory of WebApplication1.

Along the same lines, associate WebApplication2, with "WebApplication1Pool".

At this point, if you run WebApplication1, by using CTRL+F5, visual studio by default uses built-in asp.net development server. Configure visual studio to use local IIS.
1. Right click on the "WebApplication1" project in solution explorer in visual studio and select "Properties"
2. In the properties window click on "Web" tab.
3. Under "Servers" section, Select "Use Local IIS Web Server" radio button
4. Set project Url=http://localhost/WebApplication1 and save the changes.

Do the same thing for WebApplication2, but set project Url=http://localhost/WebApplication2 and save the changes.

Run both the applications. When you click the button on both the applications, Session data should be available.

Now let us recycle, application pools in IIS.
1. Open IIS
2. Select Application Pools 
3. Right click on WebApplication1Pool, and select "Recycle"

Now click the button controls, on both the applications. Notice that both the applications have lost their session data. 

Now create a new application pool in IIS, with name WebApplication2Pool and associate WebApplication2, with this new pool. Run both the applications again. Click the button controls. Session data should be available. Recycle WebApplication1Pool. Click the button controls again on both the applications. Notice, that, only WebApplication1 has lost the session data but not WebApplication2. WebApplication2 belong to WebApplication2Pool. We have not recycled WebApplication2Pool, and hence it retains it's session data.

In the next video session, we will discuss about configuring different levels of security for different application pools.

No comments:

Post a Comment

It would be great if you can help share these free resources