Remoting and calling functions in another class

M

Mark

What I want to do is call a function in my main form/class from the
server (which the another program/client calls). The main form does
not really create a server object so I can't pass the frmForm class
into the constructor of MyServer (the client side would have no idea
what to pass into that anyways). What other way to do this is there?
I guess there is static functions, but I can't make everything in
those functions static.

namespace Mark
{
public class frmForm : System.Windows.Forms.Form
{
private void frmForm_Load(object sender, System.EventArgs e)
{
HttpChannel channel = new HttpChannel(8001); //Create a new
channel
ChannelServices.RegisterChannel (channel); //Register channel
RemotingConfiguration.RegisterWellKnownServiceType(typeof (MyServer),"MyServer",WellKnownObjectMode.Singleton);
}
public void Function1()
{
//do some non-static stuff here
}
}

public class MyServer: MarshalByRefObject
{
public void CalledByClient()
{
//want to call frmForm.Function1 here
}
}
}
 
R

Richard Blewett

Well MyServer is a singleton how about getting the form to achieve the same
effect by manually marshallingobject into the remoting infrastructure.

instead of using RegisterWellKnownServicetype, create an instance of
MyServer pass itself into the ctor and then call
RemotingServices.Marshal passing the instance.

Its proably a good idea in this case to override InitializeLifetimeServices
in MyServer and return null. This will disable the lease based distributed
garbage collection of remoting which in this case is probably a good idea.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
 
M

Mark

Richard Blewett said:
Well MyServer is a singleton how about getting the form to achieve the same
effect by manually marshallingobject into the remoting infrastructure.

instead of using RegisterWellKnownServicetype, create an instance of
MyServer pass itself into the ctor and then call
RemotingServices.Marshal passing the instance.

Its proably a good idea in this case to override InitializeLifetimeServices
in MyServer and return null. This will disable the lease based distributed
garbage collection of remoting which in this case is probably a good idea.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

So would I do something like?

MyServer svr = new MyServer();
svr.PassFormOver(this);
ObjRef objrefWellKnown = RemotingServices.Marshal(svr, ???????);

What do I put for the other arguments?

I would of course put something like

public class MyServer: MarshalByRefObject
{
static public Mark.frmForm frmMyForm;

public void PassFormOver(Mark.frmForm frm)
{
frmMyForm = frm;
}

public void CalledByClient()
{
//want to call frmForm.Function1 here
}
}
}
 
R

Richard Blewett

HttpChannel channel = new HttpChannel(8001);
MyServer ms = new MyServer(this);
RemotingServices.Marshal(ms, "MyServer");
ChannelServices.RegisterChannel (channel); //Register channel

you basically provide the same info as with RegisterWellKnownServiceType but
*you* create the instance rather than the remoting infrastructure. And as
you can see I'd probably create a ctor on MyServer which took an instance of
your form.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
 
M

Mark

That seems to work. Thanks. One more question though. How does it
know it's a singleton? Sorry if it's a rookie question but this is my
first time working with remoting. Does it also already know it's a
WellKnownServiceType?
 

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