How to pass arguments to a remote constructor

C

Clinton Pierce

I've got a class that I want to access remotely. The touble is, I can't
figure out how to call the constructor of the class with arguments -- and
the arguments are necessary to initialize the class.

For example, here's my code on the client end:

static void Main(string[] args)
{
HttpChannel chan = new HttpChannel(0);
ChannelServices.RegisterChannel(chan);
MarshalByRefObject obj = (MarshalByRefObject)
RemotingServices.Connect(typeof(PR1.Client),
"http://127.0.0.1:65100/theEndPoint");
try
{
PR1.Client c = obj as PR1.Client;
string s = c.ClientName;
Console.WriteLine("Name: " + s);
}
catch (System.Exception ex)
{
Console.WriteLine("Exception caught: \n" + ex.Message);
}
return;
}

The constructor for client requires that I pass in 3 strings as arguments.

How do I do this?
 
N

Nicholas Paldino [.NET/C# MVP]

Clinton,

When you call Connect, you are looking for a well-known object.
Well-known objects can either be single-call, or singleton. Each of these
modes require a parameterless constructor. The reason for this is that if
it is a singleton, then the instance will already be created when you call
it (the constructor doesn't really do much in this case). In the case of
the single-call, the argument could be made that parameters could be passed,
however, the idea is that these are services, that are running whether you
create them or not, so you constructing a new object in the service doesn't
make sense.

If you want a model where you can pass the parameters to the
constructor, then I would use a client-activated object, which requires you
to register the type on the client and the server through the static
RegisterActivatedClientType and RegisterActivatedServiceType methods on the
RemotingConfiguration class.

Hope this helps.
 

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