how to pass objects as parameter to remoting object methods?

G

Gelios

Hi All!
Probably my question is newbie but anycase.
Should I do object that I want to pass as parameter to remoting object serializable?

For Example:

//server

public class MyServerObject
{
Settings settings = Settings.Instance();

public static void Main()
{
settings.Restore();
//Initialize TCP channel and Remoting object
//channel properties
IDictionary props = new Hashtable();
props["name"] = "Channel1";
props["port"] = settings.getServerPort();
//create channel
TcpChannel channel = new TcpChannel(props, null, new BinaryServerFormatterSinkProvider());
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemotingObject ),
"ServerUri", WellKnownObjectMode.Singleton);
}
}


//client
public class MyClientObject
{
Settings settings = Settings.Instance();
public static void Main()
{
this.settings.Restore();
string serverUri = "tcp://" + settings.getServerAddress() + ":" + settings.getServerPort() + "/ServerUri";
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
serverObject = (MyRemotingObject) Activator.GetObject(typeof(MyRemotingObject), serverUri);
if (serverObject == null) LoggingProcessor.Error("Could not locate server");

//do something
ServiceObject serviceObject = new ServiceObject();
serviceObject.str1 = "this is a string";
serviceObject.num1 = 12;
serverObject.myMethod(serviceObject);

}
}


//remitong object
public class MyRemotingObject : MarshalByRefObject
{
public void myMethod(MySomeObject myObject)
{
// do somethinng
}
}

//service object
public class ServiceObject
{
public string str1;
public int num1;
public int num2
}

In given case I got following error:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: The type SES.Tools.General.Queue in Assembly Tools, Version=1.0.1934.2848, Culture=neutral, PublicKeyToken=null is not marked
as serializable.

I tried declare ServiceObject as [Serializable] and got following error:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Any ideas?
 
N

Nicholas Paldino [.NET/C# MVP]

Gelios,

It says the Queue object is not serializable (and the definition is not
shown here), which makes me think that the class that you tagged as
[Serializable] has members that must be serializable as well. That error
makes me think that there are fields that are not. Make sure that the
fields are all serializable, and i should work.

Hope this helps.
 
G

Gelios

Nicholas said:
Gelios,

It says the Queue object is not serializable (and the definition is not
shown here), which makes me think that the class that you tagged as
[Serializable] has members that must be serializable as well. That error
makes me think that there are fields that are not. Make sure that the
fields are all serializable, and i should work.

Hope this helps.

First of all thanks.
I think so too, but it is clear to me.
I showed Queue object as ServiceObject example.
Below real definition:

using System;

namespace SES.Tools.General
{
/// <summary>
/// Summary description for Queue.
/// </summary>
public class Queue
{
public int queueId;
public string description;
public string email;
public int ruleId;

public override string ToString()
{
return description;
}

}
}

Am I understand you correct? I should this object serializable?
 
R

Richard Blewett [DevelopMentor]

If you want to take the object over "by value" - in other words get a copy of it to the server then mark it as [Serializable]. If you want to pass it "by reference" so that the server gets a proxy to the object amd all calls to it are remoted back to to the client, derive from MarshalByRefObject. I'd recommend the first from looking at the object in question.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

First of all thanks.
I think so too, but it is clear to me.
I showed Queue object as ServiceObject example.
Below real definition:

using System;

namespace SES.Tools.General
{
/// <summary>
/// Summary description for Queue.
/// </summary>
public class Queue
{
public int queueId;
public string description;
public string email;
public int ruleId;

public override string ToString()
{
return description;
}

}
}

Am I understand you correct? I should this object serializable?
 

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