Tuesday, January 21, 2014

How IIS process a request

Those who need basic understanding of IIS, please Click Here.
Depending upon the extension, IIS determines the handler. What if the url does not have any extension(case of MVC and webapi). Well in that case the old IIS(5.0,6.0) wont work as expected, but the 7.0 will work.

The difference between 6.0 and 7.0 is, 7.0 by default run in integrated mode. That is, it treats every request to be .NET request and hence it does not need any extension to identify, if it is .NET request or not.

The below diagram gives an overview of how request is handled in IIS.

The request from the browser once reaches to IIS, it resolve the extension(for old IIS) and pass it to the respective handler(aspnet_wp.exe in 5.0 and w3wp.exe in 6.0/7.0).

Once it reaches to these process, it creates object of ApplicationManager, which loads AppDomain(if not loaded) and pass the request to respective appdomain. Application manager is responsible for managing different appdomain in the process.

In the Appdomain, a hosting environment is created, which provides information(like folder where application is, applicationID, sitename etc) about application

Once hosting environment is created, it creates object of HttpContext, HttpRequest, HttpResponse.

After all these objects are created, application is started by creating object of HttpApplication(if global.asax is available, then object of global.asax created which inherits frm HttpApplication only). The HttpApplication will take care of performing ASP.NET life cycle. All the events raised in page life cycle is taken care by HttpApplication. (If it is global.asax, then the event in global file also taken care of.)

Ref -
http://msdn.microsoft.com/en-us/library/ms178473(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/bb470252(v=vs.100).aspx

Friday, January 10, 2014

Routing in IIS

I was going through the link below to understand the routing in IIS -
http://www.iis.net/learn/extensions/url-rewrite-module/iis-url-rewriting-and-aspnet-routing

I found it useful and have noted few important points which might summarize the whole article.
Here are those points -

IIS URL Rewriting
The URL rewriting module analyzes the requested URL and changes it to a different URL on the same server.The URL rewriting module runs early in the request-processing pipeline, modifying the requested URL before the Web server decides which handler to use to process the request.

ASP.NET Routing
Developers can associate a certain URL with a handler that can process requests made to that URL.
ASP.NET routing is implemented as a managed-code module that plugs into the IIS request-processing pipeline at the Resolve Cache stage (PostResolveRequestCache event) and at the Map Handler stage (PostMapRequestHandler event).During the PostResolveRequestCache event, the module looks through a routing table for a route that matches the requested URL path. If a match is found, the module obtains a reference to the handler that corresponds to that route and saves the reference as part of the current HTTP context. A handler can be any .NET Framework object that implements the System.Web.IHttpHandler interface.
During the PostMapRequestHandler event, the module checks if the HTTP context contains any information about a handler. If it does, ASP.NET routing uses the information to set the Handler property of the current HTTP context. This ensures that during the Execute Handler stage, IIS will execute the handler that was selected by the routing module.























Difference between URL Rewriting and ASP.NET Routing -
1) The IIS URL Rewrite module can be used with any type of Web application, which includes ASP.NET, PHP, ASP, and static files. ASP.NET routing can be used only with .NET Framework-based Web applications.
2) The IIS URL Rewrite module works the same way regardless of whether integrated or classic IIS pipeline mode is used for the application pool. For ASP.NET routing, it is preferable to use integrated pipeline mode. ASP.NET routing can work in classic mode, but in that case the application URLs must include file name extensions or the application must be configured to use "*" handler mapping in IIS.
3) The IIS URL Rewrite module can make rewriting decisions based on domain names, HTTP headers, and server variables. By default, ASP.NET routing works only with URL paths and with the HTTP-Method header.
4) In addition to rewriting, the URL Rewrite module can perform HTTP redirection, issue custom status codes, and abort requests. ASP.NET routing does not perform these tasks.

Friday, January 3, 2014

ASP .NET Web API Basics - part 1

REST - Representational State Transfer
REST suggest use of HTTP protocol(Get, Post etc) to do any operation on server. As per REST following is the protocol we should follow these conventions with HTTP verbs-
Get - Retrieve resource
Post - Create resource
Put - Update resource
Delete - Delete resource

Here are the url example which will help us understand how to achieve different server side operations based on HTTP verbs -

Get Request -
http://mydomain.com/api/books   -- retrieve all books
http://mydomain.com/api/books/1 -- retrieve book with id 1
Similarly we can utilize the POST, PUT and DELETE operations.

WebAPI -
It is possible to achieve REST full service architecture using WCF also, but it is not that straight forward, it requires a lot of configuration. Moreover TDD point of view, it is not easy to test it.

Microsoft came up with new solution which they call it as ASP.NET WEB API, which is kind of combination of both WCF WEB API and ASP.NET MVC(conceptual idea only).
It allow you to write REST based services.
Here is the architecture placement of WEBAPI(via Microsoft site) -











Creating project
This is how you can build a sample WEB-API app -
Creating project -> VS -> new project of type MVC4 -> Select Web API

WebAPI Vs MVC -
http://softtechhelp.blogspot.in/2013/11/webapi-vs-mvc.html

Controller
WebAPI controller inherits from Apicontroller.
The function name is directly mapped with the HTTP verb. Which means if i have a controller named as Person and have Get method in it, an HTTP get request to /person will execute this method. I can have overloaded methods to handle different get methods with arguments as well.
Example -
http://mydomain.com/api/books   -- retrieve all books
http://mydomain.com/api/books/1 -- retrieve book with id 1

Routing
Similar to MVC, WebAPI also has a routing table, which defines the URL structure. By default the url structure is /api/{controller}. You can always customize it.

Response format-
By default the response of get request is an XML. But it depends on what client is requesting for. If you see in fiddler, the client sends the "Accept" tag, where it mentions the desired format of the response. If we modify it and make it text/json, the same code will respond back with the same data in JSON format.

FromBody tag -
If you see the post method in the controller, the input args has "FromBody" attribute attached to it, which is responsible for de-serializing the object sent.