WebReference.com - Part 2 of Chapter 6: Professional C# Web Services, from Wrox Press Ltd (3/8)

To page 1To page 2current pageTo page 4To page 5To page 6To page 7To page 8
[previous] [next]

Professional C# Web Services

Activator.GetObject

Instead of using the new operator to instantiate remote objects, we can use the Activator class. Behind the scenes in the implementation of the new operator this class is used not only for remote but also for local object instantiations. For well-known objects the static method GetObject() returns a proxy to the remote object.

Using Activator.GetObject() instead of the new operator we no longer need to register the well known client type, because the URL to the remote object must be passed as argument to the GetObject() method:

         // Create and register the client channel
         TcpClientChannel channel = new TcpClientChannel();
         ChannelServices.RegisterChannel(channel);
         MyRemoteObject obj = (MyRemoteObject)Activator.GetObject(
            typeof(MyRemoteObject), 
            "tcp://localhost:9000/SimpleServer/MyRemoteObject");

Whether we use the new operator or the Activator.GetObject() method is just a matter of choice. The new operator is easier to use and hides the fact that we deal with remote objects. GetObject() is nearer to the reality as the name of this method clearly shows that a new object does not get instantiated. We already know that with both versions when we use well-known objects a proxy is returned, and no connection to the server happens at this time.

Singletons

Well-known objects can be in SingleCall mode or Singleton. With the SingleCall mode an object is created with every method call; with a Singleton only one object is created in all. Singleton objects can be used to share information between multiple clients.

Using a configuration file the only change that must be done to the server is setting the mode attribute of the <wellknown> element to Singleton:

      <wellknown 
         mode="Singleton" 
         type="Wrox.Samples.MyRemoteObject, MyRemoteObject" 
         objectUri="MyRemoteObject" />

Without using a configuration file, the enumeration WellKnownObjectMode.SingleCall has to be replaced with WellKnownObjectMode.Singleton:

WellKnownServiceTypeEntry remObj = new WellKnownServiceTypeEntry(
         typeof(MyRemoteObject), 
         "SimpleServer/MyRemoteObject",
         WellKnownObjectMode.Singleton);
      RemotingConfiguration.RegisterWellKnownServiceType(remObj);

No changes are required for the client.

Multiple threads are used on the server to fulfill concurrent requests from clients. We have to develop this remote object in a thread-safe manner. For more about thread issues see the Wrox book Professional C#.

One more aspect of singletons that must be thought of is scalability. A Singleton object can't be spread across multiple servers. If scalability across multiple servers may be needed, don't use singletons.


To page 1To page 2current pageTo page 4To page 5To page 6To page 7To page 8
[previous] [next]

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


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