Source Code : NetRemoting |
.NET Remoting provides a powerful and high performance way of working with remote objects. Architecturally, .NET Remote objects are a perfect fit for accessing resources across the network without the overhead posed by SOAP based WebServices. .NET Remoting is easier to use than Java's RMI, but definately more difficult than creating a WebService. In this article, we will create a remote object, and access this object using the Interface. The object returns rows from a database table. For the sake of simplicity i have used the NorthWind database that is packed with the installation of the Microsoft SQL Server. |
Developed using the Release version of Microsoft .NET Visual Studio |
For my previous article in NetRemoting. Visit NetRemoting ( A Simple Approach ) |
In this example we will create a remote object and access it only by the interface. |
Part I : Creating the Interface Library .. Interface_CustomerInfo |
Click on File->New->Project. Choose to create a new "C# Library" and name it Interface_CustomerInfo then click on OK. This will create the "shared vocabulary" that both our .NET Remote client and Server will use to communicate. Any database activity, happens on the server side. The important thing to note here is that , we define an object ICustomerInfo of the type Interface. public interface ICustomerInfo This interface exposes 3 methods. The actual work is done by the server, But this is transaparent to the client. What the client looks at is, are the methods and the interface that exposes these methods.Here is the complete souce listing … |
using System;
namespace Interface_CustomerInfo |
Compile this project to generate the Interface_CustomerInfo.DLL. |
Part II :Creating the Server ( CustomerServer ) |
In Visual Studio.NET, Click on File->New Project have created a simple Windows Application. You can also play around by creating a simple console application. We need to Add Reference to the Interface_CustomerInfo.DLL that will make use of the Interface. Click on the Project->Add Reference, and add a reference to the DLL that we created in Part I by clicking the "Browse" button. In order to use the .NET remote functionality, you must add a reference to the System.Runtime.Remoting.DLL using Project->Add Reference under the .NET tab. |
hts = new HttpServerChannel(8228); ChannelServices.RegisterChannel(hts); |
Net Remoting supports 2 types of channels . For local applications or intranetwork applications you can use the TcpChannel for better performance. The other type , the HttpChannel can be used for internet applications In this example I am using the TcpChannnel ( just for the heck of it !! ) .The previous application i mentioned above uses an HttpChannel. If you're working over the internet HTTP can sometimes be the only choice depending on firewall configurations This will set up the port number we want our object to respond to requests on, and the ChannelServices.RegisterChannel will bind that port number to the stack on the operating system. |
RemotingConfiguration.RegisterWellKnownServiceType(typeof(CUSTOMER_SERVER1) , "CUSTOMER_SERVER1" , WellKnownObjectMode.Singleton); |
The first parameter is the object you're binding, typeof ( (CUSTOMER_SERVER1 ). The second parameter is the String that is the name for the object on the TCP or HTTP channel. For example, remote clients would refer to the above object as "http://localhost:8228/CUSTOMER_SERVER1". The third parameter tells the container what should be done with the object when a request for the object comes in. WellKnownObjectMode.Single call makes a new instance of the object for each client WellKnownObjectMode.Singleton uses one instance of the object for all callers. It makes sense to use the Singleton option here since we have to maintain a unique database connection that can be used across clients and SQL calls. Important : The server class will actually implement the methods that are exposed from the interface. Note the following decleration of the Customer_Server1 class
This class inherits from the MarshalByRefObject object and ICustomerInfo interface that we have created in the interface DLL in step 1. |
The complete code for the Server object is below. |
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text; using System.IO ; using System. namespace CustomerServer public CUSTOMER_SERVER1() { } public void Init(string userid, string password) if ( myConnection == null ) } public bool ExecuteSelectCommand(string selCommand) myCommand.Connection = myConnection; public string GetRow() int nCol = myReader.FieldCount ; object [] values = new Object[nCol]; /// <summary> public Form1() // /// <summary> #region Windows Form Designer generated code } /// <summary> private void Form1_Load(object sender, System.EventArgs e) |
Compile the Server Object. |
Part III :Creating the Client ( CustomerClient ) |
The CustomerClient object is our test object of our newly created CustomerServer remote object. Create a new project by clicking File->New->Project. I have created a simple windows client that connects to the remote object , executes an SQL command and retuns rows from the database as a single string separated by comma. Note that we need to add a reference to our shared Interface_CustomerInfo.DLL created in Part I. Also add reference to the System.Runtime.Remoting DLL using the References->AddRefrences option in Solution explorer. Now Notice the 2 important lines in the program. The first line creates a TcpClientChannel. This channel is not bound to a port. The seond line actually gets a reference to our server CUSTOMER_SERVER1 object. The Activator.GetObject method returns a type of Object that we can then cast into our CUSTOMER_SERVER1. The parameters we pass in are extremely similar to what we passed to the RemotingConfiguration object on the server project. The first parameter is the Type of the object, the second is the URI of our remote object. Notice that we are using a ICustomerInfo object. The server is serving the CUSTOMER_SERVICE1 object but we are using the ICustomerInfo object. When you are calling the Init method of the interface , the implemented version of the method , handled by the server is activated. This process is completely transaprent to the client when using the ICustomerInfo interface. |
ChannelServices.RegisterChannel( new TcpClientChannel()); custl = (ICustomerInfo)Activator.GetObject(typeof(ICustomerInfo) , "tcp://localhost:8228/CUSTOMER_SERVER1"); if ( custl == null ) { Console.WriteLine("TCP SERVER OFFLINE …PLEASE TRY LATER"); return ; } custl.Init("skulkarni" , "" ) ; |
Here is the complete listing … |
using System; using System.Drawing; using System.Collections; namespace CustomerClient private System.Windows.Forms.TextBox textBox1; public Form1() // /// <summary> #region Windows Form Designer generated code } /// <summary> private void Form1_Load(object sender, System.EventArgs e) private void button2_Click(object sender, System.EventArgs e) private void button1_Click(object sender, System.EventArgs e) |
Run the Server , then Run the client. Type in a SQL statemtent ( against the NorthWind ) database. NET remoting makes life very easy for us. Its very simple to use and can provide an excellent way to work with resources across your network or the internet. |
↧
NET REMOTING .. The Interface Approach
↧