.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 Web services. .NET Remoting is easier to use than Java's RMI, but 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.
This interface exposes 3 methods. The actual work is done by the server, But this is transparent to the client. What the client looks at is, are the methods and the interface that exposes these methods. Here is the complete source listing.
Compile this project to generate the Interface_CustomerInfo.DLL.
Part II. Creating the server (CustomerServer)
In Visual Studio.NET, Click on File->New Project has created a simple Windows Application.
You can also play around by creating a simple console application.
We need to Add a 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.
.Net Remoting supports 2 types of channels. For local applications or internetwork 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 TCP channel ( 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.
The first parameter is the object you're binding, type of ( (CUSTOMER_SERVER1 ). The second parameter is the String which 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.A 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 implement the methods that are exposed to the interface.
Note. The following declaration 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.
Compile the Server Object.
Part III. Creating the Client ( CustomerClient )
The CustomerClient object is the 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 returns rows from the database as a single string separated by a comma.
Note that we need to add a reference to our shared Interface_CustomerInfo.DLL created in Part I. Also, add a 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 second 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 an 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 transparent to the client when using the ICustomerInfo interface.
Here is the complete listing.
Run the Server, then Run the client. Type in a SQL statement ( against the NorthWind ) database.
NET remoting makes life very easy for us.
It's very simple to use and can provide an excellent way to work with resources across your network or the internet.