I Code, Therefore I Am

while(!(succeed == try()));

Differences between ASP.NET Web Service and WCF Services Part I

Recently, after I published an entry on .NET Web Services someone said it would be nice if I did a follow-up showing now Web Services differ from WFC Services, so I thought that would be a perfect think to write about, especially because they’re so similar yet very much different.

WCF Definition

WCF is a part of the .NET Framework that provides a unified programming model for rapidly building service-oriented applications that communicate across the web and the enterprise

On the surface they may seem very much alike, and when I used WCF the first time I thought the same thing, but boy was I wrong. ASMX uses the .NET Framework to do its work, while WCF is its own framework.

Developing web services in ASP.NET completely relies on defining data, and it also relies on the XmlSerializer also has its own setbacks as well. The following is a list of key issues to know when defining .NET Framework classes that the XmlSerializer can serialized to and from XML:

Only the public fields and property can be serialized into XML Instances of collection classes can be serialized into XML but only if the class implemented either ICollection Classes that implement IDictionary interface, like the Hashtable cannot be serialized into XML.

A WCF service relies on the DataContractSerializer benefits of using the DataContractSereializer are:

  1. DataContractSerializer is simply faster and more efficient than the XmlSerializer used by Web Services
  2. XMLSerialization does not indicate the which fields or properties of the type are serialized into XML whereas DataContractSerializer Explicitly shows the which fields or properties are serialized into XML.
  3. The DataContractSerializer can translate the HashTable into XML.

To develop an ASP.NET Web Service we must add the WebService Attribute to the class and WebMethodAttribute to sll the class methods (If we want then visible to the caller. Here’s an example.

Web Service Exsmple:

[WebService]
public class Service : System.Web.Services.WebService
{
    [WebMethod]
    public string Test(string strMsg)
    {
        return strMsg;
    }
}

If you were to write a WCF service we would use this code
WCF Service”

[ServiceContract]
public interface ITest
{
    [OperationContract]
    string ShowMessage(string strMsg);
}
public class Service : ITest
{
    public string ShowMessage(string strMsg)
    {
        return strMsg;
    }
}

The ServiceContractAttribute specifies that a interface defines a WCF service contract, OperationContract Attribute indicates which of the methods of the interface defines the operations of the service contract.

NOTE: A class that implements the service contract is referred to as a service type in WCF.

Now down to the heart of this, and that’s showing the differences between an ASP.NET Web Service and 2 WCF Service:

  • 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 protocols like Named Pipe,TCP and others. Many 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.SerializatiSon is supported while in the WCF Service System.RunTime.Serialization is supported.
  • WCF Services can be multithreaded via ServiceBehavior class while web service cannot be. WCF Services supports a 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.

This has been a short entry on the differences, you can be expecting more in the future, Thanks for reading and I hope you found it informative. To Be Continued


About The Author

Richard has been a professional software developer for over 15 years now. In fact he was a geek long before being a geek was 'cool'. He has his Bachelors in Computer Science from The University of Georgia and is an avid, passionate .NET developer. Richard eats, sleeps, drinks and dreams in code

Comments

One Response to “Differences between ASP.NET Web Service and WCF Services Part I”

  1. Sathish says:

    Nice explanation.

Leave a Reply