spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / csharp / webservices / chap6 / 2 To page 1current pageTo page 3To page 4To page 5To page 6To page 7To page 8
[previous] [next]

Professional C# Web Services

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


Nothing has really changed with the server and the remote object, so we can use the same client application we created earlier to communicate with the new server. To complete the picture, we will change the client code as can be seen below:

// SimpleClient.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Wrox.Samples
{
   class SimpleClient
   {
      static void Main(string[] args)
      {

         // Create and register the client channel

         TcpClientChannel channel = new TcpClientChannel();
         ChannelServices.RegisterChannel(channel);

         // Register the name and port of the remote object

         WellKnownClientTypeEntry entry = new WellKnownClientTypeEntry(
            typeof(MyRemoteObject), 
            "tcp://localhost:9000/SimpleServer/MyRemoteObject");
         RemotingConfiguration.RegisterWellKnownClientType(entry);

Now a client channel from the class TcpClientChannel is instantiated and registered. We use the WellKnownClientTypeEntry class to define the remote object. In contrast to the server version of this class, the complete path to the remote object must be specified here as we have to know the server name in the client, but the mode is not specified because this is defined from the server. The RemotingConfiguration class is then used to register this remote object in the remoting runtime.

As we saw earlier, the remote object can be instantiated with the new operator. To demonstrate that a well-known object gets activated with every method call, the method Hello() is now called five times in a for loop:

         MyRemoteObject obj = new MyRemoteObject(); 
         for (int i=0; i < 5; i++)
            Console.WriteLine(obj.Hello());
      }
   }
}

Running this program we can see in the output of the server (see screenshot below) that a new instance of the remote object is not created by calling the new operator in the client code, but with every method call instead. Well-known objects could also be called server-activated objects as compared to client-activated objects. With client-activated objects a new object gets instantiated on the server when the client invokes the new operator:

results of example code run

One important fact that we should remember with well-known objects:

A well-known object doesn't have state. A new instance is created with every method call.


home / programming / csharp / webservices / chap6 / 2 To page 1current pageTo page 3To page 4To page 5To page 6To page 7To page 8
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

Created: February 25, 2002
Revised: February 25, 2002


URL: http://webreference.com/programming/csharp/webservices/chap6/2/2.html