Thursday, 1 March 2012

Remoting in WCF


http://www.slideshare.net/VidhiPatel3/net-remoting-11817527
Creating a Remoting Object
  1.  Create a solution named Lab1 to contain the projects
  2.   Add a new class library project to the solution named calc ,enable remoting on it
Properties :
Total : integer , Initial value set to 0
Methods :
            Add : adds two integers
Sum: add an integer to Total property
Clear: clears Total
 
3. Add a new console application to host the object named server
    Add a reference to calc.dll
    Add runtime.remoting reference to it
4-      Modify Main so that it registers an TcpChannel instance with the remoting infrastructure. Choose an     arbitrary    port no for the channel to listen on. 
5-      Put a call to ReadLine in your server at the end of Main so that the server program doesn't exit until you press the ENTER key. 
6-      Register your class that implements Calc with the remoting infrastructure so that it's exposed as a well-known server type that has singleton activation semantics
Work out any build issues with your server, then make sure you can run your server program without failure 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace calc
{
    public class Calculator : MarshalByRefObject
    {
        private int total;

        public int Tot
        {
            get { return total; }
            set { total= value; }
        }
      
        public Calculator()
        {
          
            Tot = 0;
        }
        public int add(int a, int b)
        {
            Tot = a + b;
            return Tot;
        }
        public int Sum(int x)
        {
            Tot = Tot + x;
            return Tot;
        }
        public void Clear()
        {
            Tot = 0;
        }
    }
}





Hosting the Remotable object:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpServerChannel channel = new
                   TcpServerChannel(1234);
                ChannelServices.RegisterChannel(channel, false);
                RemotingConfiguration.RegisterWellKnownServiceType
                    (typeof(calc.Calculator), "calc",
                    WellKnownObjectMode.Singleton); /////////////////////

                Console.WriteLine("Started Listening .....");
            }
            catch
            {
                Console.WriteLine("Error occured !!!!");
            }

            Console.ReadLine();

        }
    }
}




 

Create Client Applcation:-


1-      Add Windows Forms application project type, naming the project client. This executable will contain the client program that makes calls to the server program.
2-      Make interface to get two integer numbers , and to show results from remote object , and also buttons to call methods on remote object ( Add, clear total ,Sum);

3-      As with the server code, register an instance of the TcpChannel class with the remoting infrastructure and activate the object in form load event as below :
ChannelServices.RegisterChannel(new TcpChannel());
      calc obj =  (calc)Activator.GetObject(
        typeof(_____.____),
        "tcp://localhost:___/_____");
4-      Build and test your client and then test your client-server solution. Convince yourself that the client only needs access to the calc.dll at runtime in order to work. Also convince yourself that Activator.GetObject does not involve any communication with the server, and that the server does not need to be running until a method call is placed.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using calc;


namespace Vdesai_Assignment1
{
    public partial class Client : Form
    {
        public Client()
        {
            InitializeComponent();
        }
        calc.Calculator cal;
        private void Client_Load(object sender, EventArgs e)
        {
            TotalTxt.Text = "0";
            TcpClientChannel channel = new TcpClientChannel();
            ChannelServices.RegisterChannel(channel, false);
            cal = (Calculator)Activator.GetObject(
               typeof(Calculator), "tcp://localhost:1234/calc");
        }

        private void AddBtn_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(Atxt.Text);
            int b = Convert.ToInt32(Btxt.Text);
            int c = cal.add(a, b);
            TotalTxt.Text = c.ToString();
        }

        private void SumBtn_Click(object sender, EventArgs e)
        {
            int y = Convert.ToInt32(SumTxt.Text );
            int x = Convert.ToInt32(TotalTxt.Text);
            TotalTxt.Text = cal.Sum(y).ToString();
        }

        private void ClearBtn_Click(object sender, EventArgs e)
        {
            Atxt.Text = string.Empty ;
            Btxt.Text = string.Empty;
            SumTxt.Text = string.Empty;
            TotalTxt.Text = string.Empty;
        }
    }
}






No comments:

Post a Comment