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

using System.ServiceModel.Channels;

using System.ServiceModel;

using System.Net;

using System.IO;

using System.Runtime.Serialization.Formatters.Binary;

public class Helper

{

    static readonly String TokenContextFileName = "token_context.bin";

    public static IDictionary<String, String> LoadContext()

    {

        IDictionary<String, String> ctx = null;

        try

        {

            using (FileStream fs = new

            FileStream(TokenContextFileName, FileMode.Open, FileAccess.Read))

            {

                BinaryFormatter bf = new BinaryFormatter();

                ctx = bf.Deserialize(fs) as IDictionary<String, String>;

                fs.Close();

            }

        }

        catch (Exception ex)

        {

        }

        return ctx;

    }

    public static void SaveContext(IClientChannel channel)

    {

        IDictionary<String, String> ctx = null;

        IContextManager cm = channel.GetProperty<IContextManager>();

        if (cm != null)

        {

            ctx = cm.GetContext() as IDictionary<String, String>;

            try

            {

                using (FileStream fs

                = new FileStream(TokenContextFileName, FileMode.CreateNew))

                {

                    BinaryFormatter bf = new BinaryFormatter();

                    bf.Serialize(fs, ctx);

                    fs.Close();

                }

            }

            catch (Exception ex)

            {

            }

        }

    }

    public static void DeleteContext()

    {

        try

        {

            File.Delete(TokenContextFileName);

        }

        catch (Exception ex)

        {

        }

    }

    public static void SetContext(IClientChannel channel,

    IDictionary<String, String> ctx)

    {

        IContextManager cm = channel.GetProperty<IContextManager>();

        if (cm != null)

        {

            cm.SetContext(ctx);

        }

    }

}

 
Step 6: On Programm.cs page write this code.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DurableServiceclient

{

    class Program

    {

        static void Main(string[] args)

        {

            SimpleCalculatorService.SimpleCalculatorClient client

            = new SimpleCalculatorService.SimpleCalculatorClient();

            int currentValue = 0;

            currentValue = client.Add(200);

            Console.WriteLine("The current value is {0}", currentValue);

            Helper.SaveContext(client.InnerChannel);

            client.Close();

            client = new SimpleCalculatorService.SimpleCalculatorClient();

            IDictionary<string, string> cntx = Helper.LoadContext();

            Helper.SetContext(client.InnerChannel, cntx);

            currentValue = client.Subtract(23);

            Console.WriteLine("The current value is {0}", currentValue);

            currentValue = client.Multiply(2);

            Console.WriteLine("The current value is {0}", currentValue);

            Helper.DeleteContext();

            client.EndPersistence();

            Console.WriteLine("Press <ENTER> to shut down the client.");

            Console.ReadLine();

            client.Close();

        }

    }

}

Step 7:
Run and See The Result. For more find the attached code.
 

Ebook Download
View all
Learn
View all