.NET 远程处理 - 代理问题
我在服务器上有 RemoteRamdom 类:
服务器:
public class RemoteRandom : MarshalByRefObject
{
Random r = new Random();
public Random GetRandomObject()
{
return r;
}
}
客户端:
RemoteRandom remoteRandom = (RemoteRandom)Activator.GetObject(typeof(RemoteRandom), "tcp://localhost:1000/UzakNesne");
Random r = remoteRandom.GetRandomObject();
while (true)
{
Console.WriteLine(r.Next());
}
问题: 客户端可以成功调用 GetRandomObject 方法。但是,当我调用 r 实例的方法时,它在本地运行。我的意思是,即使我关闭服务器应用程序,r.Next() 也会继续工作。
r 如何在服务器上工作?
I have RemoteRamdom class on Server:
SERVER:
public class RemoteRandom : MarshalByRefObject
{
Random r = new Random();
public Random GetRandomObject()
{
return r;
}
}
CLIENT:
RemoteRandom remoteRandom = (RemoteRandom)Activator.GetObject(typeof(RemoteRandom), "tcp://localhost:1000/UzakNesne");
Random r = remoteRandom.GetRandomObject();
while (true)
{
Console.WriteLine(r.Next());
}
The Problem: The client can call GetRandomObject method successfully. However, when I call the methods of r instance, it runs locally. I mean, r.Next() continues working even I close the server application.
How can r work on server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定它是远程对象吗?
您的代码基本上不显示重定向激活器以实际从服务器引用该类的配置。如果没有配置,激活器将愉快地创建一个本地类。
Are you SURE it is a remote object?
Your code basically does not show the configuration that is redirecting the Activator to actually reference the class from a server. Witbhout configuration, the activator will happily create a LOCAL CLASS.
返回的对象 r 将被序列化并在本地重新创建,因为 Random 类被标记为可序列化并且不继承自 MarshalByRefObject。
您可以以与创建remoteRandom相同的方式在服务器上创建r,尽管您只能从客户端访问它。
The returned object r will be serialized and recreated locally because the Random class is marked as serializable and doesn't inherit from MarshalByRefObject.
You can create r on the server in the same way you create remoteRandom, although you'll only be able to access it from the client.