Create the Console Client Application for Durable Service

Step 1: Download and follow the Steps To create a Durable Service from my last blog and Run it.
 
Step 2: Create the console client application and name it as DurableServiceClient
 
Step 3:  Add following reference to client application
 
System.ServiceModel
System.WorkflowService
 
Step 4: Run the WCF service and Add Service Reference to the project and name it as SimpleCalculatorService
 
Step 5: Create the Helper class called it as Helper.cs. This helper class is used to Store, Retrieve and set the context at the client side. Context information will be saved in 'token_context.bin' file. Copy and paste the below code to your helper file.
 
Helper.cs
  1. using System.ServiceModel.Channels;  
  2. using System.ServiceModel;  
  3. using System.Net;  
  4. using System.IO;  
  5. using System.Runtime.Serialization.Formatters.Binary;  
  6. public class Helper  
  7. {  
  8.     static readonly String TokenContextFileName = "token_context.bin";  
  9.     public static IDictionary<String, String> LoadContext()  
  10.     {  
  11.         IDictionary<String, String> ctx = null;  
  12.         try  
  13.         {  
  14.             using (FileStream fs = new  
  15.             FileStream(TokenContextFileName, FileMode.Open, FileAccess.Read))  
  16.             {  
  17.                 BinaryFormatter bf = new BinaryFormatter();  
  18.                 ctx = bf.Deserialize(fs) as IDictionary<String, String>;  
  19.                 fs.Close();  
  20.             }  
  21.         }  
  22.         catch (Exception ex)  
  23.         {  
  24.         }  
  25.         return ctx;  
  26.     }  
  27.     public static void SaveContext(IClientChannel channel)  
  28.     {  
  29.         IDictionary<String, String> ctx = null;  
  30.         IContextManager cm = channel.GetProperty<IContextManager>();  
  31.         if (cm != null)  
  32.         {  
  33.             ctx = cm.GetContext() as IDictionary<String, String>;  
  34.             try  
  35.             {  
  36.                 using (FileStream fs  
  37.                 = new FileStream(TokenContextFileName, FileMode.CreateNew))  
  38.                 {  
  39.                     BinaryFormatter bf = new BinaryFormatter();  
  40.                     bf.Serialize(fs, ctx);  
  41.                     fs.Close();  
  42.                 }  
  43.             }  
  44.             catch (Exception ex)  
  45.             {  
  46.             }  
  47.         }  
  48.     }  
  49.     public static void DeleteContext()  
  50.     {  
  51.         try  
  52.         {  
  53.             File.Delete(TokenContextFileName);  
  54.         }  
  55.         catch (Exception ex)  
  56.         {  
  57.         }  
  58.     }  
  59.     public static void SetContext(IClientChannel channel,  
  60.     IDictionary<String, String> ctx)  
  61.     {  
  62.         IContextManager cm = channel.GetProperty<IContextManager>();  
  63.         if (cm != null)  
  64.         {  
  65.             cm.SetContext(ctx);  
  66.         }  
  67.     }  
  68. }  
Step 6: On Programm.cs page write this code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace DurableServiceclient  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             SimpleCalculatorService.SimpleCalculatorClient client  
  12.             = new SimpleCalculatorService.SimpleCalculatorClient();  
  13.             int currentValue = 0;  
  14.             currentValue = client.Add(200);  
  15.             Console.WriteLine("The current value is {0}", currentValue);  
  16.             Helper.SaveContext(client.InnerChannel);  
  17.             client.Close();  
  18.             client = new SimpleCalculatorService.SimpleCalculatorClient();  
  19.             IDictionary<stringstring> cntx = Helper.LoadContext();  
  20.             Helper.SetContext(client.InnerChannel, cntx);  
  21.             currentValue = client.Subtract(23);  
  22.             Console.WriteLine("The current value is {0}", currentValue);  
  23.             currentValue = client.Multiply(2);  
  24.             Console.WriteLine("The current value is {0}", currentValue);  
  25.             Helper.DeleteContext();  
  26.             client.EndPersistence();  
  27.             Console.WriteLine("Press <ENTER> to shut down the client.");  
  28.             Console.ReadLine();  
  29.             client.Close();  
  30.         }  
  31.     }  
  32. }  
Step 7: Run and See The Result. For more find the attached code.