SUMMARY
This article illustrates how to create a simple, remote server that another application can access. The application that accesses this server can be located on the same computer, on a different computer, or on a different network. The remote server is broken into two parts: the server object and the server application. The server object is the object that the client communicates with, and the server application is used to register the server object with the Remoting run-time framework.
back to the top
Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
- Visual Studio .NET
- Visual Studio 2005
This article assumes that you are familiar with the following topics:
- Visual Studio .NET or Visual Studio 2005
- Visual C# .NET or Visual C# 2005
- Networking
back to the top
Creating the remote server object
The first step in creating the server application is to create a server object. The server object is what the client application instantiates and communicates with on the server computer. The client application does this through a proxy object that is created on the client. In this sample, the server object resides in a Class Library (DLL) and is called
myRemoteClass.
- Create a new Class Library application in Visual C# .NET. Name the project ServerClass. Class1 is created by default.
- In Solution Explorer, rename the Class1.cs code file to ServerClass.cs.
- Open ServerClass.cs and rename Class1 to myRemoteClass. You also need to rename the default constructor for the class to so that it matches the class name. myRemoteClass should inherit from the MarshalByRefObject class. Your class should now appear as follows:
public class myRemoteClass: MarshalByRefObject
{
public myRemoteClass()
{
//
// TO DO: Add constructor logic here.
//
}
}
- Add a public method to myRemoteClass that takes a string, displays a message to the console with a value of the string, and returns True if the string is not empty.
public bool SetString(String sTemp)
{
try
{
Console.WriteLine("This string '{0}' has a length of {1}", sTemp, sTemp.Length);
return sTemp != "";
}
catch
{
return false;
}
}
- Build the project to create the ServerClass.dll assembly.
- Save and close the project.
Note In Visual C# 2005, Program.cs is created instead of Class1.cs by default.
back to the top
Creating the remote server application
After you have created the server object that the client will communicate with, you must register this object with the Remoting framework. When you register the object, you must also start the server and have the server listen on a port for clients to connect to that port. To do this, you need a project type that outputs an executable file.
The reason to include the server object in a separate project is so that you can easily reference the server object from the client project. If you include it in this project you cannot reference it, because references can only be set to DLL files.
- Create a new Console Application in Visual C# .NET to start the remote server. Class1 is created by default.
- In Solution Explorer, rename the Class1.cs file to RemoteServer.cs.
- Add a reference to the System.Runtime.Remoting namespace.
- Add a reference to the ServerClass.dll assembly that you created in the previous section.
- Use the using statement on the Remoting, Remoting.Channels, and Remoting.Channels.TCP namespaces so that you are not required to qualify declarations in those namespaces later in your code. You must use the using statement prior to any other declarations.
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
- Declare the appropriate variable. Declare and initialize a TcpChannel object that listens for clients to connect on a certain port, which is port 8085 in this example. Use the RegisterChannel method to register the channel with the channel services. Add the following declaration code in the Main procedure of Class1:
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
- Call the RegisterWellKnownType method of the RemotingConfiguration object to register the ServerClass object with the remoting framework, and specify the following parameters:
- The full type name of the object that is being registered (which is ServerClass.myRemoteClass in this example), followed by the assembly name ServerClass. Specify both the name of the namespace as well as the class name here. Because you did not specify a namespace in the previous section, the default root namespace is used.
- Name the endpoint where the object is to be published as RemoteTest. Clients need to know this name in order to connect to the object.
- Use the SingleCall object mode to specify the final parameter. The object mode specifies the lifetime of the object when it is activated on the server. In the case of SingleCall objects, a new instance of the class is created for each call that a client makes, even if the same client calls the same method more than once. On the other hand, Singleton objects are created only once, and all clients communicate with the same object.
RemotingConfiguration.RegisterWellKnownServiceType(
System.Type.GetType("ServerClass.myRemoteClass, ServerClass"),
"RemoteTest",
WellKnownObjectMode.SingleCall);
- Use the ReadLine method of the Console object to keep the server application running.
System.Console.WriteLine("Hit <enter> to exit...");
System.Console.ReadLine();
- Build your project.
- Save and close the project.
Note In Visual C# 2005, Program.cs is created instead of Class1.cs by default.
back to the top
Testing the server object
For additional information about how to create a client application that communicates with the server object that you just created, click the following article number to view the article in the Microsoft Knowledge Base:
300943
How to create client access to remote server by using Visual Basic .NET
back to the top
REFERENCES
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
301116
How to marshal an object to a remote server by reference by using Visual Basic .NET
301112 How to marshal an object to a remote server by reference by using Visual Basic .NET
For additional information about the
TcpChannel class, visit the following Microsoft Developer Network (MSDN) Web site:
For additional information about the
RemotingConfiguration.RegisterWellKnownServiceType method, visit the following MSDN Web site:
For additional information about Quickstart Tutorials, visit the following gotodotnet.com Web site:
For an overview of .NET Remoting, see the .NET Framework Developer's Guide documentation.
For additional information about MSDN Discussion Groups and Visual C# .NET from the Usenet newsgroup, visit the following Microsoft Web site:
back to the top