【发布时间】:2010-10-18 15:51:40
【问题描述】:
我通过远程处理暴露的单例类有点问题。在我的服务器中,我有:
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 的实例,我可以获得所需的行为,但是有没有一种方法可以让我从服务器访问与我的客户端相同的对象,而无需远程处理开销?
【问题讨论】:
标签: c# .net singleton remoting