Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Difference between ASMX (Asp.Net classic Web Services) and WCF Services.

 ASMX VS WCF Services:

Web service:

Before WCF ASMX Web Service, or simply an Asp.Net Web Service is used to create service oriented applications.
Web Service provides its services to client applications over the web; client application makes an HTTP request to a web server to get access to its various services.

WCF Service:

Windows Communication Foundation (WCF) is a framework in Microsoft .Net for building connected, service-oriented applications.

Feature

Asp.net Web service (ASMX)

WCF Service

Hosting

Supports only IIS hosting.

You can use IIS, self hosting, Windows activation service (WAS), Windows Service.

Performance

Slower compared with WCF

Faster than asp.net web service.

It performs better over XmlSerializer since, DataContractSerializer shows which properties or fields, actually translated (or serialized) into XML.

Implementation

ASMX services are defined using two attributes namely the [WebService] and [WebMethod] in a Class.

WCF services are defined using two important attributes called the [ServiceContract] and [OperationContract], typically these attributes used with interface.

Security, Reliability and transaction support

Support security but is less secure as compared to WCF.

Supports security, reliable messaging, transaction and AJAX and REST supports.

Multithreading Support

Doesn’t support multi-threading.

Supports  by using ServiceBehaviour class.

Message exchange pattern support

 

Supports One-Way, Request-Response service operations only.

Supports One-Way, Request-Response and Duplex service operations for message exchange.

Protocols

Supports only HTTP and HTTPS.

Supports protocols like HTTP, HTTPS, TCP, Named Pipes and MSMQ.

Exception handling

Unhandled Exceptions returns to the client as SOAP faults.

Supports better exception handing by using FaultContract.

Serialization

ASMX web service would use XmlSerializer attribute for data conversion.

It is only capable of serializing public properties or members.

WCF service uses the DataContractSerializer Class to translate data into XML by using the System.Runtime.Serialization.

Hash Table

 XmlSerializer cannot translate data in an Hashtable() into XML.

DataContractSerializer can translate Hashtable() data into XML.

Easy

Easy and simple to write and configure but not dynamic, example: if changes service URL require more changes compared to WCF.

 

Easy  ,reliable and portable for new changes.

 

 

Conclusion: 

It’s better to use WCF service for new project and for existing project if possible migrate your ASMX service to WCF service.

Message exchange patterns in WCF

Message exchange pattern describes how client and WCF service exchange message to each other.

You know that WCF data is in XML or JSON format and WCF Client and service exchange messages in XML or JSON format.

 There are three types of exchange patterns :

1. Request/Response
2. One way
3. Duplex

Request-reply pattern:

In the request-reply pattern, a client application sends a message to a WCF service and then waits for a reply. This is the classic and most commonly used message exchange pattern in WCF.

In this exchange pattern if the service doesn't respond to the client then the client will receive a TimeoutException.


Example:

    [ServiceContract(ConfigurationName = "IProductWebService")]
    public interface IEcommerceWebService
    {
 
        /// <summary>
        /// Product price information updating
        /// </summary>
        // specifies the request-reply message pattern
        [OperationContract(IsOneWay=false)]
        bool UpdatePriceList(ProductPrice Price);
      }

 

One-way pattern:

 one-way message exchange pattern, a client application sends a message to a WCF service but the service does not send a reply message to the client. You can use this pattern when a client requests the service take an action but does not need to wait for a reply.

This pattern doesn’t supports output parameters, by-reference parameters and return value to an operation, otherwise you will receive an InvalidOperationException.


Example:

    [ServiceContract(ConfigurationName = "IProductWebService")]
    public interface IEcommerceWebService
    {
       
        /// <summary>
        /// Save new price information
        /// </summary>
        [OperationContract(IsOneWay = true)]
        void SavePriceData(BasicPrice Price);
      }

Duplex pattern:

In the request/reply and one-way message exchange patterns, only the client can initiate communication. In the duplex pattern, both the client and the service can initiate communication. The client calls a method of the service. The service can then use a client callback to call a method in the client. You can use this pattern when you want the service to send a notification or alert to the client after the client has called the service.


Example:

//associate service contract with service contract using call back attribute 
[ServiceContract(CallbackContract = typeof(IProductServiceCallBack))] 
public interface IProductService 

    [OperationContract
    Product GetInfo(); 
}
public interface IProductServiceCallBack 

   [OperationContract]
   void SaveProduct(Product BasicProduct);