【发布时间】:2009-06-30 21:25:30
【问题描述】:
我正在使用 .Net Remoting 并尝试访问在服务器中修改的远程对象,这是对象定义: 这个想法是,我在服务器上创建的 MRB 对象返回给客户端,并在客户端调用 getString() 时打印出“Set On Server”。
我现在得到的是客户端上的空字符串,因此当客户端上调用 new 时,MRB 对象没有发送到客户端。 对不起所有不同的类等,但这是唯一的方法,我尽可能地修剪。
我真正想要的是运行时在客户端打印的“Set On The Server”。
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 !";
}
}
知道这里是服务器:
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();
}
}
还有.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>
最后是客户:
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();
}
}
}
还有它的配置:
<?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>
【问题讨论】: