Skip to main content

Posts

Showing posts with the label Serializer

Svcutil.exe

Svcutil.exe Creating data contract from service assembly We can create a schema definition from the Service assembly  Create a WCF Service and build it. Ensure that its working correctly.  Open developer command prompt  Navigate to the bin/Debug folder of the project  Execute the command svcutil /dconly ProductServiceLibrary.dll This should create a bunch of files Open ProductServiceLibrary.xsd in notepad We can also create a ProductReview class from the schema definition. This is the inverse of the process that created a data contract based on the xsd Go back to developer command prompt and run the following command svcutil /dconly ProductServiceLibrary.xsd Notice that the above command creates a cs file called ProductServiceLibrary.cs Links and references ServiceModel Metadata Utility Tool (Svcutil.exe) -  https://msdn.microsoft.com/en-us/library/aa347733(v=vs.110).aspx

Serialization and Deserialization Demo

Open the WCF service application that you created earlier. Add a new project using Windows > Console application template Lets name it SerializationDemo Add reference to the WCF Service project and also add reference to System.Runtime.Serialization and System.IO Add the following code  static void Main(string[] args)         {             ProductReview p = new ProductReview() { Id = "001", ProductCode = "12", ProductName = "Mask", Review = "Very Good!" };             DataContractSerializer dcs = new DataContractSerializer(typeof(ProductReview));                       //Serialize             using (FileStream fs = new FileStream("ProductReview.xml", FileMode.Create))             {                 dcs.WriteObject(fs, p);   ...

WCF Serialization

Lets say we want to beam Kirk and Spock (from StarTrek) to earth to save earth! To do this Kirk and Spock have to be de-materialized into energy pattern and these energy pattern re-materialize when it arrives on earth. Similarly, if you want to send two .NET objects, for example Kirk and Spock, over the wire to another application then you can use Serializers. Serializers are used to serialize objects into message and then to deserialize message back to objects at the destination. WCF uses a number of Serializers here are some of them: DataContractSerializer NetDataContractSerializer Both serializers inherit from XMLObjectSerializer NetDataContractSerializer Used when same type is expected on either side of the wire. Requires .NET framework on both ends Makes the resulting XML more complex When serializing an instance of a type note the following: Serializer created to serialize a root type can only be used to serialize instances of that root type and instance...