.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
2.26.2002

 

Notes about Remoting and Events



I'm just getting started with remoting, checking out the Chat sample with the SDK. Shows simple chat clients that connect to chat server which simply broadcasts the incoming chats from all clients. Here are a few thoughts about the remoting infrastructure:

The Server
Amazed that the server process doesn't really have any code in it!! Just calls RemotingConfiguration.Configure("ChatCentral.exe.config"); then sits at a Console.ReadLine waiting for end of applicaiton. All configuration of the object that will run in the server process is contained in the config file.

wellknown
mode="Singleton"
type="ChatCoordinator, ChatCoordinator"
objectUri="Chat"

The above xml describes that the object called ChatCoordinator (actually defined in a separate c# class library file) will be instantiated upon first client method invocation (docs say this, not during the client call to "new") and be a singleton object -- all clients will get a reference to the same object.


channels
channel
ref="http"
port="8080"

This says that the server object will receive incoming calls on http port 8080. netstat -an shows that this port is open and listening right after the call to RemotingConfiguration.Configure( )

The Client
Client code is also pretty basic. The client creates a new object that is configured to run on the server. After the call to RemotingConfiguration.Configure("ChatClient.exe.config"); the runtime now knows that creations of the ChatCoordinator object are now to be remoted to the given URI. The callbacks must have their own channel - in this case we specify 0 to let the system choose a port.

wellknown
type="ChatCoordinator, ChatCoordinator"
url="http://localhost:8080/Chat"

channel
ref="http"
port="0"


The standard event infrastructure sets up the callbacks from the server to the client when there is a new chat message. The chat client object creates a delegate and assigns it to the public event in the ChatCoordinator object.

Interesting notes:

  • SubmitEventArgs derives from EventArgs and is used to pass custom data to event handlers. Must be serializable for remoting.
  • ChatClient must derive from MarshalByRefObject, so the delegate to call back on can be passed out of the AppDomain. Get a SerializationException if not.
  • If client connects to the wrong port or wrong URL, you get System.Net.WebException "unable to connect to remote server"


Comments: Post a Comment

Powered by Blogger