tags:

views:

531

answers:

2

I'm attempting to pass a remote object as a parameter to a remote method, but I get a security exception when the remote object attempts to run a method on the received remote object.

This is a sample remote object:

public class SharpRemotingDLL : MarshalByRefObject
{
    public String getRemoteMessage(SharpRemotingDLL server)
    {
        // the exception is raised here
        return server.getMessage();
    }

    public String getMessage()
    {
         return "Hello World!";
    }
}

This is the server starter (two instances of this are running, one on 127.0.0.10025, the other on 127.0.0.10026):

public static int Main(string[] args)
{
    TcpServerChannel channel;
    channel = new TcpServerChannel(10025);
    ChannelServices.RegisterChannel(channel, false);
    RemotingConfiguration.RegisterWellKnownServiceType(
        typeof(SharpRemotingDLL),
        "SharpRemotingDLL",
        WellKnownObjectMode.Singleton);
    Console.ReadLine();
    return 0;
}

And this is the client:

static void Main(string[] args)
{
    SharpRemotingDLL server0 = (SharpRemotingDLL)
        Activator.GetObject(typeof(SharpRemotingDLL),
        "tcp://localhost:10025/SharpRemotingDLL");
    SharpRemotingDLL servers[1] = (SharpRemotingDLL)
        Activator.GetObject(typeof(SharpRemotingDLL),
        "tcp://localhost:10026/SharpRemotingDLL");
    Console.WriteLine(server0.getRemoteMessage(server1));
}

How do I correctly pass server1 as a parameter to the getRemoteMessage method?

A: 

I have written this kind of code before, but I have never tried to pass a server object to another server object in this way. I'm not a security expert, but I can sense why this might not be allowed.

You should obtain the message directly from Server1, rather than asking another remote server to return it. In other words, your message retrieval logic needs to be in the client.

Robert Harvey
That was just some sample code to demonstrate the problem. In my real code, SharpRemotingDLL is a class exporting basic file operations (CreateFile, ReadFile etc.) and I'm using a similar synthax to implement CopyFile and MoveFile between two servers:client usage server1.CopyFile("foo.txt",server2)CopyFile method: public void CopyFile(String name, SharpRemotingDLL server) { server.WriteFile(name,this.ReadFile(name)); }
vbigiani
What is the exact wording of the error message?
Robert Harvey
I can't translate exactly because my OS is in Italian, and googling for the error message is unhelpful. Anyway: http://pastebin.com/m6219b56f
vbigiani
A: 

vbigiani,

This article should shed some light on your problem:

http://weblogs.asp.net/jan/archive/2003/06/01/8106.aspx

If that doesn't work, you can get several relevant Google hits here:

Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed

Robert Harvey
That solved it, thank you!
vbigiani