tags:

views:

484

answers:

3

I am using .Net Remoting and trying to access a remote object modified in the Server, here is the object def: The idea is that the MRB object, which I create on the server, is returned to the client and "Set On Server" is printed out when getString() is called on the client.

What I get right now is a null string on the client, so the MRB object was not send to the client when new was called on the client. Sorry for all the different classes etc, but its the only way, I trimmed as much as possible.

What I really want is for "Set On The Server" printed on the client when run.

using System;
using System.Runtime.Remoting.Lifetime;
namespace RemoteType
{
    public class MyRemoteObject : System.MarshalByRefObject
    {
        private string sharedString;
        public string getString()
        {
            return sharedString;
        }
        public void setString(string value)
        {
        sharedString = value;
        }   
        public MyRemoteObject()
        {
            Console.WriteLine("MyRemoteObject Constructor Called");
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
        public string Hello()
        {
            return "Hello, Welcome to .Net Remoting !";
        }
    }

Know here is the server:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleServer
{
    class SimpleServer
    {
        public static MyRemoteObject MRB = null;
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemotingAppServer.exe.config");
            MRB = new MyRemoteObject();
            MRB.setString("Set on the server");
            Console.WriteLine(MRB.getString());
            RemotingServices.Marshal((MRB), "MyRemoteObject");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
        }
    }

And the .NET Remote Config App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
                <channel ref="tcp" port="8000" />
            </channels>
            <service>
                <wellknown mode="Singleton" 
                type="RemoteType.MyRemoteObject, RemoteType"
                objectUri="MyRemoteObject" />
            </service>
        </application>
    </system.runtime.remoting>
</configuration>

Finally the client:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleClient
{
    class SimpleClient
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemoteClient.exe.config");
            MyRemoteObject robj = new MyRemoteObject();
            Console.WriteLine(robj.Hello() + " " + robj.getString());
            Console.ReadLine();
        }
    }
}

And its config too:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application name = "SimpleClient">
            <client>
                <wellknown
                type="RemoteType.MyRemoteObject,RemoteType"
                url="tcp://localhost:8000/MyRemoteObject"/>
            </client>
            <channels>
                <channel ref="tcp" port="0"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>
+1  A: 

Hello. I did test your code and worked perfectly. I did set three projects, one with server, another with client and a third one shared between server and client for the remote object. In the remote app.config you can remove the wellknown entry as you are already marshalling it by code.

jmservera
Did you see "Set on the server" printed on the Client when the client runs ?
daniel
@daniel: Did you set up the project to start server and client at once? If yes, maybe adding a Thread.Sleep at the beginning of the client code might help.
Theo Lenndorff
Yes, I saw "Set on the server" in both screens client and server. I did have two solutions, one for server and one for client. I first started server and then started client many times, all with same successful result.
jmservera
Must be something weird on my machine, but I get:\bin\Debug>RemoteClient.exeMyRemoteObject Constructor CalledHello, Welcome to .Net Remoting !
daniel
A: 

Are you sure your sockets closes as soon as the application ends?

If you test multiple times rapidly this may cause tcp communication problems

Eric
A: 

Could a firewall be blocking tcp port 8000 in your environment? You could try switching to http just as a test.

Chris Tybur