我使用.Net Remoting并尝试访问服务器中修改的远程对象,这里是对象def: 这个想法是,我在服务器上创建的MRB对象返回给客户端,并且在客户端上调用getString()时,将打印出“Set On Server”。问题与.Net Remoting(C#)
我现在得到的是客户端上的一个空字符串,所以MRB对象在客户端调用新的时候并没有发送给客户端。 对不起所有的课程等,但它是唯一的方法,我尽可能修剪。
我真正想要的是“设置在服务器上”运行时打印在客户端上。
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远程配置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>
当客户端运行时,您是否看到在客户端上打印的“在服务器上设置”? – daniel 2009-06-30 22:15:25