Sameer Gaikwad CRUD Operation SQL stored procedure mesameergaikwad
This article was originally published on mesameergaikwad as Sameer Gaikwad CRUD Operations Using LINQ to SQL in MVC - mesameergaikwad
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 the 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 the HttpContext object. The application property of the HttpContext object can be used for the application state and its Session property can be used for the 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 and supports various protocols & message formats. The only costly area of WCF for developers is its configuration settings. However, this headache was also got solved with WCF4.0 by providing default configuration settings. You can notice up to.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.
0 Comments