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.

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

In the example, the URI type of the remote object class used by the client <strong>and</strong> a mode are specifi ed. The<br />

mode WellKnownObjectMode.SingleCall means that a new instance is created for every method call;<br />

in the sample application no state is held in the remote object.<br />

.<strong>NET</strong> Remoting allows creating stateless <strong>and</strong> stateful remote objects. In the fi rst<br />

example, well - known single - call objects that don ’ t hold state are used. The other object<br />

type is called client - activated. Client - activated objects hold state. Later in this chapter,<br />

when looking at the object activation sequence, you learn more details about these<br />

differences <strong>and</strong> how these object types can be used.<br />

After registration of the remote object, it is necessary to keep the server running until a key is pressed:<br />

using System;<br />

using System.Collections.Generic;<br />

using System.Runtime.Remoting;<br />

using System.Runtime.Remoting.Channels;<br />

using System.Runtime.Remoting.Channels.Tcp;<br />

namespace Wrox.ProCSharp.Remoting<br />

{<br />

class Program<br />

{<br />

static void Main()<br />

{<br />

var channel = new TcpServerChannel(8086);<br />

ChannelServices.RegisterChannel(channel, true);<br />

RemotingConfiguration.RegisterWellKnownServiceType(<br />

typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);<br />

Console.WriteLine("Press return to exit");<br />

Console.ReadLine();<br />

}<br />

}<br />

}<br />

code snippet RemoteHello /HelloServer/Program.cs<br />

a simple Client application<br />

The client is again a <strong>C#</strong> console application: HelloClient . With this project, you also have to reference the<br />

System.Runtime.Remoting assembly so that the TcpClientChannel class can be used. In addition, you<br />

have to reference the RemoteHello assembly. Although the object will be created on the remote server, the<br />

assembly is needed on the client for the proxy to read the type information during runtime.<br />

In the client program, create a TcpClientChannel object that ’ s registered in ChannelServices .<br />

For the TcpChannel , you can use the default constructor, so a free port is selected. Next, the Activator<br />

class is used to return a proxy to the remote object. The proxy is of type System.Runtime.Remoting<br />

.Proxies.__TransparentProxy . This object looks like the real object because it offers the same methods.<br />

The transparent proxy uses the real proxy to send messages to the channel:<br />

using System;<br />

using System.Runtime.Remoting.Channels;<br />

using System.Runtime.Remoting.Channels.Tcp;<br />

namespace Wrox.ProCSharp.Remoting<br />

{<br />

class Program<br />

{<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!