spacer

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

home / programming / csharp / webservices / chap6 / 2 To page 1To page 2current pageTo 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?


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.


home / programming / csharp / webservices / chap6 / 2 To page 1To page 2current pageTo 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/3.html