15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

OC226 ❘ ChaPTer 54 .net remOtinG<br />

Take a look at an example. To see all the parts of event h<strong>and</strong>ling with .<strong>NET</strong> Remoting, create five classes:<br />

Server, Client, RemoteObject, EventSink, <strong>and</strong> StatusEventArgs. The dependencies of these classes are<br />

shown in Figure 54-5.<br />

«serializable»<br />

StatusEventArgs<br />

+Message<br />

call h<strong>and</strong>ler<br />

on event<br />

«remotable»<br />

EvenSink<br />

+StatusH<strong>and</strong>ler()<br />

«remotable»<br />

RemoteObject<br />

+LongWorking()<br />

Client<br />

Server<br />

+Main()<br />

figure 54-5<br />

+Main()<br />

The Server class is a remoting server such as the one you are already familiar with. The Server class<br />

will create a channel based on information from a configuration file <strong>and</strong> register the remote object that’s<br />

implemented in the RemoteObject class in the remoting runtime. The remote object declares the arguments<br />

of a delegate <strong>and</strong> fires events in the registered h<strong>and</strong>ler functions. The argument that’s passed to the h<strong>and</strong>ler<br />

function is of type StatusEventArgs. The class StatusEventArgs must be serializable so that it can be<br />

marshaled to the client.<br />

The Client class represents the client application. This class creates an instance of the EventSink class<br />

<strong>and</strong> registers the StatusH<strong>and</strong>ler() method of this class as a h<strong>and</strong>ler for the delegate in the remote object.<br />

EventSink must be remotable like the RemoteObject class, because this class will also be called across<br />

the network.<br />

remote object<br />

The remote object class is implemented in the file RemoteObject.cs. The remote object class must<br />

be derived from MarshalByRefObject, as you already know from the previous examples. To enable<br />

the client to register an event h<strong>and</strong>ler that can be called from within the remote object, you have to<br />

declare an external function with the delegate keyword. Declare the delegate StatusEvent() with two<br />

arguments: the sender (so the client knows about the object that fired the event) <strong>and</strong> a variable of type<br />

StatusEventArgs. Into the argument class you can put all the additional information that you want to send<br />

to the client.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!