[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

How to pass arguments to a remote constructor

Clinton Pierce

6/15/2004 2:21:00 PM

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/theEndP...);
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?


1 Answer

Nicholas Paldino [.NET/C# MVP]

6/15/2004 2:32:00 PM

0

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.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com



"Clinton Pierce" <cpierce@payroll1.com> wrote in message
news:eI4LFQuUEHA.556@tk2msftngp13.phx.gbl...
> 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/theEndP...);
> 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?
>
>