使用VS​​辛格尔顿一次调用的

使用VS​​辛格尔顿一次调用的

本文介绍了使用VS​​辛格尔顿一次调用的.NET远程处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到此时所有的.NET远程code我写的或已被暴露为SingleCall工作。

Up until this point all the .NET remoting code I have written or worked with has been exposed as SingleCall.

我跑过托管在Windows服务中一个.NET远程组件,公开为单身。

I ran across a .NET remoting component hosted in a Windows service that is exposed as a Singleton.

此目的,必须通过一个以上的客户端在同一时间叫的潜力,并且它没有锁或其他规定,以保护其内部状态

This object has the potential to be called by more than one client at the same time, and it has no locks or other provisions to protect its internal state.

如果我理解正确辛格尔顿随后这对大问题的可能性是否正确?

If I understand Singleton correctly then this has the potential for big problems correct?

推荐答案

没有比SingleCall组件的更多潜力。双方将有问题,如果他们试图访问不安全的方式共享内存的位置。

No more potential than a SingleCall component. Both will have problems if they attempt to access a shared memory location in an unsafe manner.

SingleCall和单件之间的区别在于,对于SingleCall,每个传入请求将获得创建来处理该呼叫已定义类型的新实例。每个实例都将拥有自己的内存空间和实例变量,但他们仍然可以共享静态和全局变量,外部资源,文件,网络连接等。如果SingleCall类是codeD访问在一个线程中的任何共享内存状态-unsafe的方式,那么你就会有问题。

The difference between SingleCall and Singleton is that, for SingleCall , every incoming request will get a new instance of the defined type created to handle that call. Each instance will have its own memory space and instance variables, but they can still share static and global variables, external resources, files, network connections, etc. If the SingleCall class is coded to access any shared memory state in a thread-unsafe manner, then you will have issues.

有一个单身,而另一方面,只得到了所有传入请求创建一个实例,因此按照定义,该单内使用的每一个实例变量,其实,所有的请求之间共享。一个很好的例子可能是消息发布,所有code服务器需要访问将邮件发送到一个或多个订阅的客户端......

A Singleton, on the other hand, only gets one instance created for ALL incoming requests, so by definition, every instance variable in use within that singleton is, in fact, shared among all incoming requests. A good example might be a message publisher, that all code in the server needs to access to send messages to one or more subscribed clients....

要解决从@Cocowalla意见,确保如果你这样做,你覆盖的方法

To address comment from @Cocowalla, make sure if you do this you override the method

  MarshalByRefObject.InitializeLifetimeService()

如图所示,或者你单身会死了意外,如果没有人叫了一段时间...

as shown, or your singleton will die out unexpectedly if no one calls it for a while...

public class MessageManager : MarshalByRefObject
{
    #region Singleton / MarshalByRefObject code
    private static MessageManager mgr =
        new MessageManager(); // creates singleton
    static MessageManager() { }
    private MessageManager() { }
    public static MessageManager Instance { get { return mgr;  } }
    public override object InitializeLifetimeService() { return (null); }
    #endregion Singlelton code
    // ... other stuff ...
 }

  // in Remoting Host initialization code...
   MessageManager mgr = MessageManager.Instance; // generates singleton;
   RemotingServices.Marshal(mgr, URI);

这篇关于使用VS​​辛格尔顿一次调用的.NET远程处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 07:21