Monday, November 28, 2011

How IIS Process ASP.NET Request

Note: Referenced from Abhijit Jana article

Introduction

When request come from client to the server a lot of operation is performed before sending response to the client. This is all about how IIS Process the request.  Here I am not going to describe the Page Life Cycle and there events, this article is all about the operation of IIS Level.  Before we start with the actual details, let’s start from the beginning so that each and every one understands its details easily.  Please provide your valuable feedback and suggestion to improve this article.

What is Web Server?

When we run our ASP.NET Web Application from visual studio IDE, VS Integrated ASP.NET Engine is responsible to execute all kind of asp.net requests and responses.  The process name is "WebDev.WebServer.Exe" which actually take care of all request and response of an web application which is running from Visual Studio IDE.
Now, the name “Web Server” come into picture when we want to host the application on a centralized location and wanted to access from many locations. Web server is responsible for handle all the requests that are coming from clients, process them and provide the responses.

What is IIS?

IIS (Internet Information Server) is one of the most powerful web servers from Microsoft that is used to host your ASP.NET Web application. IIS has it's own ASP.NET Process Engine  to handle the ASP.NET request. So, when a request comes from client to server, IIS takes that request and  process it and send response back to clients.



 

Request Processing:


Hope, till now it’s clear to you that what is Web server and IIS is and what is the use of them. Now let’s have a look how they do things internally. Before we move ahead, you have to know about two main concepts

1.    Worker Process
2.    Application Pool


Worker Process:  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.  When a request comes to the server from a client worker process is responsible to generate the request and response. In a single word we can say worker process is the heart of ASP.NET Web Application which runs on IIS.

Application Pool:  Application pool is the container of worker process.  Application pools is used to separate sets of IIS worker processes that share the same configuration.  Application pools enables a better security, reliability, and availability for any web application.  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, other applications or worker processes are not affected. This makes sure that a particular web application doesn't not impact other web application as they are configured into different application pools.
Application Pool with multiple worker process is called “Web Garden”.

Now, I have covered all the basic stuff like Web server, Application Pool, Worker process. Now let’s have looked how IIS process the request when a new request comes up from client.

If we look into the IIS 6.0 Architecture, we can divided them into Two Layer


1.    Kernel Mode
2.    User Mode

Now, Kernel mode is introduced with IIS 6.0, which contains the HTTP.SYS.  So whenever a request comes from Client to Server, it will hit HTTP.SYS First.


Now, HTTP.SYS is Responsible for pass the request to particular Application pool. Now here is one question, How HTTP.SYS comes to know where to send the request?  This is not a random pickup. Whenever we create a new Application Pool, the ID of the Application Pool is being generated and it’s registered with the HTTP.SYS. So whenever HTTP.SYS Received the request from any web application, it checks for the Application Pool and based on the application pool it send the request.

So, this was the first steps of IIS Request Processing.

Till now, Client Requested for some information and request came to the Kernel level of IIS
means at HTTP.SYS. HTTP.SYS has been identified the name of the application pool where to send. Now, let’s see how this request moves from HTTP.SYS to Application Pool.
In User Level of IIS, we have Web Admin Services (WAS) which takes the request from HTTP.SYS and pass it to the respective application pool.


When Application pool receive the request, it simply pass the request to worker process (w3wp.exe) . The worker process “w3wp.exe” looks up the URL of the request in order to load the correct ISAPI extension. ISAPI extensions are the IIS way to handle requests for different resources. Once ASP.NET is installed, it installs its own ISAPI extension (aspnet_isapi.dll) and adds the mapping into IIS.  

Note: Sometimes if we install IIS after installing asp.net, we need to register the extension with IIS using aspnet_regiis command.



When Worker process loads the aspnet_isapi.dll, it start an HTTPRuntime, which is the entry point of an application. HTTPRuntime is a class which calls the ProcessRequest method to start Processing.




When this methods called, a new instance of HTTPContext is been created.  Which is accessible using HTTPContext.Current Properties. This object still remains alive during life time of object request.  Using HttpContext.Current we can access some other objects like Request, Response, and Session etc.


After that HttpRuntime load an HttpApplication object with the help of  HttpApplicationFactory class.. Each and every request should pass through the corresponding HTTPModule to reach to HTTPHandler, this list of module are configured by the HTTPApplication.

Now, the concept comes called “HTTPPipeline”. It is called a pipeline because it contains a set of HttpModules (For Both Web.config and Machine.config level) that intercept the request on its way to the HttpHandler. HTTPModules are classes that have access to the incoming request. We can also create our own HTTPModule if we need to handle anything during upcoming request and response.


HTTP Handlers are the endpoints in the HTTP pipeline. All requests that are passing through the HTTPModule should reach to HTTPHandler.  Then HTTP Handler generates the output for the requested resource. So, when we requesting for any aspx web pages,   it returns the corresponding HTML output.
All the request now passes from  httpModule to  respective HTTPHandler then method and the ASP.NET Page life cycle starts.  This ends the IIS Request processing and start the ASP.NET Page Lifecycle.


Conclusion
When client request for some information from a web server, request first reaches to HTTP.SYS of IIS. HTTP.SYS then sends the request to respective Application Pool. Application Pool then forward the request to worker process to load the ISAPI Extension which will create an HTTPRuntime Object to Process the request via HTTPModule and HTTPHanlder. After that the ASP.NET Page LifeCycle events start.
This was just overview of IIS Request Processing to let Beginner’s know how the request get processed in backend.  If you want to learn in details please check the link for Reference and further Study section.

Tuesday, November 1, 2011

Interview Questions Silverlight


What is Moonlight?

Moonlight is an open source implementation of Silverlight, primarily for Linux and other Unix/X11 based operating systems. In September of 2007, Microsoft and Novell announced a technical collaboration that includes access to Microsoft's test suites for Silverlight and the distribution of a Media Pack for Linux users that will contain licensed media codecs for video and audio.

What is RIA service?

WCF RIA Services simplifies the development of n-tier solutions for Rich Internet Applications (RIA), such as Silverlight applications. A common problem when developing an n-tier RIA solution is coordinating application logic between the middle tier and the presentation tier. To create the best user experience, you want your RIA Services client to be aware of the application logic that resides on the server, but you do not want to develop and maintain the application logic on both the presentation tier and the middle tier. RIA Services solves this problem by providing framework components, tools, and services that make the application logic on the server available to the RIA Services client without requiring you to manually duplicate that programming logic. You can create a RIA Services client that is aware of business rules and know that the client is automatically updated with latest middle tier logic every time that the solution is re-compiled.

What is the difference between WCF and RIA service?

RIA Services gives you is code generation. It creates your classes for you. You only need to put the business logic. I think RIA Services is very useful for developing small to mid apps very rapidly.
Another difference is that it hides the asynchronous calls to the web service. Many people don't like the async model (which is the only one you can use in Silverlight) and a RIA service handles that for you.

How many types of binding are there in Silverlight?

There are three types of data binding that happens in Silverlight applications between a source and target.
1.       OneTime data binding
2.       OneWay data binding (default mode)
3.       TwoWay data binding
OneTime data binding: As the name suggests, data binding happens between the source and target only once. In this binding, the source (CRL object) data will bind with the target (XAML element) only when the page executes the first time. Later on, any changes made in the source will not be reflected back in the target element. You will prefer to use this binding mode to reduce overhead if you know that the source property won’t change.
OneWay data binding: In this data binding, the source will immediately inform about changes to the target control. We will be using this binding only for frequently changed elements. This is the default mode.
TwoWay data binding: This happens in bidirectional mode. Whenever any change occurs in the backend or the CRL object, it will inform to the XAML controls immediately, and vice versa.

 What is a storyboard?

Controls animations with a timeline, and provides object and property targeting information for its child animations.

What is the difference between Label and a text block controls?

Even though TextBlock lives in the System.Windows.Controls namespace, it is not a control.  It derives directly from FrameworkElement.  Label, on the other hand, derives from ContentControl.  This means that Label can:
1.       Be given a custom control template (via the Template property).
2.       Display data other than just a string (via the Content property).
3.       Apply a DataTemplate to its content (via the ContentTemplate property).
4.       Do whatever else a ContentControl can do that a FrameworkElement cannot.

What are Pixel Shedders?

A pixel shader is an object that transforms pixels output from the rendering pipeline before they're rendered to the display surface.

What is the difference between WPF and Silverlight?

WPF is for desktop applications- you create an .exe file when you build WPF applications. To run a WPF exe requires you to have Framework 3 or higher on your pc.
Silverlight is on the web. What you create is an application that can be hosted either in an html page or an asp.net page. To watch it from your browser it requires to have installed in your browser the silverlight plugin. Silverlight uses very fewer libraries than WPF does, and that's pretty logical because Silverlight’s CLR is in the plugin which is about 5-10 MB.
WPF and silverlight though use the same logic in development and the same technology.

Can WPF application runs in browser, if so then why do I go for Silverlight?

WPF applications can be deployed to the desktop or run in Internet Explorer (on Windows only, as far as I know). When WPF application run in Internet Explorer they run in a sandbox, so users simply point Internet Explorer at an URL and your application appears without any installation or confirmation need. All development tools are the same (Visual Studio) when making desktop and browser-based WPF applications and you can use the same widgets for both.
WPF running in Internet Explorer have some restrictions compared to a program running on the desktop. For example, opening new windows is not possible and communication (WCF) is not allowed. Apparently SOAP calls can be used instead.
Making a WPF application run in the browser is easy. You create a project in Visual Studio marking it as a “WPF Browser Application”. After compilation, you publish the executable to a web server. I think one idea in Visual Studio is to allow a desktop-based WPF application and the same browser-based application to be produced from the same codebase. I have not tried this yet, but this looks like a promising concept.
Like WPF-applications can run in a browser, so do Silverlight applications. But Silverlight applications can be deployed to more platforms (OS X and Linux) and more browsers. This does however come with a cost… Like browsers-based WPF applications lose access to some functions compared to desktop applications, Silverlight applications can build on even less infrastructure. The GUI is one (of many) missing elements in Silverlight

What us the difference between DataTemplate, ControlTemplate and HierarchicalDatatemplate?

Before we get into differences between them, let me show how one would typically use them. It’s a good practice that all your templates are embedded inside resources / styles. It makes your XAML centralized, shareable and easier to understand (a refactored XAML of sorts, waiting for ReSharper to include this refactoring.
Don’t Do:

[ListBox]
            [ListBox.ItemTemplate]
                [DataTemplate]
                    [!– Your Data Template Goes here–]
                [/DataTemplate]
            [/ListBox.ItemTemplate]
[/ListBox]
Recommended:
[Window.Resources]
        [DataTemplate x:Key=”myDataTemplate”]
            [!– Your Data Template Goes here–]          
        [/DataTemplate]
[/Window.Resources]
[ListBox ItemTemplate=”{StaticResource myDataTemplate}” /]
The above XAML holds true also for HierarchicalDataTemplate & ItemsPanelTemplate. But it won’t quite work for ControlTemplate, as ControlTemplate can be assigned to Template property of a class that inherits from ContentControl class, which ListBox doesn’t.
So the listbox code would typically look like this:

[ControlTemplate x:Key=”myControlTemplate” TargetType=”{x:Type ListBoxItem}”]
            [TextBox /]
[/ControlTemplate]
[ListBox]
            [ListBoxItem Template=”{StaticResource myControlTemplate}” /]          
[/ListBox]
But the problem with above snippet is, you are manually specifying item which is never the case in real world. Ideally you would like to bind ItemsSource Property of ListBox with some collection obtained at runtime from your data access layer. Solution to this problem is creating a style and binding it to ItemContainerStyle property of ListBox. By doing that you will be able to use ItemsSource for Binding & the ControlTemplate which is present inside the style will be applied to each binded item.

[ListBox x:Name=”myList” ItemsSource=”{Binding}” ItemContainerStyle=”{StaticResource myStyle}” /]
[Style x:Key=”myStyle” TargetType=”{x:Type ListBoxItem}”]
            [Setter Property=”Template”]
                [Setter.Value]
                    [ControlTemplate TargetType=”{x:Type ListBoxItem}”]
                        [TextBox Text=”{Binding collectionPropertyName}” /]
                    [/ControlTemplate]
                [/Setter.Value]
            [/Setter]
[/Style]

ItemsPanelTemplate is mainly used by controls inheriting from ItemsControl class for displaying their Items. ItemsPanelTemplate can be customized through ItemsPanel property of ItemsControl class or any class which inherits from ItemsControl. 

[ListBox x:Name=”myList” ItemsPanel=”{StaticResource myItemsPanelTemplate}” /]
[ItemsPanelTemplate x:Key=”myItemsPanelTemplate”]          
            [StackPanel Orientation=”Horizontal” /]
[/ItemsPanelTemplate]
 
The point of confusion is normally between selecting a ControlTemplate or DataTemplate. Normal saying goes as DataTemplate provides visualization to a business object while ControlTemplate does the same to a UI control. But in my project I see ControlTemplate used almost everywhere. Normally UI guys start creating screens while we are still giving shapes to our domain model (deadlines issues you see). Moreover, I feel one has to get slightly deep with WPF control properties to create an appealing professional UI.  So atleast here, we developers don’t quite mind giving a free hand to UI guys. We just go over to control templates residing inside styles & giving names of Business Object properties inside the bindings created by them. Also ControlTemplate triggers are based on UI control properties which is what UI team is often interested in. Few more differences are given here.
HierarchicalDataTemplate is an extension of DataTemplate with an additional ItemSource property. It is used to render hierarchical data in controls like treeview or menu. HierarchicalDataTemplate ends with a Data Template.

[HierarchicalDataTemplate DataType=”{x:Type Department}” ItemsSource=”{Binding Path=Classes}”]
            [TextBlock Text=”{Binding Path=DepartmentName}” /]
[/HierarchicalDataTemplate]
[DataTemplate DataType=”{x:Type UniversityClass}”]
            [TextBlock Text=”{Binding Path=ClassName}” /]
[/DataTemplate]

Attached property vs. Dependency property?

Attached properties are a type of dependency property. The difference is in how they're used.
With an attached property, the property is defined on a class that isn't the same class for which it's being used. This is usually used for layout. Good examples are Panel.ZIndex or Grid.Row - you apply this to a control (ie: Button), but it's actually defined in Panel or Grid. The property is "attached" to the button's instance.
This allows a container, for example, to create properties that can be used on any UIelement.
As for implementation differences - it's basically just a matter of using Register vs. RegisterAttached when you define the property.

Define Framework element?

UIElement is a base class for most of the objects that have visual appearance and can process basic input in Silverlight.
FrameworkElement provides a framework of common APIs for objects that participate in Silverlight layout. FrameworkElement also defines APIs related to data binding, object tree, and object lifetime feature areas in Silverlight.

What kind of elements used in storyboard?

From/To Animation: Animates between a starting and ending value. While creating the Animation, we will be having From and To properties to set the beginning and ending values.
Use the From property to set the starting value
Use the To property to set the ending value
These are simple to implement and these are basic animations. Animations belonging to this category are:
·         ColorAnimation
·         DoubleAnimation
·         PointAnimation
Key-frame Animation: Animates between a series of values specified using key-frame objects. Key-frame animations are more powerful than From/To animations because you can specify any number of target values and even control their interpolation method. This kind of Animation implementation is a bit complex than basic animations. Animations belonging to this category are:
·         ColorAnimationUsingKeyFrames
·         DoubleAnimationUsingKeyFrames
·         PointAnimationUsingKeyFrames

What is Downloader Object in Silverlight?

This object will retrieve objects (images, video, xaml) asynchronously, much like AJAX.  There is a gaping limitation being that it cannot go cross domain. 
An interesting feature of the Downloader object is the ability to get XAML asynchronously.  This gives us the ability to create module Silverlight applications and code XAML Plug-ins.
What is BAML? 
XAML stands for extended application markup language. It is nothing but an xml file which is used to declarative create the user interface of the silver light or the WPF applications. This XAML file generally rendered by the silverlight plugin and displayed inside the browser. When you compile your project which includes XAML pages,those first converts into BAML (Binary application markup language) and then rendered in the web browser.

What kind of Brushes you are used in Silver light project?

     a) Linear Gradient
     b) Solid color brush

What kind of audio video formats is supported in Silverlight?

Silverlight supports Windows Media Audio and Video (WMA, WMV7-9) and VC-1, as well as MP3 audio.

What is the XAP mime type in Silverlight?

The .xap mime type is: application/x-silverlight