15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

emote objects, Clients, <strong>and</strong> servers ❘ OC197<br />

activation<br />

A new context is created if an instance of a class that’s created needs a context different from the calling<br />

context. The attribute classes that are associated with the target class are asked if all the properties of<br />

the current context are acceptable. If any of these properties are unacceptable, the runtime asks for all<br />

property classes associated with the attribute class <strong>and</strong> creates a new context. The runtime then asks<br />

the property classes for the sinks they want to install. A property class can implement one of the<br />

IContributeXXXSink interfaces to contribute sink objects. Several of these interfaces are available to go with<br />

the variety of sinks.<br />

attributes <strong>and</strong> Properties<br />

The properties of a context are defined with context attributes. A context attribute class is primarily an<br />

attribute — you can find out more about attributes in Chapter 14, “Reflection.” Context attribute classes<br />

must implement the interface IContextAttribute. A custom context attribute class can derive from the<br />

class ContextAttribute, because this class already has a default implementation of this interface.<br />

The .<strong>NET</strong> Framework includes two context attribute classes: System.Runtime.Remoting.Contexts.<br />

SynchronizationAttribute <strong>and</strong> System.Runtime.Remoting.Activation.UrlAttribute.<br />

The Synchronization attribute defines synchronization requirements; it specifies the synchronization<br />

property that is needed by the object. With this attribute you can specify that multiple threads cannot access<br />

the object concurrently, but the thread accessing the object can change.<br />

With the constructor of this attribute, you can set one of four values:<br />

➤<br />

➤<br />

➤<br />

NOT_SUPPORTED defines that the class should not be instantiated in a context where the<br />

synchronization is set.<br />

REQUIRED specifies that a synchronization context is required.<br />

REQUIRES_NEW always creates a new context.<br />

➤<br />

SUPPORTED means that it doesn’t matter what context you get; the object can live in it.<br />

Communication between Contexts<br />

How does the communication between contexts happen The client uses a proxy instead of the real object.<br />

The proxy creates a message that is transferred to a channel, <strong>and</strong> sinks can intercept. Does this sound<br />

familiar It ought to. The same mechanism is used for communication across different application domains<br />

or different systems. A TCP or HTTP channel is not required for the communication across contexts, but<br />

a channel is used here too. CrossContextChannel can use the same virtual memory in both the client <strong>and</strong><br />

server sides of the channel, <strong>and</strong> formatters are not required for crossing contexts.<br />

remoTe objeCTs, ClienTs,<br />

<strong>and</strong> serVers<br />

Before stepping into the details of the .<strong>NET</strong> Remoting<br />

architecture, this section looks briefly at a remote<br />

object <strong>and</strong> a very small, simple server <strong>and</strong> client<br />

application that uses this remote object. After that the<br />

required steps <strong>and</strong> options are discussed in more detail.<br />

Figure 54-3 shows the major .<strong>NET</strong> Remoting classes<br />

in the client <strong>and</strong> server application. The remote object<br />

that will be implemented is called Hello. HelloServer<br />

is the main class of the application on the server, <strong>and</strong><br />

HelloClient is used for the client.<br />

HelloClient<br />

+Main()<br />

figure 54-3<br />

MarshalByRefObject<br />

Hello<br />

+Greeting()<br />

HelloServer<br />

+Main()<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!