Thursday, December 2, 2010

What is difference between HTTP Handler and HTTP Module.

Here are the difference between HTTP Handlers and HTTP Modules.

Http Handlers:

Http handlers are component developed by asp.net developers to work as end point to asp.net pipeline. It has to implement System.Web.IHttpHandler interface and this will act as target for asp.net requests. Handlers can be act as ISAPI dlls the only difference between ISAPI dlls and HTTP Handlers are Handlers can be called directly URL as compare to ISAPI Dlls.

Http Modules:

Http modules are objects which also participate in the asp.net request pipeline but it does job after and before HTTP Handlers complete its work. For example it will associate a session and cache with asp.net request. It implements System.Web.IHttpModule interface.

HTTP Handler implement following Methods and Properties

  1. Process Request: This method is called when processing asp.net requests. Here you can perform all the things related to processing request.
  2. IsReusable: This property is to determine whether same instance of HTTP Handler can be used to fulfill another request of same type.

Http Module implements following Methods and Properties.

  1. InIt: This method is used for implementing events of HTTP Modules in HTTPApplication object.
  2. Dispose: This method is used perform cleanup before Garbage Collector destroy everything.

An Http Module can Support following events exposed to HTTPApplication Object.

  1. AcquireRequestState: This event is raised when asp.net request is ready to acquire the session state in http module.
  2. AuthenticateRequest: This event is raised when asp.net runtime ready to authenticate user.
  3. AuthorizeRequest: This event is raised when asp.net request try to authorize resources with current user identity.
  4. BeginRequest: This event is raised when HTTP Modules receive new request.
  5. EndRequest: This event is raised before sending response to client.
  6. Disposed: This event is raised when http modules completes processing of request
  7. Error: This event is raised when any error occurs during processing of request.
  8. PreRequestHandlerExecute: This event is raised just before ASP.NET begins executing a handler for the HTTP request. After this event, ASP.NET will forward the request to the appropriate HTTP handler.
  9. PostRequestHandlerExecute: This event is raised when when HTTP Handler will finish the execution of current request.
  10. PreSendRequestContent: This event is raised just before ASP.NET sends the response contents to the client. This event allows us to change the contents before it gets delivered to the client. We can use this event to add the contents, which are common in all pages, to the page output. For example, a common menu, header or footer.
  11. PreSendRequestHeaders: This event is raised before asp.net Just send response header to client browser.
  12. ReleaseRequestState: This event is raised when asp.net runtime finishes handling of all the request.
  13. ResolveRequestCache: This event is raised to determine whether the request can be fulfilled by returning the contents from the Output Cache.
  14. UpdateRequestCache: This event is raised when ASP.NET has completed processing the current HTTP request and the output contents are ready to be added to the Output Cache.

Hope this will help you better understanding of HTTP Handler and HTTP Modules. I will post an real time implementation code in forthcoming blog post.

Shout it
kick it on DotNetKicks.com
Share:
Wednesday, December 1, 2010

GUID Vs Int data type as primary key

Recently one of my friend ask me when I should go for GUID and When I should go for Int as primary key in table. So decided to write a blog post for it. Here are advantages and disadvantage of the GUID and INT.

INT Data Type:

Advantages:

  1. Its required small space in terms of the storage it will only allocates 4 bytes to store data.
  2. Insert and update performance will be faster then the GUID. It will increase the performance of the application.
  3. Easy to index and Join will give best performance with the integer.
  4. Easy to understand and remember
  5. Support of function that will give last value generated like Scope_Indentity()

Disadvantages:

  1. If you are going to merge table frequently then there may be a chance to duplicated primary key.
  2. Limited range of uniqueness if you are going to store lots of data then it may be chance to run out of storage for INT data type.
  3. Hard to work with distributed tables.

GUID Data Type:

Advantages:

  1. It is unique for the current domains. For primary key is uniquely identifies the table.
  2. Less chances of for duplication.
  3. Suitable for inserting and updating large amount of data.
  4. Easy for merging data across servers.

Disadvantages:

  1. Bigger storage size (16bytes) will occupy more disk size then integer.
  2. Hard to remember and lower performance with Join then integer.
  3. Don’t have function to get last uniquely generated primary key.
  4. A GUID primary Key will added to all the other indexes on tables. So it will decrease the performance.

Conclusion:

From above the advantages and disadvantages we can conclude that if you are having very large amount of data in table then go for the GUID as primary key in database. Otherwise INT will give best performance. Hope this will help you. Please post your opinion as comment.

Shout it
kick it on DotNetKicks.com
Share:
Friday, November 26, 2010

Difference between int.Parse and Convert.ToInt32

I know this post sound basic to most of people but still lots of people does not know this. So I decided to post a blog post for this. Both int.Parse and Convert.ToInt32 are used to convert string into the integer but Only difference between them is to Convert.ToInt32 handle null and returns ‘0’ as output and int.parse is not going to handle NULL and will give a Argument Null Exception. Here is the example for that both are almost same except handling null.


          string convertToInt = "12";
string nullString = null;
string maxValue = "32222222222222222222222222222222222";
string formatException = "12.32";

int parseResult;

// It will perfectly convert interger
parseResult= int.Parse(convertToInt);

// It will raise Argument Null Exception
parseResult= int.Parse(nullString);

//It willl raise Over Flow Exception
parseResult= int.Parse(maxValue);

//It will raise Format Exception
parseResult= int.Parse(formatException);


//For Convert.ToInt32

//It will perfectly convert integer
parseResult= Convert.ToInt32(convertToInt);

//It will ouput as 0 if Null string is there
parseResult= Convert.ToInt32(nullString);

//It will raise Over Flow Exception
parseResult= Convert.ToInt32(maxValue);

//It will raise Format Exception
parseResult= Convert.ToInt32(formatException);

Hope this will help you understand the better but still there is third option available called int.TryParse which can handle all kind of exception and return result as Output Parameter.

Shout it
kick it on DotNetKicks.com
Share:

Microsoft Community Techdays in Ahmedabad on 11th December

Microsoft Community Techdays is a great event organized by Microsoft for Every Quarter. Its going to be there on 11th December in Ahmedabad. I want to be there hope I could as my schedule is so start but you guys can not forgot to register at this event. It’s totally free and It’s great place to meet lots of interesting guys like Pinal Dave,Jacob Sebastian etc.

spot

Here are the schedule for Ahmedabad.

10:15am - 10:30am
Welcome - Pinal Dave

10:30am - 11:15am
SQL Tips and Tricks for .NET Developers by Jacob Sebastian

11:15am - 11:30am
Tea Break

11:30am - 12:15pm
Best Database Practice for SharePoint Server by Pinal Dave

12:15pm - 01:00pm
Self Service Business Intelligence by Rehab

01:00pm - 02:00pm
Lunch

02:00pm - 02:45pm
Managing your future, Managing your time by Vinod Kumar

02:45pm - 03:30pm
Windows Azure News and Introducing Storage Services by Mahesh Devjibhai Dhola

03:30pm - 03:45pm
Tea Break

03:45pm - 04:30pm
Improve Silverlight application with Threads and MEF by Prabhjot Singh Bakshi

04:30pm - 04:45pm
Thank you - Mahesh Devjibhai Dhola

So don’t forgot to be there. Register your seat at : http://www.communitytechdays.com

Shout it
Share:
Friday, October 29, 2010

Difference between Web Service and WCF Service

While taking interviews of .NET developers I often ask this questions. But lots of people don’t know exact difference between this. So, I decided to write a separate blog about this.

Here are the few differences.

  • Web services can be hosted in IIS as well as outside of the IIS. While WCF service can be hosted in IIS, Windows activation service,Self Hosting,WAS and on lots of proctols like Named Pipe,TCP etc.Here lots of people disagree how we can host the web service outside of the IIS but Here is the article for that.http://msdn.microsoft.com/en-us/library/aa529311.aspx.
  • In Web Services Web Service attribute will added  on the top of class. In WCF there will be a Service Contract attributes will be there. Same way Web Method attribute are added in top of method of Web service while in WCF Service Operation Contract will added on the top method.
  • In Web service System.XML.Serialization is supported while in the WCF Service System.RunTime.Serialization is supported.
  • WCF Services can be multithreaded via ServiceBehavior class while web service can not be.
  • WCF Services supports different type of bindings like BasicHttpBinding, WSHttpBinding, WSDualHttpBinding etc.while Web services only used soap or xml for this.
  • Web services are compiled into a class library assembly. A file called the service file is provided that has the extension .asmx and contains an @ WebService directive that identifies the class that contains the code for the service and the assembly in which it is located while in WCF.WCF services can readily be hosted within IIS 5.1 or 6.0, the Windows Process Activation Service (WAS) that is provided as part of IIS 7.0, and within any .NET application. To host a service in IIS 5.1 or 6.0, the service must use HTTP as the communications transport protocol.

Hope this will help you. Happy programming!!!

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:
Tuesday, October 26, 2010

Output Caching in asp.net

Recently one of my friend ask about output cache so I decided to put a post about how output cache works and what is the advantages and disadvantage of using output cache. Output cache is a technique to cache generated response from the asp.net pages or controls. Output Caching increases the performance drastically by reducing server round trips. We can use @OutputCache directive to controls output caching for a page or controls.

The @OutputCache includes following attributes.

  • Duration: This attribute will explain how long output cache will be there for a page or control. It can be set in seconds. If you set 60 then it will not going to generate response from server until 60 second It will generate response from the cache it self. Here is example of duration where it will set 60 second for page.
  • <%@ OutputCache Duration="60" %>  

  • VaryByParam: This attribute will determine cache entries based on get or post parameters. It will vary cache based on get or post parameters suppose you set product Id query string as VaryByParam it will create a different cache based on product Id. Following is a example how you can set the VaryByParam based on Product Id.
  • <%@ OutputCache Duration="Seconds" VaryByParam="ProductId"%>
    

  • Location: This attribute will specify where the Item will be cached. Here are options available for that.
    • Any: The output cache can be located at any browser from where request is generated or Server where request is processed or Proxy server participating in request.
    • Client: The output cache will be located on browser client from where request is generated.
    • Downstream: The output cache can be stored in any HTTP 1.1 cache-capable devices other than the origin server. This includes proxy servers and the client that made the request.
    • Server: The output cache will stored in the server where generated request will be processed.
    • ServerAndClient: The output cache will generated either on Browser where request generated or on server where generated request will be processed. Proxy servers are not allowed for this.
    • None: None specifies that output cache will be disabled for this controls or Page.
Here is example of location.

      <%@ outputcache duration="10"  Location="Server" %>
      

  • VaryByCustom: This attribute is for different browsers where request is generated this means it will generate new instance of cache based on different browser versions.
  • <%@ OutputCache Duration="Seconds" VaryByCustom="Browser" %>
  • VaryByHeader: This attribute allows to determine different instances of cache based on the headers. Here is example for it.
  • <%@ OutputCache VaryByHeader="Accept-Language" %>
    
Note: If you specify the output cache it will not fire server side events like click or selected index changed etc. So make sure the controls that you used in output cache will not have this kind of controls Or you have to handle this in other scenarios.

Hope this will help you.. Happy programming!!

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Difference between sliding expiration and absolute expiration

ASP.net cache is great feature through which we can increase the performance of the our web application via reducing server round trips to database. We can cache any serializable data into the cache. There are so many ways to cache data but one of the simplest way to cache data like insert data into cache object. Here we must need to validate cache if any data is changed and there are so many ways from where we can set dependency to validate the cache like files,SQL Cache Dependency etc. We also can validate cache or expire via setting time to duration to its object. Like after the defined time our cache will expire and then it will again put new fresh data into the cache. This is called as time base expiration. We can put this kind of expiration via two way.

  1. Absolute Expiration
  2. Sliding Expiration

Absolute Expiration: Absolute expiration means It will expire cache after some time period set at the time of activating cache. This will be absolute expiration whether cache will be used or not It will expire the cache. This type of expiration used to cache data which are not frequently changing.

Sliding Expiration: Sliding expiration means It will expire cache after time period at the time of activating cache if any request is not made during this time period. This type of expiration is useful when there are so many data to cache. So It will put those items in the cache which are frequently used in the application. So it will not going to use unnecessary memory.

Here is the example how we can set the sliding expiration and absolute expiration.

string cacheData = "The data to be cached";
//Absolute Expiration
Cache.Insert("AbsoluteCacheKey", cacheData, null,
DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration);
//Sliding Expiration
Cache.Insert("SlidingExpiration", cacheData, null,
System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));
In above example I have created the a string to be cache that can be any serialized data. In the absolute expiration you can see that it will expires after one minute whether its accessed or not. While in sliding expiration it will expire cache if cache is not accessed within specified time.

Hope this will help you for better understanding of asp.net caching technique. Happy programming!!!

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews