by Santhakumar Munuswamy
Posted on 13 February 2015
WCF
Introduction
This article explains the difference between Web Services and WCF techniques.
Overview
My previous article provided an Introduction to WCF (Windows Communication Framework). Now we will discuss the difference between Web Services and WCF concepts.
Web Services (ASMX)
Web Services are used to send/receive message using the SOAP (Simple Object Access Protocol) via HTTP only.
It is available in the namespace “System.Web.Services. WebService Class” with a constructor, methods, prosperities and events.
Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
namespace HelloWorldServices
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
WCF
WCF is used to exchange message using any format via any transport protocol like HTTP, TCP/IP, MSMQ, Named Pipes and so on. Its default format is SOAP.
Note:
Microsoft Message Queuing (MSMQ) is a messaging queue services developed by Microsoft.
Simple Object Access Protocol (SOAP) is a messaging protocol developed by W3C.
Code : IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace HelloWorldWCFService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace HelloWorldWCFService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
Difference between Web Service and WCF Table
S. No
|
Web Services
|
WCF
|
1
|
It is available namespace name "System.Web. Services.WebService Class"
|
It is available namespace name "System.ServiceModel"
|
2
|
It is supported hosting only IIS
|
It is supported hosting like IIS, Self Hosting (Console hosting), Windows Activation Services, Windows Services
|
3
|
It is use to XML Serializer only
|
It is use to DataContractSerializer only
|
4
|
It is supported one-way communication and Request-Response
|
It is supported one-way, two-way (Duplex) communication and Request- Response
|
5
|
It is supported binding like XML 1.0, MTOM (Message Transmission Optimization Mechanism), Custom
|
It is supported binding like XML 1.0, MTOM (Message Transmission Optimization Mechanism),Binary and Custom
|
6
|
It is supported transport protocol like HTTP, TCP and custom
|
It is supported transport protocol like HTTP, TCP, Named Pipes, MSMQ, P2P and custom
|
7
|
It is supported protocol security
|
It is supported protocol security, transaction and reliable message
|
Conclusion
This article will help to understand difference between Web Services and WCF in .NET