.net - 单例和客户端激活对象 (CAO)、Remoitng

标签 .net singleton remoting .net-remoting

两者执行相同的操作

MSDN

  • Singleton:““单次调用对象为一个且仅有一个传入请求提供服务......”
  • CAO:“客户端激活对象 (CAO) 是根据客户端请求激活的服务器端对象......”

在这两种情况下,数据都不是共享的,但在单例中,一次只能连接一次客户端,为什么有人想要这样 Singleton在什么场景下有用,它们之间还有什么区别吗?

最佳答案

你读错了台词。这是来自 MSDN 的定义。我突出显示(粗体)这些对象之间的一些差异。

  • Single Call

    Single Call objects service one and only one request coming in. Single Call objects are useful in scenarios where the objects are required to do a finite amount of work. Single Call objects are usually not required to store state information, and they cannot hold state information between method calls. However, Single Call objects can be configured in a load-balanced fashion.

  • Singleton Objects

    Singleton objects are those objects that service multiple clients and hence share data by storing state information between client invocations. They are useful in cases in which data needs to be shared explicitly between clients and also in which the overhead of creating and maintaining objects is substantial.

  • Client-Activated Objects (CAO)

    Client-activated objects (CAO) are server-side objects that are activated upon request from the client. This way of activating server objects is very similar to the classic COM coclass activation. When the client submits a request for a server object using "new" operator, an activation request message is sent to the remote application. The server then creates an instance of the requested class and returns an ObjRef back to the client application that invoked it. A proxy is then created on the client side using the ObjRef. The client's method calls will be executed on the proxy. Client-activated objects can store state information between method calls for its specific client and not across different client objects. Each invocation of "new" returns a proxy to an independent instance of the server type.

状态信息是存储在对象的变量或属性中的一些数据,用于处理客户端请求。

由于单个调用对象是在客户端请求它执行某些工作时创建的,并在完成工作后销毁,因此它无法保存状态信息,导致每个请求都会创建一个新对象(它可以在数据源中加载和存储数据,以完成其工作)。

单例对象仅创建一次(可能在服务器启动时),并且只要服务器进程正在运行,它就存在。它可以将信息存储在变量和属性中以处理客户端请求,因为每个客户端都使用相同的对象,并且在客户端调用后它不会被销毁。

显示单个调用和单例对象之间差异的一个简单示例是创建一个 Increment() 方法来递增对象中的变量(整数)并将该变量写入控制台。单次调用对象将始终向控制台打印相同的值(如果变量以 0 开头,则为 1),而单例对象将始终在每次调用后打印递增的值(1、2、3 等)。

关于.net - 单例和客户端激活对象 (CAO)、Remoitng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184067/

相关文章:

powershell - net use */delete/y 不能解决错误 "New-PSDrive : Multiple connections to a server ...."

.net - native 线程异常崩溃 .Net 应用程序无一异常(exception)

c# - 根据另一个按钮激活或停用按钮

c# - 何时何地使用 GetType() 或 typeof()?

node.js - 为什么这个 EventEmitter pubsub 单例接口(interface)不能在 Node.js 中工作?

ios - 初始化单例异步 iOS

c# - unity .net Remoting Server get_animation只能从主线程调用

java - 如何将旧数据转换到新系统?使用 sql 或托管代码?

c# - 可以在 C# 中使用带有非默认构造函数的单例吗?

c# - 当您通过 .NET Remoting 从远程对象返回 Stream 时到底发生了什么