Well, this is a very simple Example to implement the remoting concept.
It consists of 3 parts:
-
Business Component.
-
Server application
-
Client application
I have used the Northwind database of the SQL Server in the business logic layer. The code works OK.
How to execute?
-
Copy the server folder on one Windows XP SP2 Machine
-
Copy the client folder on the same Windows XP SP2 machine
-
Click the exe in the server folder
-
Click the exe in the Client folder.
The Client exe will execute as long as the server exe is running. The moment the server exe is stopped, the client will generate an exception.
//Note: if you are using 2 machines, specify the IP address of the server machine in place of the localhost.
For 2 different machines, copy the client folder to one machine, and the server folder to the other machine.
//pwd is 1234: Use the password of your system. The Northwind database must be installed.
//You can specify the IP Address of the
//server computer in place of.
-
Open a class library project.Name it as remlibrary.
-
Type this code in the class file.
- using System;
- using System.Data;
- using System.Data.SqlClient;
- public class product:MarshalByRefObject
- {
- public DataSet GetDataFromDatabase(int catID)
- {
- SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=northwind");
- SqlDataAdapter da = new SqlDataAdapter("select productname,unitprice from products where categoryid=@a", cn);
- da.SelectCommand.Parameters.AddWithValue("@a", catID);
- DataSet ds = new DataSet();
- da.Fill(ds, "abc");
- return ds;
- }
- }
Server application:
-
Open a Console Application.Name it as ConsoleApplicationrem.
-
Add a reference to System.RunTime.Remoting.dll
-
Add a reference to the remlibrary.dll
-
-
-
-
-
-
-
-
- using System;
-
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- using remlibrary;
- namespace ConsoleApplicationrem {
- public class Server {
- public static void Main() {
- TcpChannel h = new TcpChannel(6);
- ChannelServices.RegisterChannel(h, false);
- RemotingConfiguration.RegisterWellKnownServiceType(typeof(product),
- "RemoteObjects", WellKnownObjectMode.Singleton);
- Console.WriteLine("The Server hasstarted");
- Console.WriteLine("Press the enter keyto stop the server ...");
- Console.ReadLine();
- }
- }
- }
For the Client application.
You must add a reference to the remlibrary.dll;
- 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 remlibrary;
- namespace WindowsFormsApplicationrem
- {
- public partial class Form1 : Form
- {
-
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
-
- product remoteObject = (product)Activator.GetObject(typeof
- (product), "tcp://localhost:6/RemoteObjects");
- int a=Convert.ToInt32(textBox1.Text);
- DataSet ds=remoteObject.GetDataFromDatabase(a);
- dataGridView1.DataSource = ds.Tables[0];
- }
- }
- }