- 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, 7:54 PM EDT
Good Explanation..
mahdiehPosted Oct 12, 2017, 9:43 PM EDT
Very helpful and excellent thanks so much
Guest UserPosted Dec 31, 2015, 12:02 AM EST
Very useful, thank you!
Gowtham RajamanickamTOP 100Posted Mar 16, 2015, 7:20 PM EDT
well explained..
Gowtham RajamanickamTOP 100Posted Mar 16, 2015, 7:20 PM EDT
well explained..
rakesh mhatrePosted Jan 6, 2015, 10:52 PM EST
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, 12:18 AM EST
nice comparison with examples,thanks for your article
Ajay YadavPosted Nov 18, 2014, 4:33 AM EST
great comparison, good
sanjay guptaPosted Oct 29, 2014, 9:52 PM EDT
Well explained.
ajeeshkumar nPosted Oct 29, 2014, 9:43 PM EDT
Hi Rasmita.. how wcf host in windows activation service ?
Saravanakumar SekaranPosted Oct 29, 2014, 6:43 PM EDT
very nice article
Anubhav RanjanPosted Oct 29, 2014, 3:41 AM EDT
Nice Article. Good to remember key points in less time.
Manju lata YadavPosted Oct 29, 2014, 12:00 AM EDT
Simple and easy to understand.
Sam HobbsPosted Oct 28, 2014, 10:34 PM EDT
It sounds so complicated. Thank you for making it as simple as possible.