Allow the server to access the same object which is remoted to the clients
is a frequently occurring problem in remoting. Luckily, the answer is pretty
simple. Currently, you've probably registered your remoting object using
RemotingConfiguration.RegisterWellKnownServerObject(). The problem is that
this method exposes the object anonymously, i.e. the server doesn't have a
reference to this instance.
To fix this, you do two things:
1) Declare and instantiate a named instance of your remote object.
2) Get rid of your call to RegisterWellKnownServerObject() and instead call
RemotingServices.Marshal() on your named object. This will expose the object
so it can accept client requests.
Hope that helps -
Ken
> Hello!
> I have a remoting server which exposes a Singleton object to a client.
[quoted text clipped - 9 lines]
>
> Thanks
Rob Richardson - 16 Dec 2004 19:49 GMT
Ken,
Thanks for your reply. I am a newbie in this area, and do not know about
named objects. I haven't found a clear sample on the Web. Could you point
me to a sample of this technique?
Thanks again!
Rob
Rob Richardson - 16 Dec 2004 21:08 GMT
Ken,
I tried using RemotingServices.Marshal() as you suggested. It worked!
In the server, I replaced:
RemotingConfiguration.RegisterWellKnownServiceType (typeof (Clock), "Clock",
WellKnownObjectMode.SingleCall);
with:
Clock serverClock = new Clock();
serverClock.Number = 99;
ObjRef serverClockRef = RemotingServices.Marshal(serverClock,
"ServerClockURI");
In the client, I replaced:
RemotingConfiguration.RegisterWellKnownClientType (typeof (Clock),
"tcp://localhost:1234/Clock");
with:
RemotingConfiguration.RegisterWellKnownClientType (typeof (Clock),
"tcp://192.168.1.29:1234/ServerClockURI");
The client application happily reported that it was using clock number 99.
Next question: For my application as it is currently designed, this will
suffice, since there will never be more than a single client communicating
with a server. But what happens when we decide that a single server
application will manage an unknown number of printers? In that case, I
would want my objects to be client-activated, but I still want the server to
know what objects it created. How do I do that?
Thanks again!
Rob
Ken Kolda - 16 Dec 2004 21:50 GMT
In this case, you probably want to go with a factory model. In this model,
you create a singleton SAO which serves up your CAOs. By doing this you're
able to keep a hold of references to the same CAOs used by the clients. For
a good explanation of implementing a factory model in remoting, check out:
http://www.glacialcomponents.com/ArticleDetail/CAOGuide.aspx
Ken
> Ken,
>
[quoted text clipped - 28 lines]
>
> Rob