A Windows Communication Foundation (WCF) is a Microsoft technology, which provides a framework for developers to design service-oriented applications. WCF services are relatively fast, secure and support transport protocols other than https.
If you wish to learn more about WCF services, we recommend you go through our Comprehensive WCF Tutorial for Beginners. This tutorial will introduce you to the world of WCF, its usages and advantages over other web services.
WCF services provide everything that ASMX web services provide, with many added features. Therefore, it is important to know why we should upgrade ASMX web services with WCF services (if at all we wish to upgrade).
01) Programming the Services
WCF services are defined using two important attributes called the [ServiceContract] and [OperationContract]. Typically, an interface is where we define these attributes, though we can also define them in a class itself. Learn more on Invoking a WCF Service method using jQuery Ajax and JSON.
C#
[ServiceContract()] public interface IBooks { [OperationContract()] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)] BookDetails ShowBookDetails(string sBookID); }
Vb.Net
<ServiceContract()> Public Interface IBooks <OperationContract()> _ <WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json)> _ Function ShowBookDetails(ByVal sBookID As String) As BookDetails End Interface
ASMX services are defined using two attributes namely the [WebService] and [WebMethod] in a Class. Learn more on Using Asp.Net Web Service with an example.
C#
public class WebService : System.Web.Services.WebService { [WebMethod] public ListShowCountry(string sLookUP) { … } }
Vb.Net
Public Class WebService Inherits System.Web.Services.WebService <WebMethod()> _ Public Function ShowCountry(ByVal sLookUP As String) As List(Of String) … End Function End Class
02) Serialization
Serialization is a process of converting or translating data (in any format) into XML before sharing it on the network.
Using XmlSerializer in ASMX
Typically, an ASMX web service would use XmlSerializer attribute for data conversion. The namespace it uses is “System.Xml.Serialization”.
It is only capable of serializing public properties or members with Get and Set methods. Now, that apparently is a limitation, since it does not serialize private properties. For example, if you have declared a private class, the serializer will not be able to translate the class at all.
XmlSerializer cannot translate data in an Hashtable() into XML.
Using DataContractSerializer in WCF
A WCF service uses the DataContractSerializer Class to translate data into XML by using the System.Runtime.Serialization namespace. Data translation using DataContractSerializer engine is more efficient and much quicker than the XmlSerializer.
It performs better over XmlSerializer since, DataContractSerializer shows which properties or fields, actually translated (or serialized) into XML.
DataContractSerializer can translate Hashtable() data into XML.
Learn more on WCF DataContractSerializer and [DataContract()] with examples.
C#
using System.Runtime.Serialization; [DataContract()] public class BookDetails { [DataMember()] public string Name { } }
Vb.Net
Imports System.Runtime.Serialization <DataContract()> _ Public Class BookDetails <DataMember()> Public Property Name() As String Get End Get Set End Set End Property End Class
03) How Can We Host Both the Services?
We can host WCF services on a variety of servers such as in IIS, Managed Windows Service, in Windows Process Activation Service (commonly known as WAS), Self Hosting or Stand Alone Applications.
We have come across this MSDN article , which has a detail description on various Hosting methods for WCF services.
On the other hand, we can host ASMX services in IIS. It is as simple as hosting a website. Compile the web services (classes) and copy the files (.asmx) in “BIN” directory, situated in the root directory.
Using WSE (Web Services Enhancements), we can host ASMX services in Windows Services, Console Applications and Windows Form Applications too.
04) Supported Transport Protocols
WCF services support various transport protocols to transfer data from one endpoint to another.
The transport protocol is the first few set of characters before the colon “:” as shown clearly in the above picture. Other than https (Hypertext Transfer Protocol), WCF services support four more protocols.
• HTTPs (Hypertext Transfer Protocol Secure)
• TCP (Transmission Control Protocol)
• MSMQ (Message Queuing)
• Named Pipes
ASMX services simply use an https transport protocol for transferring data between a service and its clients. It does not support any other protocol. The file with an extension .asmx is accessed using an URL similar to the one shown in the above image.
05) Exception Handling
Exception handling, as we know improves the performance of an application or a service. It is an important part in application development nowadays.
If not handled explicitly, an ASMX service would return an exception (also known as SOAP fault) to the client. Technically, this is wrong as it exposes weak patches at the service end.
Using [FaultContract] attribute, WCF services handle exceptions efficiently by not returning the unhandled exceptions to the client. We said efficient, since it does not compromise on sensitive data by accidently leaking vital information to the outside world in the form of unhandled exceptions.
A Fault Contract ensures that the WCF service transfers a properly defined error message to client, in case an error occurs during a business process.
06) Security (How secure are your services?)
Security is an important aspect when designing applications or services on a distributed environment.
Security for ASMX services
If you are using ASMX services, you can restrict users (clients) from accessing the services at will. Authentication and Authorization is done in the web.config file or using IIS itself.
Authentication
Identify your users before permitting them to use the services you provide.
Authorization
You can secure service resources using Authorization, which ensure what resources users can actually access. These users must have already passed the Authentication process.
Security for WCF Services
WCF has inherited the security features that were already available with ASMX services. Two important features of ASMX services (described above) are by default available with WCF.
WCF has other built-in security features too.
Integrity: To ensure data integrity, this feature is used to digitally sign the messages, before sending it across the network. The idea behind this process is to make sure that the receiver (client or service) receives the messages without any tampering.
Confidentiality: Encryption ensures data remains confidential and private, and unauthorized users cannot view it over the network.
WCF also supports Transport and Message level security. Each transport protocol (https, TCP etc.) has its own way of dealing with security. We will apply transport level security at service and client endpoints.
At Message level security, every message sent across the network will have user credentials and claims, usually encrypted to protect the message and user details. It is free of any protocol and provides end-to-end security. However, it does not support interoperability with ASMX web services.
Every little point that we have mentioned and explained in this article, strongly suggests that you have good reasons to migrate all your previously written ASMX services to WCF services. We also understand that every point deserves a detail explanation, but it is beyond the scope of this article.
The main objective of writing this article was to present the practical differences between the two services. We will bring many more articles related to the features discussed above in the future. Therefore, keep reading and if you have any suggestions, then do write to us.