Ingo Rammer Technology Consulting
^ .NET Remoting FAQ
» IngoRammer.com
» My Books
» Conferences
» Consulting & Services
» Newsletter
 
 
 
» Contact
 

HOWTO: Publish a specific object-instance via Remoting

Question

How can I publish a certain object's instance? The reason I want to do this, is that I want to pass parameters using the object's constructor during server-startup.

i.e.

SomeObject obj = new SomeObject("Hello World");

// and now do-some-magic and publish this object 
// at http://localhost:1234/thisobject.soap

When a client now contacts http://localhost:1234/thisobject.soap, the call should be answered from this exact instance.

Answer

You can use RemotingServices.Marshal() to publish a specific instance.

The following piece of code will do exactly what you described in your question:

HttpChannel channel = new HttpChannel( 1234 ); 
ChannelServices.RegisterChannel( channel ); 

SomeObject obj = new SomeObject("Hello World"); 
RemotingServices.Marshal(obj,"thisobject.soap");

And by the way, if you want your object to live forever, don't forget to override InitializeLifetimeService() to return a "null" which means indefinite lifetime!

public class SomeObject : MarshalByRefObject 
{
  public override Object InitializeLifetimeService()
  {
    return null;
  }
} 






© 2002, 2003 by Ingo Rammer (ingo@ingorammer.com). Information is provided as-is and is subject to heavy changes due to its pre-release character.