WCF(Windows Communication Foundation)- Session in WCF

Hello everyone, thanks for your support so far, in last post we saw how we can create a simple service and use the same. In this post we will see how we can make use of session in WCF.

INTRODUCTION:

WCF Session has following features:

  1. They are initiated and terminated by calling client.
  2. Message delivered during session are processed in the order in which they are received.
  3. There is no general data store associated with a WCF session.

Before using this functionality, we should make sure if we need to maintain session or not. By default WCF does not support session.

For example if we have a banking application, we need to give always the updated balance, we does not need to maintain the session at all. But if we have an E-Commerce application where a user can add anything in cart without logging into the system, we need to maintain the session.

Binding is also important if we are going to use the session, as only Ws-*, NetTCPBinding and NetNamedPipeBinding supports this feature, if we try to configure with any other binding the service wwill through an exception saying this binding doesn’t support the session.

When using the session, we can control on which method call we need to start the session and on which method to end the session , as well as control the flow of method to be called.

By using IsInitiating attribute we can set on which method we need to start the session and IsTerminating used to set on which method to end the session.

Let’s create a new service using which we will see how a session can be used and useful.

  1. Let’s create our service first. To create service Go to File-> New – > Project and from templates select WCF and select WCF Service Library. name the project as MerchandiseService an click ok. template
  2. By default we will have IService1 and Service1.cs files, let’s go ahead and rename IService1 as IMerchandiseService and Service1 as MerchandiseService.
  3. We will have 4 methods, Login, AddToCart, ReviewCart and CheckOut. Login will initiate the session and CheckOut will end the session. So lets go ahead and this methods in our interface.
  4. To control the use of session we need to provide the property of SessionMode to true with ServiceContract Attribute.
  5. Also make sure to change binding to WsHttpBinding, as basicHttpBinding does not support Session. We can find it in app. config file.                                                                                                                            <endpoint address=”” binding=”wsHttpBinding” contract=”MerchandiseService.IMerchandiseService”>
  6. Our session will start with Login and end with CheckOut method. if someone tries to call methods in wrong sequence we will get the exception. Let’s add methods to interface 
    using System.ServiceModel;
    
    namespace MerchandiseService
    {
        [ServiceContract(SessionMode = SessionMode.Required)]
        public interface IMerchandiseService
        {
            [OperationContract(IsInitiating = true, IsTerminating = false)]
            long Login(string loginName, string password);
    
            [OperationContract(IsInitiating = false, IsTerminating = false)]
            bool AddToCart(long loginId, long productId);
    
            [OperationContract(IsInitiating = false, IsTerminating = false)]
            bool ReviewCart(long cartId);
    
            [OperationContract(IsInitiating = false, IsTerminating = true)]
            bool CheckOut(long CartId);
        }
    }
    
  7. In this declaration IsInitiating = true is set on Login, it specifies that it will start the session and CheckOut has IsTerminating = true which specifies this is the final method and should be called in end.
  8. Now let’s go ahead and implement the same in service. we will be using hard-coded values, but in real-life example it will be DataAccessLayer. 
    namespace MerchandiseService
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
        public class MerchandiseService : IMerchandiseService
        {
            public bool AddToCart(long loginId, long productId)
            {
                return true;
            }
            public bool CheckOut(long CartId)
            {
                return true;
            }
            public long Login(string loginName, string password)
            {
                return 10001;
            }
            public bool ReviewCart(long cartId)
            {
                return true;
            }
        }
    }
  9. Done with the service go ahead and press F5 to see if it is working or not, getting compiled, let’s add a client and use the same. we will use a console App to see how it works.
  10. Right click on project and Add – > New Project, select Windows and select Console Application and name this App as MerchandiseClient and click ok. client
  11. Now let’s add service reference to our project, right click on references and select Add Service Reference, a dialog box will appear click on discover, as both are in same application we can use this approach, in real-life application service will be deployed on some server and we can enter the address and click on Go.
  12. Once service is discovered you can click on service and get the methods under the service.Change the Namespace to MerchandiseService and click on Advanced and from Collection Type select System.Collections.Generic.List and click ok. reference reference1
  13. This reference will be added in web/app.config file available and we can change it once we are deploying our service on other server. We can find <endpoint> in config file.
  14. Now go ahead and use this service in our program.cs file. 
    using MerchandiseClient.MerchandiseService;
    using System;
    
    namespace MerchandiseClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    using (MerchandiseServiceClient client = new MerchandiseServiceClient())
                    {
                        long loginId = client.Login("test", "test");
                        if (loginId > 0)
                        {
                            Console.WriteLine("User loggedin");
                        }
                        bool resultCart = client.AddToCart(loginId, 34445);
                        if (resultCart)
                        {
                            Console.WriteLine("Product Added to Cart");
                        }
                        bool resultReview = client.ReviewCart(345436);
                        if (resultReview)
                        {
                            Console.WriteLine("Cart review successfull");
                        }
                        bool resultCheckOut = client.CheckOut(767867);
                        if (resultCheckOut)
                        {
                            Console.WriteLine("Order Placed");
                        }                  
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
        }
    }
    
  15. Now go ahead and press F5, we should get the below result. subbessresult
  16. Now to check if session is state is actually being managed we will call CheckOut before Review. 
    using MerchandiseClient.MerchandiseService;
    using System;
    
    namespace MerchandiseClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    using (MerchandiseServiceClient client = new MerchandiseServiceClient())
                    {
                        long loginId = client.Login("test", "test");
                        if (loginId > 0)
                        {
                            Console.WriteLine("User loggedin");
                        }
                        bool resultCart = client.AddToCart(loginId, 34445);
                        if (resultCart)
                        {
                            Console.WriteLine("Product Added to Cart");
                        }
                        bool resultCheckOut = client.CheckOut(767867);
                        if (resultCheckOut)
                        {
                            Console.WriteLine("Order Placed");
                        }
                        bool resultReview = client.ReviewCart(345436);
                        if (resultReview)
                        {
                            Console.WriteLine("Cart review successfull");
                        }                 
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
        }
    }
    
  17. Done with the changes press F5, we will get the message shown when Review is called, saying CheckOut has already been called.result1

 

CONCLUSION:

There are multiple question which we need to ask if session is actually needed or not, most of the cases it will not be required, but in case we need to maintain the state we can go ahead and use this powerful feature of WCF.

In next post we will see use of instance within WCF.

You can also download the code from Here

You can follow my official facebook page , also subscribe my blog for more information.

You can also mail me on santosh.yadav19613@gmail.com in case you have any questions.

1 Comment


  1. I don’t normally comment but I gotta admit appreciate it for the post on this one : D.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *