Using Interfaces In .NET Remoting
Abstract
.NET Remoting allows for a clean seperation between client side code and server side code through the use of interfaces. This allows a developer to distribute generic interfaces to client machines and change the server side code without having to redistribute the code changes back to the clients.
In this article, we will create a remoteobject and referance it only by interface. We will also create aninterfaces DLL that both the client and server code use.
Step 1: Creating The Interface Library
Launch Visual Studio.NET Beta 2, thenchoose File->New->Project. Choose to create a new "C# Library"and name it RemotingExampleInterfaces then click on OK. The purpose forthis library is to declare the object interfaces that we will use onboth the client and server side.
The code for this library is very simple, but a few notes need to be placed on the code below.
First, instead of declaring classes we are declaring interfaces.This tells the compiler that we're not actually creating a class, weare creating a "template" for a class. An interface defines whatMethods the object should have as well as the parameters and returnvalues for these methods. Note the ';' at the end of each methoddeclaration instead of the '{}'s that would normally contain themethod's implementation. The final thing to mention on interfaces asthat they have no access modifiers such as public or private. That is defined by the class that we choose to implement our interface in.
| using System;
namespace RemotingExampleInterfaces public interface IResume public interface IRemotingExampleService |
Choose Build from the Build menu to compilethis DLL. The output will be placed in the bin/Debug subdirectory ofyou project directory.
Step 2: Create The Implementation And Server Object
Now that we have defined the interfaces weplan on using, we need to create an implementation of these interfacesthat does something. In a real project, you would create another sharedlibrary with you implementation to aid code reuse and keep your projectmodular. In this simpleexample, we'll just create a console application that provides aninline implementation of our interfaces.
Go to File->New->Project and choose C# Console application, then click on OK.
Since we are making use of theSystem.Runtime.Remoting namespace, we will need to add a referance tothe correct DLL. This is done by clicking Project->Add Referance,click on the ".NET" tab and chose System.Runtime.Remoting.dll.
We also need to add a referance to theinterfaces DLL we created in Step 1. This is done by choosingProject->Add Referance, click "Browse" andchoose the .DLL file in the bin/Debug directory of theRemotingExampleInterfaces project we created in Step 1.
| public class Resume : MarshalByRefObject, IResume public class RemotingExampleService : MarshalByRefObject, IRemotingExampleService |
Take a look at these two class declarations. Each is a subclass of the MarshalByRefObject type. This is veryimportant to the use of these objects in .NET remoting. Also, each ofthese objects is an implementation of the IResume andIRemotingExampleService interfaces we created in Step 1. Note that inthese classes are methods that match the names of the methods wedefined in our interface.
If you have questions about the TcpServerChannel or related topics, check out my article entitled, "Introduction to .NET Remoting" hosted on this site.
The complete code for the object is below.
| using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using RemotingExampleInterfaces; using System.Runtime.Serialization; namespace RemotingInterfaceServer public class RemotingExampleService : MarshalByRefObject, IRemotingExampleService public IResume GetResume() public String GetFormattedResume() [Serializable] public String GetFormattedResume() public String GetFormattedName() |
Compile this program by going toBuild->Build and note the location of the generated executable filefound in the /bin/debug subdirectory of this project.
Step 3: Create The Client
To create the client program, go to File->New->Project and choose a C# Console Application named RemotingExampleClient.
| IRemotingExampleService resService =(IRemotingExampleService)Activator.GetObject( typeof(IRemotingExampleService), "tcp://localhost:9988/RemotingExampleService"); Console.WriteLine("RESUME:\n"+ resService.GetFormattedResume()); |
Notice that we're using theIRemotingExampleService here instead of RemotingExampleService? That isbecause our client side code knows only about the interface, not theimplementation the server is using. The result of this is the server isserving "RemotingExampleService", but our client is using"IRemotingExampleService".
| IResume aResume = resService.GetResume(); Console.WriteLine("NAME:"+ aResume.GetFormattedName()); Console.WriteLine("RESUME:"+ aResume.GetFo rmattedR |
This section of code makes use of theinstance of IRemotingExampleService to return yet another interface.Here we don't have to make any calls to the Activator object becausethe .NET runtime accesses the server's Resume implementationtransparently using the IResume interface.
To make this code work, we will need to adda referance to both our RemotingExampleInterfaces andSystem.Runtime.Remoting dlls as we did in step 2. Without this you willget compiler errors.
The complete code listing is below. Formore information on the Activator object, see my article titled"Introduction to .NET Remoting" on this site.
| using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; using RemotingExampleInterfaces; namespace RemotingExampleClient |
Compile this project and note the location of the .EXE file.
Running the completed example
Run the server application we created in step 2 by double clicking the .exe file we created. Then run the client application by double clicking the .exe file created in step 3. If all goes well, you should see the formatted resume output.
Conclusion
Interfaces are the preferred way to access.NET remote objects. They enable the developer to create clean codewith a complete seperation between client side use of remote objectsand the server side implementation. In most cases it is a good idea touse a DLL for your interfaces, another DLL for your implementation inorder to maximize reuse potential.
|
David Talbot is an experienced Software Architect with a diversebackground including creating network applicances, working withtelevision set top boxes, building Billing/CRM systems, Web Portals andmore. He has also provided technical guidance in different capacitieson two C# books. David is currently finishing up work on a real estate analytics application in C# for Pathfinder Technologies and seeking additional contract or permenet work. |
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="468" height="60"><param name="movie" value="/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" /><param name="quality" value="high" /> <embed src="http://www.csharphelp.com/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="468" height="60"></embed> </object>












No comments yet... Be the first to leave a reply!