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 that will return 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 |
Part I : Creating the Marshal Object .. CustomerLoader |
Click on File->New->Project. Choose to create a new "C# Library" and name it 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. If you create a separate object or a class that gets returned from this , you need to declare it as SERIALIZABLE. The namespace we're using for this particular object is CustomerInfo, the object that is of the type MarshalByRefObject. A marshaled object allows us to access objects across application domain boundaries in applications that support remoting. MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using a proxy. Objects that do not inherit from MarshalByRefObject are implicitly marshal by value. MarshalByRefObject objects are accessed directly within the boundaries of the local application domain. The first time an application in a remote application domain accesses a MarshalByRefObject, a proxy is passed to the remote application. Subsequent calls on the proxy are marshaled back to the object residing in the local application domain. |
Here is the complete souce listing … |
using System; using System.Text; using System.IO ; using System.Data.SqlClient; namespace CustomerInfo public CustomerLoader() public void Init(string userid , string password) if ( myConnection == null ) } public void ExecuteSelectCommand(string selCommand) myCommand.Connection = myConnection; public string GetRow() int nCol = myReader.FieldCount ; } |
Compile this project to generate the CustomerInfo.DLL to be added to the server and client assemblies. |
Part II :Creating the Server |
In Visual Studio.NET, Click on File->New Project. Choose a Command Line Application and name it CustServer. I have created a simple Windows Application. You can also play around by creating a simple console application. We need to Add Reference to the CustomerInfo.DLL that will make use of the Marshal Object. 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 ntranetwork 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 HttpChannel ( just for the heck of it !! ) .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. |
> Remoti |
The first parameter is the object you're binding, typeof ( CustomerLoad). 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/CustomerLoad". 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. |
The complete code for the object is below. |
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime ; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using CustomerInfo; namespace CustServer public Form1() // /// <summary> #region Windows Form Designer generated code } /// <summary> private void Form1_Load(object sender, System.EventArgs e) textBox1.Text = "HTTP Channel Registered …\r\nWaiting on Clients Connect" ; } |
Compile the Server Object. |
Part III :Creating the Client ( CustClient ) |
The CustClient object is our test object of our newly created CustServer 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 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 HTTPClient channel. This channel is not bound to a port. The seond line actually gets a reference to our remote CustomerLoad object. The Activator.GetObject method returns a type of Object that we can then cast into our CustomerLoad. 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.
|
ChannelServices.RegisterChannel( new HttpClientChannel()); custl = (CustomerLoader)Activator.GetObject(typeof(CustomerLoader ) , "http://localhost:8228/CustomerLoader"); if ( custl == null ) { Console.WriteLine("HTTP SERVER OFFLINE …PLEASE TRY LATER"); return ; } custl is an object of type CustomerLoader. |
Here is the complete listing … |
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime ; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using CustomerInfo; 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) |
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 .. ( A simple approach )
↧