|
Location: Home > Resources
> PublishingObject
HOWTO: Publish a specific object-instance via Remoting
2002
Ingo Rammer
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;
}
}
|