Monday, May 27, 2013

Web Service (asmx) vs Handler (ashx)

In ASP.NET environment, it comes with quite extensive way of handling the web request. There is Page class (aspx), Handler (ashx), Web service (asmx), Web service in WCF (asmx), etc. It's all upto the developer to decide the best way to write their program.

Let's compare the Web Service (asmx) and Handler (ashx):
  • With Web Service you can run your Visual Studio and add the web service as web reference. This will create the necessary classes for you. With web service, the serialization and deserialization of the data object will be handle by .Net framework. This simply means that you can work with Web Service without having deep knowledge in the "background" process.
  • With Handler you must find out what is the parameter to be pass to the handler. No code generator that is able to help out over here. It provides very basic features to serve the request and you will have to serialize/deserialize your data objects manually (you may use JavaScriptSerializer to deal with the JSON data). It does not limit you from returning the data in JSON, XML or CSV format. It's all depend on your design.
With Web Service, it saves you from lots of codes in the serialization process. With Handler, it allows to develop low level request & response. Both has it's own pros and cons.

There is no conclusion on which one is better. It goes back to you to choose which one is suitable for the project that you work on.

6 comments:

  1. I have a question. Can you use generic asp .net handler for file transfer? I have a project, where I have a client run as windows service. Client will upload/download files from client computer to/from server every n minutes. On server side I want to make ashx which will process request and download/upload file. And it will do other things too, depends on type of request (delete file from server, compute hash of file, rename file, etc.)

    ReplyDelete
    Replies
    1. To handle file transfer efficiently, you may have to use FTP or WCF. This is because both ASMX and ASHX are relying on HTTP/HTTPS which is not efficient when handing binary data.

      In your case, you may consider develop a WCF solution and enable TCP protocol.

      Delete
  2. Yes. But in my project I have a demand that it needs to use HTTP/HTTPS protocol. So I need to stuck to WCF?

    ReplyDelete
    Replies
    1. You may configure WCF to use HTTP/HTTPS or TCP protocol. It's quite straight forward in changing the config file with the WCF Config tool.

      Delete
  3. I really enjoyed reading post, big fan. Keep up the work andplease tell me when can you publish more articles or where can I read more on the subject?
    video and blog marketing

    ReplyDelete
  4. this post help me more clear. Thanks

    ReplyDelete