Web Service vs. WCF Service
- Only public properties/fields can be serialized.
- Only collection classes implementing IEnumerable or Icollection can be serialized.
- Classes that implement IDictionary, such as HashTable cannot be serialized.
- You can't explicitly indicate which fields or properties are to be serialized into XML and which are to be ignored by serializer.
ASP.NET WCF services use DataContractSerializer in System.RunTime.Serialization namespace for serialization, which overcomes all the limitations of XmlSerializer mentioned above.
- [WebService]
- public class MyService
- {
- [WebMethod]
- public SumClass SumOfNums(string JsonStr)
- {
- var ObjSerializer = new JavaScriptSerializer();
- var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);
- return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);
- }
- }
- public class SumClass
- {
- public int First, Second, Sum;
- public SumClass GetSumClass(int Num1, int Num2)
- {
- var ObjSum = new SumClass
- {
- Sum = Num1 + Num2,
- };
- return ObjSum;
- }
- }
Classes and structures to be exposed should be decorated with [DataContract] attribute and [DataMember] attribute should be added to fields or properties.[ServiceContract] attribute has to be added to the service class.[OperationContract]attribute should be added to the methods exposed by the service.
- [ServiceContract]
- blic class MyService : WebService
- {
- [OperationContract]
- public SumClass SumOfNums(string JsonStr)
- {
- var ObjSerializer = new JavaScriptSerializer();
- var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);
- return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);
- }
- }
- [DataContract]
- public class SumClass
- {
- [DataMember]
- public int First;
- [DataMember]
- public int Second;
- [DataMember]
- public int Sum;
- public SumClass GetSumClass(int Num1, int Num2)
- {
- var ObjSum = new SumClass
- {
- Sum = Num1 + Num2,
- };
- return ObjSum;
- }
- }
WCF services can be hosted in IIS , WindowsActivation Services(WAS), Managed Windows services or self hosting etc.
WCF uses ServiceModelMetadata Utility tool,svcutil.exe for the same purpose.
- namespace MyWebServiceDemo
- {
- public class CustomSoapHeader : SoapHeader
- {
- public string CustomElement;
- }
- [WebService]
- public class MyService
- {
- CustomSoapHeader ObjCustomSoapHeader = new CustomSoapHeader();
- [WebMethod]
- [SoapHeader("ObjCustomSoapHeader")]
- public int SumOfNums(int First, int Second)
- {
- return First + Second;
- }
- }
- }
- [MessageContract]
- public class CustomHeader
- {
- [MessageHeader]
- public string UserName;
- [MessageBodyMember]
- public int Id;
- }
- [ServiceContract]
- public class FaultDemo
- {
- [OperationContract]
- [FaultContract(typeof(FaultClass))]
- public int Divide(int Dividend, int Divider)
- {
- if (Divider == 0)
- {
- throw new FaultException<FaultClass>(new FaultClass
- {
- ExceptionReason = "Divider is 0",
- ExceptionDetails = "Divider should be non-zero"
- });
- }
- return Dividend / Divider;
- }
- }
11. If ASP.NET Web service class is derived from WebService, the WebService base class’ Context property can be used to access HttpContext object.Application property of HttpContext object can be used for application state and it's Session property can be used for session state.
WCF provides extensible objects for state management. ServiceHostBase is used to maintain state that all instances of all services on the same host can access, and InstanceContext maintains state that can be accessed within the same instance of a service.
So, WCF service is an advanced technology than Web service. Performance wise WCF is faster than web service. WCF provides more security, support various protocols & message formats. The only costly area of WCF for developer is it's configuration settings. However, this headache also got solved with WCF4.0 by providing default configuration settings. You can notice upto .NET3.5 visual studio is providing a direct template for web service. From .NET4.0, you will not get any direct template for web service so, you need to create a web application & add a web service to it.

Guest UserPosted Nov 1, 2018, 11:54 PM
Good Explanation..
mahdiehPosted Oct 13, 2017, 1:43 AM
Very helpful and excellent thanks so much
Guest UserPosted Dec 31, 2015, 5:02 AM
Very useful, thank you!
Gowtham RajamanickamPosted Mar 16, 2015, 11:20 PM
well explained..
Gowtham RajamanickamPosted Mar 16, 2015, 11:20 PM
well explained..
rakesh mhatrePosted Jan 7, 2015, 3:52 AM
do u know how to open local drive in gridview ? like Team Viewer? i want to transfer files to ftp
Venkat MungariPosted Dec 7, 2014, 5:18 AM
nice comparison with examples,thanks for your article
Ajay YadavPosted Nov 18, 2014, 9:33 AM
great comparison, good
sanjay guptaPosted Oct 30, 2014, 1:52 AM
Well explained.
ajeeshkumar nPosted Oct 30, 2014, 1:43 AM
Hi Rasmita.. how wcf host in windows activation service ?
Saravanakumar SekaranPosted Oct 29, 2014, 10:43 PM
very nice article
Anubhav RanjanPosted Oct 29, 2014, 7:41 AM
Nice Article. Good to remember key points in less time.
Manju lata YadavPosted Oct 29, 2014, 4:00 AM
Simple and easy to understand.
Sam HobbsPosted Oct 29, 2014, 2:34 AM
It sounds so complicated. Thank you for making it as simple as possible.