Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Create a Remote Server?

This sample demonstrates how to create the remoting version of a Hello World server. When a client calls the HelloMethod on the HelloServer class, the server object appends the string passed from the client to "Hi There" and returns the resulting string back to the client. The following code example demonstrates both the server object and the server application.

		
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples {
    public class Sample {
        public static int Main(string [] args) {
            TcpChannel chan = new TcpChannel(8085);
            ChannelServices.RegisterChannel(chan);
            RemotingConfiguration.RegisterWellKnownServiceType(
                Type.GetType("RemotingSamples.HelloServer,object"),
                "SayHello", WellKnownObjectMode.SingleCall);
            System.Console.WriteLine("Hit  to exit...");
            System.Console.ReadLine();
            return 0;
        }
    }
}
C#

		
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples {
    public class HelloServer : MarshalByRefObject {
        public HelloServer() {
            Console.WriteLine("HelloServer activated");
        }

        public String HelloMethod(String name) {
            Console.WriteLine("Hello.HelloMethod : {0}", name);
            return "Hi there " + name;
        }
    }
}
C#

The class HelloServer is derived from MarshalByRefObject to make it remotable. When the server is started, you need to create and register a TCP channel that will listen for clients to connect on port 8085. You also need to register the remote object with the remoting framework by calling RegisterWellKnownType. The parameters for this call are as follows:

  1. The name of the assembly that contains the object being registered. Since the object is contained in an executable, you provide the name of the executable here. Both server and server.exe will work since the normal rules of locating an assembly apply.

  2. The next parameter is the full type name of the object being registered, in this case "RemotingSamples.HelloServer". You need to specify both the name of the namespace and the classname here. If you don't use namespaces, you pass the classname.

  3. Then, you provide the name of the endpoint where the object will be published. Clients need to know this name in order to connect to the object. Any string will work; for this example, use "SayHello". It is also possible to connect to remote objects using ASP.NET and if this is a requirement, the name of the endpoint should be namespace/class.soap. For this example, the endpoint will be RemotingSamples/HelloServer.soap.

  4. The final parameter specifies the object mode, which can be SingleCall or Singleton. For this example, you specify SingleCall. 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 will be created for each call made from a client, even if the same client calls the same method more than once. On the other hand, Singleton objects are created once only and all clients communicate with the same object.
Now you can compile this example and execute it.

		
csc /debug+ /r:System.Runtime.Remoting.dll /r:Object.dll server.cs
C#

The class Sample should be complied as an executable and the class HelloServer would have be a separate dll.

When you start the server, the object will be created as part of the registration process so the framework can extract the relevant metadata from the object. After registration, this object is destroyed and the framework starts listening for clients to connect on the registered channels.

VB Hello Server Sample
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\HowTo\Samples\Remoting\Hello\]




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.