Internet Information Service (IIS)

IIS

What is IIS?

IIS (Internet Information Server) is one of most powerful web servers from Microsoft that is used to host your ASP.NET Web application using HTTP (HyperText Transfer Protocol). IIS has its own ASP.NET Process Engine to handle the ASP.NET request. So, when a request comes from client to server, IIS takes that request, processes it and sends the response back to clients.

How to install IIS on your machine?

For Windows 7:
Check by typing inetmgr in Run Command. If it is installed then the IIS manager window will open else you get an error message. For installing, follow below given steps:
1)    Go to Start à Control Panel àPrograms àTurn Windows features on or off à A pop up comes
2)    Select Internet Information Service and the services required beneath it. à IIS gets installed.
3)    For enabling IIS, there are different options, check them in detail.
To Go to IIS, there are 2 ways:
1)    Start à Run àType “inetmgr” (instead of Start à Run, press Windows + R)
2)    Start à Control panel à System Security à Administrative Tools à Internet Information Service

What is Worker Process?

The Worker Process (w3wp.exe) runs the ASP.Net application in IIS. This process is responsible to manage all the request and response that are coming from client system.  All the ASP.Net functionality runs under the scope of worker process. 
An Internet Information Services (IIS) worker process is a windows process(w3wp.exe) which runs Web applications, and is responsible for handling requests sent to a Web Server for a specific application pool. To check if worker process is running or not: Right click on TaskBar à Start Task Manager à Processes à search for w as shown in below image


 One will come to know which application pools are active.

What is Virtual Directory?

It points to physical path and can browse application/webpages using IIS. We can create virtual directory from IIS (by right clicking on default website à Add Application à write the alias that you want for the application à select physical path à select Solution folder/Project folder path) or directly from ASP.net  website (Project properties à Web à Local IIS Web Server à write and alias name after localhost à click on Create Virtual Directory à it will be created in IIS. )

What is Application Pool?

Application pool is the container of worker process.  It is used to separate sets of IIS worker processes that shares the same configuration. The worker process serves as the process boundary that separates each application pool so that when one worker process or application is having an issue or recycles/restarts, other applications or worker processes are not affected (this may loose state information, if stored inside worker process).

IIS application pools also provide a bunch of advanced settings like what Windows user account it runs as, auto restarting of the process, auto shutdown, recycling etc. These settings impact the behavior IIS worker process. It is possible for an application pool to create multiple IIS worker processes which is called a web garden. Application pools enables a better security, reliability, and availability for any web application.

One application pool can contain one or more web applications. IIS can create one or more application pools. Applications in different application pools, run in their own Worker process.

How to create Application Pool?

Go to IIS, right click on Application Pools à Add Name, select .Net Framework and click on Ok.

Application Pool Identity:

Asp.net application executes inside asp.net worker process called w3wp.exe using windows identity which is dependent on application pool identity. Built in application pool identity are:

1)    LocalService
2)    LocalSystem
3)    NetworkService
4)    ApplicationPoolIdentity

By default, when a new application pool is created, it uses ApplicationPoolIdentity. We can also create custom identity by specifying username and password.



Associate Application Pool with Web Application:

Right click on the Virtual directory of web application à Manage Application à Advanced Settings à Select Application Pool

Get Application Pool Identity that is used:



Output: AppPool1 is the name of application pool newly created.

 Deny web application to use certain file using application pool:
Firstly, both should run in different application pools à Go to securities of file (right click à Properties à Security tab à Add à Paste account name (for example: IIS APPPOOL\AppPool1)) à Click on Check Names à Click on OK à In permission list, Deny all à Click on OK

What is managed pipeline mode in Application pool?

Each application pool uses one of two .NET integration modes for running ASP.NET applications: Integrated or Classic. The .NET integration mode defined for the application pool determines how IIS processes an incoming request to the sites, applications and Web services that run in that application pool.
·         Integrated mode allows IIS to process requests in the application pool by using the IIS 7 and later integrated pipeline. This allows ASP.NET modules to participate in IIS request processing regardless of the type of resource requested. Using integrated mode makes available features of the ASP.NET 2.0 request pipeline available to requests for static content, as well as ASP, PHP and other content types. By default, IIS 7 and later application pools run in this mode.

·         Classic mode uses the IIS 6.0 processing pipeline for hosting ASP.NET applications. In this mode, requests are processed initially through IIS 7 and later modules, and ASP.NET requests are further processed by the aspnet_isapi.dll. The ASP.NET processing pipeline is separate from the IIS 7 and later processing pipeline, and the ASP.NET request processing pipeline features are not available to other resource types. This also means that an ASP.NET request must pass through authentication and authorization modules in both process models. While this is not as efficient as Integrated mode, it does allow you to run applications developed using ASP.NET version 1.1 on an IIS 7 and later server without modifying the application to run in Integrated mode.

The <add> element of the <applicationPools> element was updated in IIS 7.5 to include attributes that allow you to preload applications by using the managedRuntimeLoader, CLRConfigFile, and startMode attributes, and to run applications using the new ApplicationPoolIdentity.

Save Application Pool Settings:


Application Pool Settings can be saved as "XML" Format. Right Click on Application Pool > All Task > Save Configuration to a File. This will save all the settings of Application Pool as an XML file. We can make it password protected also.


For anonymous authentication in IIS:










Click on Edit in Actions Pane: You will find IUSR in Specific User. One can write custom username if a PC uses 2 user accounts. One can also align that with an Application pool identity too. There are 2 ways to stop anonymous user to access an application: 1) Disabled from IIS, for this click on Disable button from Actions pane. 2) Go to web.config à under configuration à Authorization
 


Deny should be written and then allow users

HTTP Handler & HTTP Module:

Many times we need to inject some kind of logic before the page is requested. Such as URL rewriting, authentication / authorization which is doable through code behind too but it can create complications. So basically when we want some code/logic to execute before it reaches the resource. ASP.NET provides two ways of injecting logic in the request pipeline HttpHandlers and HttpModules.

HTTP Handler:
HttpHandlers are also called Extension based handlers. Such as .aspx, .ascx, .jpeg etc. So whenever user requests a file it is processed by the handler based on the extension. A Handler class must implement IHttpHandler interface which has 2 methods. This is more file type based. 
ProcessRequest():
Ex: public void ProcessRequest(System.Web.HttpContext context)
{
  context.Response.Write("The page request is " +    context.Request.RawUrl.ToString());
  StreamWriter sw = new StreamWriter(@"C:\requestLog.txt",true);
  sw.WriteLine("Page requested at " + DateTime.Now.ToString() +
  context.Request.RawUrl); sw.Close();
}
 IsReusable: True/false. It indicates whether this handler code to be reused for another  requests.

Set this in web.cofig
<system.web>
<httpHandlers>
<add verb="*" path="*.Shiv,*.Koirala" type="MyPipeLine.clsMyHandler, MyPipeLine"/>
</httpHandlers>
</system.web>

HTTP Module:
HttpModule is an event pre-processor. This is more event based. HttpModule class must implement IHttpModule interface and all its methods shown below, BeginRequest, AuthenticateRequest, AuthorizeRequest, ResolveRequestCache, AcquireRequestState & PreRequestHandlerExecute.
Ex:
public class clsMyModule : IHttpModule
{
public void Init(HttpApplication objApplication)
{
// Register event handler of the pipe line
objApplication.BeginRequest += new EventHandler(this.context_BeginRequest);
objApplication.EndRequest += new EventHandler(this.context_EndRequest);
}
public void Dispose()
{
}
public void context_BeginRequest(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(@"C:\requestLog.txt",true);
sw.WriteLine("Begin request called at " + DateTime.Now.ToString()); sw.Close();
}
}
Settings in Web.config
<httpModules>
<add name="clsMyModule" type="MyPipeLine.clsMyModule, MyPipeLine"/>
</httpModules>



CHECK:

IIS Lifecycle:

Step 1: The user sends a request to IIS. IIS first checks which ISAPI (Internet Server Application Programming Interface) extension can serve this request. For ex. if the page is an ‘.ASPX page’, then it will be passed to ‘aspnet_isapi.dll’ for processing.

Step 2: If this is the first request to the website, then
ApplicationManager class creates an application domain (App. Domain) where the website can run. App. Domain (example.com) creates isolation between two web applications hosted on the same IIS.

Step 3: The newly created application domain creates hosting environment, i.e. the
HttpRuntime object. Once the hosting environment is created, the necessary core ASP.NET objects like HttpContext, HttpRequest and HttpResponse objects are created.

Step 4: Once all the core ASP.NET objects are created, HttpApplication object is created to serve the request. In case you have a global.asax file in your system, then the object of the global.asax file will be created. Please note global.asax file inherits from HttpApplication class.
Note: 
HttpApplication instances might be reused for multiple requests to maximize Application performance.

Step 5: The 
HttpApplication object is then assigned to the core ASP.NET objects to process the page.

Step 6: 
HttpApplication then starts processing the request by HTTP module events, handlers and page events.





MHPM Events:
Step 1(M: HttpModule): Client request processing starts. Before the ASP.NET engine goes and creates the ASP.NET HttpModule emits events which can be used to inject customized logic..

Step 2 (H: ‘HttpHandler’): ASP.NET engine will invoke 
ProcessRequest event if you have implemented HttpHandler in your project.

Step 3 (P: ASP.NET page): Once the 
HttpHandler logic executes, the ASP.NET page object is created. While the ASP.NET page object is created, many events are fired like Init, Load, validate, event, render and unload.

Step4 (M: HttpModule): Once the page object is executed and unloaded from memory, HttpModule provides post page execution events which can be used to inject custom post-processing logic.

REFER: IIS videos kudvenkat – 9, 82,83,84,85




Comments

Popular posts from this blog

IIS Interview Questions: