Thursday 11 February 2016

What is Httphandler and HttpModule in Asp.Net ?





ASP.NET handles all the HTTP requests coming from the user and generates the appropriate response for it. ASP.NET framework knows how to process different kind of requests based on extension, for example, It can handle request for .aspx, .ascx and .txt files, etc. When it receives any request, it checks the extension to see if it can handle that request and performs some predefined steps to serve that request.
ASP.NET default handlers:


  • Page Handler (.aspx) - Handles Web pages
  • User Control Handler (.ascx) - Handles Web user control pages
  • Web Service Handler (.asmx) - Handles Web service pages
  • Trace Handler (trace.axd) - Handles trace functionality


Now as a developer, we might want to have some of our own functionality plugged in. We might want to handle some new kind of requests or perhaps we want to handle an existing request ourselves to have more control on the generated response, for example, we may want to decide how the request for .jpg or .gif files will be handled. Here, we will need an HTTPHandler to have our functionality in place.



Example :

public class MyHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            if (context.Request.RawUrl.Contains(".cspx"))
            {
              
                string newUrl = context.Request.RawUrl.Replace(".cspx", ".aspx");
                context.Server.Transfer(newUrl);
            }
         }

      }



Now we need to add our handler in webConfig file to use it.

<httpHandlers>
    <add verb="*" path="*.cspx" type="MyHandler "/>
</httpHandlers>





What is HTTP module:

Help in processing of page request by handing application events , similar to what global.asax does. A request can pass through many HTTP modules but is being handled by only one HTTP handlers.



Use of HTTP Modules:

ASP.NET uses HTTP modules to enable features like caching, authentication, error pages etc.
<add> and <remove> tags can be used to add and inactive any http module from <httpmodule> section of a configuration file.


<add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
<add name="WindowsAuthentication"
type="System.Web.Security.WindowsAuthenticationModule"/>
<add name="FormsAuthentication"
type="System.Web.Security.FormsAuthenticationModule"/>
</httpModules>



Here I just explained about Httphandler and HttpModule. I hope its useful to readers. Please send your comments and feedback. Thank You.













Wednesday 10 February 2016

How to implement Dispose Method? Differences between Dispose and Finalize methods ?



It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessar

You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.

A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.

Code Example:-

public class MyClass : IDisposable
    {
        private bool disposed = false;
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        //Implement Dispose method to dispose resources
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // TO DO: clean up managed objects
                    Console.WriteLine("Managed code has been disposing");
                    Console.ReadKey();
                }

                // TO DO: clean up unmanaged objects
                Console.WriteLine("UnManaged code has been disposing");
                Console.ReadKey();
                disposed = true;
            }
        }
    }
    class Program
    {
        public static void Main(String[] agues)
        {
            MyClass mc = new MyClass();
            mc.Dispose();
            Console.WriteLine("All resources are released from memory.");
            Console.ReadKey();
        }
         
    }



 Output:-


  



Dispose() Method:-

  • It is used to free unmanaged resources like files, database connections etc. at any time.
  • Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.
  • It belongs to IDisposable interface.
  • It's implemented by implementing IDisposable interface Dispose() method.
  • There is no performance costs associated with Dispose method.

Finalize() Method:-

  • It can be used to free unmanaged resources (when you implement it) like files, database connections etc. held by an object before that object is destroyed.
  • Internally, it is called by Garbage Collector and cannot be called by user code.
  • It belongs to Object class.
  • It's implemented with the help of destructor in C++ & C#.
  • There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.