我使用.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