Saturday, 15 October 2011

How to Create Durable Service step 9&10

Step 9: 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 10: In the main method, I was creating the proxy for the service and calling the Add operation. Call to this method will add instance state to the database. Now I have closed the proxy and creating new proxy instance. When I call the Subtract and Multiply operation, it will operate on the previously saved value (instance state).
static void Main(string[] args)
        {
           
             //Create the proxy for the service
            SimpleCalculatorService.SimpleCalculatorClient client 
            = new SimpleCalculatorService.SimpleCalculatorClient
            "WSHttpContextBinding_ISimpleCalculator");
            int currentValue = 0;
            //Call the Add method from the service
            currentValue = client.Add(10000);     
            Console.WriteLine("The current value is {0}", currentValue);
            //Save the Context from the service to the client
           Helper.SaveContext(client.InnerChannel); 
            //Close the proxy
            client.Close();
            
            //Create new Instance of the proxy for the service
            client = new SimpleCalculatorService.SimpleCalculatorClient
            ("WSHttpContextBinding_ISimpleCalculator");
            //Load the context from the client to start from saved state
            IDictionary<string,string> cntx=Helper.LoadContext();
            //Set Context to context manager
            Helper.SetContext(client.InnerChannel, cntx);
            //Call the Subtract and Multiply method from service
            currentValue = client.Subtract(2);
            Console.WriteLine("The current value is {0}", currentValue);
            currentValue = client.Multiply(5);
            Console.WriteLine("The current value is {0}", currentValue);
            //Delete the context from the client
            Helper.DeleteContext();
            //Remove persistance state from the server
            client.EndPersistence();
            Console.WriteLine("Press <ENTER> to shut down the client.");
            Console.ReadLine();
            client.Close();

        }
End of the proxy 1, service instance saved in the database as shown below.
Serialized XML instance state save in the database is shown below.
Output of the client application.

No comments:

Post a Comment