Skip to main content

Posts

Showing posts from June, 2016

Example - Chat Application

1. Create a WCF Service library project Open Visual Studio > File  >  New  >  Project Select  WCF Service Library  template under  Visual C# > WCF  templates Change the name to  ChatServiceLibrary  and click  OK Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.  Delete IService1.cs and Service1.cs and add a new class called ChatService 1.1 Create the Data Contract  Open ChatService.cs file that you created. Add  using System.ServiceModel; Add  using System.Runtime.Serialization; Add the following code above ChatService class    [DataContract]     public class ChatMsg     {         public ChatMsg() { }         public ChatMsg(string n, string m){ this.User = n; this.Msg = m;}            ...

WCF Service Contracts

Create a WCF Service library project Open Visual Studio Select  File  >  New  >  Project Select  WCF Service Library  template under  Visual C# > WCF  templates Change the name to  ProductServiceLibrary  and click  OK Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.  Delete IService1.cs and Service1.cs Add a new class called ProductService Add the following code.      [DataContract]     public class ProductReview     {         [DataMember]         public string Id { get; set; }         [DataMember]         public string ProductCode { get; set; }         [DataMember]         public string ProductName { get; set; }         [DataMember] ...

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);   ...

Serialization and Deserialization Demo 2

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!" };             if (args.Length > 0 && args[0].Equals("s"))             {                  DataContractSerializer dcs = new DataContractSerializer(typeof(ProductReview));                         //Serialize                 using (FileStream fs = new Fi...

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...

WCF Syndication RSS/Atom

1 Create a RESTful WCF Service  Open Visual Studio Select  File  >  New  >  Project Select  WCF Service Library  template under  Visual C# > WCF  templates Change the name to  ProductServiceLibrary  and click  OK Visual studio will add reference to System.ServiceModel, System.Runtime.Serialization and will add some files with sample code.  Delete IService1.cs and Service1.cs Add reference to System.ServiceModel.Web Add a new class called ProductService 1.1 Create the Data Contract  Open ProductService.cs file that you created. Add  using System.Runtime.Serialization; Add  using System.ServiceModel; Add  using System.ServiceModel.Web; Add  using using System.ServiceModel.Syndication; Add the following code in the ProductService.cs class      [DataContract]     public class ProductReview      {     ...

WCF Exercise

Scenario Ultimate Lottery and Gaming (ULG) wants to build a WCF application that allows the client application at the ticket vendor to retrieve winning number of a given lottery draw.  Task 1: Create a WCF service that exposes a method called GetWinningNo that takes a string parameter and returns an object of type Draw. Use the following two partially completed classes:      public class Draw     {         public string Id { get; set; }         public int [] WinningNo { get; set; }          public double PoolAmt { get; set; }     }     public class LottoSrv     {         Dictionary<string, Draw> drawList = new Dictionary<string, Draw>();         public LottoSrv()         {             int [] w1 = { 1, 2, 3, 4, 5, 6, 7 };...