Getting at Service object from remoted object?

G

Grant Schenck

Hello,

I have a Windows Service developed in C# .NET. I'm making it a remote
server and I can, via an IPC Channel, expose methods and call them from a
client. However, I now want my remoted object to be able to invoke a method
on my server object and given that the object is built in a C# Class DLL
shared between the client and server I'm not sure how to get access to the
server object from the remoted object.

So, what techniques are commonly used to allow this?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Grant,

The object you expose through remoting is an object, nothing more,
nothing less. To that end, you can expose properties, methods, fields, etc,
etc, that will allow it to access the server, assuming that your object has
a reference to it.

So how does your object get access to the server? This is the same as
asking how a form has access to another form. You set a reference through a
method, the constructor, a field, or a property.

The only consideration here is that if you expose a property with your
service object through remoting, is it going to be marshaled by value, or by
reference? Something tells me you will want it to be marshaled by
reference, in which case you will want to derive your server class from
MarshalByRefObject.

Not seeing your service object design, I can't tell if it is a good idea
to expose the whole object. You might be exposing things you don't want to.

Hope this helps.
 
G

Grant Schenck

Hi,

See below.

Thanks, Grant Schenck

----- Original Message -----
From: "Nicholas Paldino [.NET/C# MVP]" <[email protected]>
To: <[email protected]>
Sent: Wednesday, September 13, 2006 8:41 AM
Subject: Re: Getting at Service object from remoted object?

Grant,

The object you expose through remoting is an object, nothing more, nothing
less. To that end, you can expose properties, methods, fields,
etc, etc, that will allow it to access the server, assuming that your
object
has a reference to it.

[GRANT] Right, I need a reference to my Service object.
So how does your object get access to the server? This is the same
as asking how a form has access to another form. You set a reference
through a method, the constructor, a field, or a property.

[GRANT] If I create a form from one form, I can pass the first form to the
second form. However, the remoting object isn't created until the client
invokes the remoted object method so I'm confused as to how the server could
get access to the object between when it is created and when the method is
invoked. Part of my confusion is I'm not clear how my remoted object, built
as a C# DLL class, can be able to call methods in my Service object.
The only consideration here is that if you expose a property with
your service object through remoting, is it going to be marshaled by
value,
or by reference? Something tells me you will want it to be marshaled by
reference, in which case you will want to derive your server class from
MarshalByRefObject.

[GRANT] Are you saying I should derive my Service class from
MarshallByRefObject (as well as ServiceBase) and somehow this object would
be a singleton for all clients, i.e., my remoted object IS my service???
Not seeing your service object design, I can't tell if it is a good
idea to expose the whole object. You might be exposing things you don't
want
to.

[GRANT] Actually I don't. My goals seem simple.

- I have a service running (controls a phone system but not particularly
relevent)
- I want to remote a single method (MakeCall)
- Client applications will invoke this method and pass a few parameters.
- Service will "get" these requests, presumably each on its own thread and
able to overlap, and via appropirate protection, service the request.
- Serving the request takes real time so using IPC (thanks from yesterday!)
the method blocks waiting for the async work to complete.
- At some point the work completes and the service will then release the
client method call thread and let it return the results.

So, I only want to expose one method from my server.
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Grant Schenck said:
Hello,

I have a Windows Service developed in C# .NET. I'm making it a remote
server and I can, via an IPC Channel, expose methods and call them from a
client. However, I now want my remoted object to be able to invoke a
method on my server object and given that the object is built in a C#
Class DLL shared between the client and server I'm not sure how to get
access to the server object from the remoted object.

So, what techniques are commonly used to allow this?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Grant,
[GRANT] If I create a form from one form, I can pass the first form to the
second form. However, the remoting object isn't created until the client
invokes the remoted object method so I'm confused as to how the server
could
get access to the object between when it is created and when the method is
invoked. Part of my confusion is I'm not clear how my remoted object,
built
as a C# DLL class, can be able to call methods in my Service object.

Certainly your remoted object has access to static properties/fields in
the same app domain, right? =)
[GRANT] Are you saying I should derive my Service class from
MarshallByRefObject (as well as ServiceBase) and somehow this object would
be a singleton for all clients, i.e., my remoted object IS my service???

If you expose your service object from your remoted object, then there
are two things that can happen. A serialized instance of the service object
could be passed back to the caller, and the caller can make calls on that.
The problem with this is that the calls occur in the client's app domain,
which is not what I think you want.

The second part is to have the service object derive from
MarshalByRefObject. Now, if you are actually passing back your
ServiceBase-derived class, this is fine, since the inheritance chain is
ServiceBase->Component->MarshalByRefObject. Calls made in the client's app
domain will be marshaled back to the app domain of the service.

I really wouldn't expose the service base here. If anything, I would
expose another object (which derives from MarshalByRefObject) which exposes
the functionality you need.
[GRANT] Actually I don't. My goals seem simple.

- I have a service running (controls a phone system but not particularly
relevent)
- I want to remote a single method (MakeCall)
- Client applications will invoke this method and pass a few parameters.
- Service will "get" these requests, presumably each on its own thread and
able to overlap, and via appropirate protection, service the request.
- Serving the request takes real time so using IPC (thanks from
yesterday!)
the method blocks waiting for the async work to complete.
- At some point the work completes and the service will then release the
client method call thread and let it return the results.

So, I only want to expose one method from my server.

If this is the case, have your object that exposes this method derive
from MarshalByRefObject. Also, you might want to make it a per-call
service, meaning your object is instantiated and then destroyed for each
call.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Grant Schenck said:
Hello,

I have a Windows Service developed in C# .NET. I'm making it a remote
server and I can, via an IPC Channel, expose methods and call them from a
client. However, I now want my remoted object to be able to invoke a
method on my server object and given that the object is built in a C#
Class DLL shared between the client and server I'm not sure how to get
access to the server object from the remoted object.

So, what techniques are commonly used to allow this?

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top