Reputation: 9825
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>
Upvotes: 2
Views: 3241
Reputation: 1622
Could a firewall be blocking tcp port 8000 in your environment? You could try switching to http just as a test.
Upvotes: 0
Reputation: 19863
Are you sure your sockets closes as soon as the application ends?
If you test multiple times rapidly this may cause tcp communication problems
Upvotes: 0
Reputation: 6672
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.
Upvotes: 2