在 .Net Remoting 中使用单例
我通过远程处理公开的单例类遇到了一些问题。 在我的服务器中,我有:
TcpChannel channel = new TcpChannel( Settings.Default.RemotingPort );
ChannelServices.RegisterChannel( channel, false );
RemotingConfiguration.RegisterWellKnownServiceType(
typeof( RemotableObject ), "RemotableObject",
WellKnownObjectMode.Singleton );
RemotableObject 是一个继承 MarshalByRefObject 的单例对象。
我的客户端通过以下方式连接到它:
remoteObject = (RemotableObject)Activator.GetObject(
typeof( RemotableObject ),
string.Format( "tcp://{0}:{1}/RemotableObject", serverIP, serverPort ) );
就远程处理而言,一切都很好,但是当我像这样访问服务器代码中的单例对象时:
int someValue = RemotableObject.Instance.SomeDynamicValue;
它访问与客户端不同的实例。 我还验证了 RemotableObject 中的私有构造函数在调试时被命中两次。
如果我通过服务器代码中的远程处理获取 RemotableObject 的实例,我可以获得所需的行为,但是有没有一种方法可以让我从服务器访问与客户端相同的对象,而无需远程处理开销?
I'm having a bit of a problem with a singleton class I'm exposing via remoting. In my server I have:
TcpChannel channel = new TcpChannel( Settings.Default.RemotingPort );
ChannelServices.RegisterChannel( channel, false );
RemotingConfiguration.RegisterWellKnownServiceType(
typeof( RemotableObject ), "RemotableObject",
WellKnownObjectMode.Singleton );
RemotableObject is a singleton object that inherits MarshalByRefObject.
My client connects to it via:
remoteObject = (RemotableObject)Activator.GetObject(
typeof( RemotableObject ),
string.Format( "tcp://{0}:{1}/RemotableObject", serverIP, serverPort ) );
Everything works great as far as the remoting goes, but when I access the singleton object in my server code like this:
int someValue = RemotableObject.Instance.SomeDynamicValue;
It accesses a different instance than the clients do. I have also verified that the private constructor in RemotableObject gets hit twice while debugging.
I can get the desired behavior if I get an instance to RemotableObject via remoting in my server code, but is there a way that I can access the same object as my clients from the server without the remoting overhead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我明白你的意思,(你希望该对象存在于服务器上,但你希望所有客户端调用都能在服务器上获取该对象的相同实例,并且你还希望在服务器代码中调用来获取该对象)
然后,如果您的服务器是“ServerName”,并且它正在侦听端口 nnnn,Uri 为“MyMsgEndpointUri”,则您已将 Uri 定义为:
在您的服务器中,通过以下方式初始化端点:
而不是
RegisterWellKnownServiceType();
另外,在表示单例的类中,请记住使用 null 操作覆盖 InitializeLifetimeService 属性...否则单例对象将在某个时刻收集垃圾...
从服务器,只需调用您的单例类的静态工厂方法来访问该单例实例...根本不要使用远程调用...
If I understand what you're after, (you want the object to live on the server, but you want all client calls to get the same instance of the object on the server, and you also want calls in the server code to get that same instance? )
then, If your server is "ServerName", and it is listening on port nnnn, with Uri as "MyMsgEndpointUri", you have defined the Uri as:
in your server, Initialize the endpoint by:
Instead of
RegisterWellKnownServiceType();
also, in the class representing the singleton, remember to override the InitializeLifetimeService property with a null operation... or the singleton object will get Garbage collected at some point...
From the server, just call your singleton classes' static factory method to get access to that singleton instance... Do not use remoting calls at all...
我从未尝试从服务器端调用 Activator.GetObject,但这应该返回客户端正在使用的相同实例。 但是,您仍然会获得代理。
I've never tried to call Activator.GetObject from the server-side, but that should return the same instance that the clients are using. But, you're still going to get a proxy.
不能。
服务器中没有办法读取客户端的内存。
No.
There is no way to read the memory of the client in the server.