在 .Net Remoting 中使用单例

发布于 2024-07-16 15:23:29 字数 897 浏览 3 评论 0原文

我通过远程处理公开的单例类遇到了一些问题。 在我的服务器中,我有:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-07-23 15:23:29

如果我明白你的意思,(你希望该对象存在于服务器上,但你希望所有客户端调用都能在服务器上获取该对象的相同实例,并且你还希望在服务器代码中调用来获取该对象)

然后,如果您的服务器是“ServerName”,并且它正在侦听端口 nnnn,Uri 为“MyMsgEndpointUri”,则您已将 Uri 定义为:

var MesgUri = "tcp://ServerName:nnnn/MyMsgEndpointUri"; 

在您的服务器中,通过以下方式初始化端点:

RemotingServices.Marshal([singletonInstance], MesgURI);  

而不是 RegisterWellKnownServiceType();

另外,在表示单例的类中,请记住使用 null 操作覆盖 InitializeLifetimeService 属性...否则单例对象将在某个时刻收集垃圾...

public override object InitializeLifetimeService() { return (null); }

从服务器,只需调用您的单例类的静态工厂方法来访问该单例实例...根本不要使用远程调用...

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:

var MesgUri = "tcp://ServerName:nnnn/MyMsgEndpointUri"; 

in your server, Initialize the endpoint by:

RemotingServices.Marshal([singletonInstance], MesgURI);  

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...

public override object InitializeLifetimeService() { return (null); }

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...

旧故 2024-07-23 15:23:29

我从未尝试从服务器端调用 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.

过期情话 2024-07-23 15:23:29

不能。

服务器中没有办法读取客户端的内存。

No.

There is no way to read the memory of the client in the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文