Using Interfaces in .NET Remoting

.NET Remoting allows for a clean separation 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 remote object and reference it only by the interface. We will also create an interfaces DLL that both the client and server code use.
Step 1: Creating The Interface Library
Launch Visual Studio.NET Beta 2, then choose File->New->Project. Choose to create a new "C# Library" and name it RemotingExampleInterfaces then click on OK. The purpose of this library is to declare the object interfaces that we will use on both 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, we are creating a "template" for a class. An interface defines what Methods the object should have as well as the parameters and return values for these methods. Note the ';' at the end of each method declaration instead of the '{}'s that would normally contain the method's implementation. The final thing to mention on interfaces is that they have no access modifiers such as public or private. That is defined by the class that we choose to implement our interface in.
  1. using System;  
  2. namespace RemotingExampleInterfaces {  
  3.       public interface IResume {  
  4.             String GetFormattedResume();  
  5.             String GetFormattedName();  
  6.       }  
  7.       public interface IRemotingExampleService {  
  8.             IResume GetResume();  
  9.             String GetFormattedResume();  
  10.       }  
Choose Build from the Build menu to compile this DLL. The output will be placed in the bin/Debug subdirectory of you project directory.
Step 2: Create The Implementation And Server Object
Now that we have defined the interfaces we plan on using, we need to create an implementation of these interfaces that does something. In a real project, you would create another shared library with your implementation to aid code reuse and keep your project modular. In this simple example, we'll just create a console application that provides an inline implementation of our interfaces.
Go to File->New->Project and choose C# Console application, then click on OK.
Since we are making use of the System.Runtime.Remoting namespace, we will need to add a referance to the correct DLL. This is done by clicking Project->Add Reference, click on the ".NET" tab and chose System.Runtime.Remoting.dll.
We also need to add a reference to the interfaces DLL we created in Step 1. This is done by choosing Project->Add Referance, click "Browse" and choose the .DLL file in the bin/Debug directory of the RemotingExampleInterfaces project we created in Step 1.
  1. public class Resume : MarshalByRefObject, IResume  
  2. public class RemotingExampleService : MarshalByRefObject, RemotingExampleService 
Take a look at these two class declarations. Each is a subclass of the MarshalByRefObject type. This is very important to the use of these objects in .NET remoting. Also, each of these objects is an implementation of the IResume and IRemotingExampleService interfaces we created in Step 1. Note that in these classes are methods that match the names of the methods we defined 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.
  1. using System;  
  2. using System.Runtime.Remoting;  
  3. using System.Runtime.Remoting.Channels;  
  4. using System.Runtime.Remoting.Channels.Tcp;  
  5. using RemotingExampleInterfaces;  
  6. using System.Runtime.Serialization;  
  8. namespace RemotingInterfaceServer {  
  9.       public class EntryPoint {  
  10.             public static void Main(string[] args) {  
  11.                   TcpServerChannel channel = new TcpServerChannel(9988);  
  12.                   ChannelServices.RegisterChannel(channel);  
  13.                   RemotingConfiguration.RegisterWellKnownServiceType(typeof emotingExampleService), "RemotingExampleService", WellKnownObjectMode.SingleCall);  
  14.             System.Console.WriteLine("Press Any Key");  
  15.             System.Console.ReadLine();  
  16.       }  
  17. }  
  18. public class RemotingExampleService: MarshalByRefObject, IRemotingExampleService {  
  19.             public RemotingExampleService() {}  
  20.             public IResume GetResume() {  
  21.                   Resume resume = new Resume();  
  22.                   resume.FirstName = "David";  
  23.                   resume.LastName = "Talbot";  
  24.                   resume.Title = "Senior Software Architect";  
  25.                   resume.Body = "Did some cool stuff";  
  26.                   return (IResume) resume;  
  27.             }  
  28.             public String GetFormattedResume() {  
  29.                   return GetResume().GetFormattedResume();  
  30.             }  
  31.       }  
  32.       [Serializable]  
  33.       public class Resume: MarshalByRefObject, IResume {  
  34.             public String FirstName, LastName, Body, Title;  
  35.             public String GetFormattedResume() {  
  36.                   System.Text.StringBuilder sb = new System.Text.StringBuilder();  
  37.                   sb.Append("*" + GetFormattedName() + "*\n");  
  38.                   sb.Append("--" + Title + "--\n");  
  39.                   sb.Append("--------------------------\n");  
  40.                   sb.Append(Body);  
  41.                   return sb.ToString();  
  42.             }  
  43.             public String GetFormattedName() {  
  44.                   return FirstName + " " + LastName;  
  45.             }  
  46.       }  
  47.       //END OF Resume Object  
Compile this program by going to Build->Build and note the location of the generated executable file found 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.
  1. IRemotingExampleService resService =(IRemotingExampleService)Activator.GetObject(typeof(IRemotingExampleService),"tcp://localhost:9988/RemotingExampleService");  
  2. Console.WriteLine("RESUME:\n"+ resService.GetFormattedResume());  
Notice that we're using the IRemotingExampleService here instead of RemotingExampleService? That is because our client-side code knows only about the interface, not the implementation the server is using. The result of this is the server is serving "RemotingExampleService", but our client is using "IRemotingExampleService".
  1. IResume aResume = resService.GetResume();  
  2. Console.WriteLine("NAME:"+ aResume.GetFormattedName());  
  3. Console.WriteLine("RESUME:"+ aResume.GetFormattedResume()); 
This section of code makes use of the instance of IRemotingExampleService to return yet another interface. Here we don't have to make any calls to the Activator object because the .NET runtime accesses the server's Resume implementation transparently using the IResume interface.
To make this code work, we will need to add a reference to both our RemotingExampleInterfaces and System.Runtime.Remoting dlls as we did in step 2. Without this, you will get compiler errors.
The complete code listing is below. For more information on the Activator object, see my article titled "Introduction to .NET Remoting" on this site.
  1. using System;  
  2. using System.Runtime.Remoting;  
  3. using System.Runtime.Remoting.Channels.Tcp;  
  4. using System.Runtime.Remoting.Channels;  
  5. using RemotingExampleInterfaces;  
  6. namespace RemotingExampleClient {  
  7.       class RemotingExampleClient {  
  8.             static void Main(string[] args) {  
  9.                   ChannelServices.RegisterChannel(new TcpClientChannel());  
  10.                   IRemotingExampleService resService = (IRemotingExampleService) Activator.GetObject(typeof(IRemotingExampleService), "tcp://localhost:9988/RemotingExampleService");  
  11.                   Console.WriteLine("RESUME:\n" + resService.GetFormattedResume());  
  12.                   IResume aResume = resService.GetResume();  
  13.                   Console.WriteLine("NAME:" + aResume.GetFormattedName());  
  14.                   Console.WriteLine("RESUME:" + aResume.GetFormattedResume());  
  15.                   Console.WriteLine("Press any key to continue...");  
  16.                   Console.Read();  
  17.             } //END OF MAIN METHOD  
  18.       } //END OF RemotingExampleClient class  
  19. //END OF RemotingExampleClient namespace 
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.


Interfaces are the preferred way to access .NET remote objects. They enable the developer to create clean code with a complete separation between the client-side use of remote objects and the server-side implementation. In most cases it is a good idea to use a DLL for your interfaces, another DLL for your implementation in order to maximize reuse potential.