两个应用程序加载相同的 .NET 程序集:相同的实例?
1) .NET 程序集 MyAssembly.dll 实现单例类 MyClass
2) .NET 应用程序引用 MyAssembly.dll 并使用 < em>MyClass
3) .NET ActiveX MyActiveX.dll 实现一个 COM Visible 类,该类又引用 MyAssembly.dll 中的 MyClass em>
我的问题是:如果应用程序和 ActiveX 同时运行(网页中的 ActiveX),我是否加载了一个或两个 MyAssembly.dll 实例?我需要它作为单例工作的一个实例。
谢谢
1) .NET Assembly MyAssembly.dll implements a Singleton class MyClass
2) A .NET APP has a reference to MyAssembly.dll and uses MyClass
3) A .NET ActiveX MyActiveX.dll implements a COM Visible class wich in turn references to MyClass in MyAssembly.dll
My question is: if the app and the ActiveX are running at the same time (the ActiveX in a web page), do I have one or two instances of MyAssembly.dll loaded? I need it to be one instance for the singleton to work.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
程序集被加载到应用程序域中,静态字段(单例所基于的)根据应用程序域进行实例化。每个进程至少有一个应用程序域,因此每个进程至少有一个实例。
您的 ActiveX 组件在 .NET 应用程序之外的另一个进程中实例化。你没有获得共享内存。
要仅实例化单例一次,您需要启动一个进程并远程访问它。您可以通过使用远程处理、DCOM、WCF 或类似的东西来实现这一点。您需要确保两个进程都访问 this 实例。
The assembly is loaded into the Application Domain, static fields (where the singleton is based on) are instantiated per Application Domain. There is at least one App Domain per process, so you get at least one instance per process.
Your ActiveX component is instantiated in another process then the .NET Application. You don't get shared memory.
To instantiate the singleton only once, you need to start a single process and access it remotely. You could achieve this by the use of remoting, DCOM, WCF or something like this. You need to make sure that both process access the this instance.
您将有两个实例,它们在不同的进程中运行。
如果您需要有一个通过 ActiveX 控件访问的单例,我建议创建一个 WCF 服务来托管您的单例对象并提供访问。然后,您可以编写一个微型 COM 可见客户端来访问该服务以支持您的 ActiveX 控件。
You are going to have two instances, they are running in different processes.
If you need to have a singleton that is accessed through an ActiveX control, I would suggest creating a WCF service to host your singleton object and provide access. You can then write a tiny COM-visible client that accesses the service to support your ActiveX control.