繁体   English   中英

应用程序之间的对象共享?

[英]Object Sharing between Applications?

假设我有一个每秒更新 1000 多次的大型数据数组。
另一个应用程序想要在很短的时间间隔内访问和读取数组。 两个应用程序都在同一台机器上。

我曾尝试使用 WCF 进行进程间通信,但是每秒数千次序列化和发送整个数组(或一个大对象)在性能上是不可行的。
有没有办法在 c# 中直接访问来自不同应用程序的对象?

您可以使用一些 IPC 技术,尽管早于 WCF 的技术在今天仍然适用。

管道

管道就是这样一种技术。 它是二进制的,在内核模式下运行并且速度非常快! 虽然它是相当低级的,并且不能访问“对象”。

.NET 远程处理

.NET Remoting将提供对对象的访问,但可能不如管道那么快。

管道和 .NET 远程处理都比序列化技术 WCF 更快,后者将事物转换为详细的 XML/SOAP。

通讯

COM 是 IPC 的二进制协议。 COM 是一种客户端服务器模型,其中客户端从 COM 或 OLE 服务器请求数据。 COM 的美妙之处在于您可以直接访问服务器中的对象——它们没有被序列化。 例如,请求SAFEARRAY 中的元素。

SAFEARRAY是由类型安全数据组成的任意维度的自动化安全结构。 幸运的是 .NET 会为我们隐藏 SAFEARRAY gobble-de-gook。

在我的示例中,我创建了一个将公开数组的Manager类。 为了获得Manager我使用了工厂模式,因此Manager本质上是一个单例。

您应该按如下方式布置您的项目:

  • MyComLib.Contracts.dll - 包含所有接口
  • MyComLib.dll - 包含FactoryManager的实现

首先是合同:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IArrayItem
{
    #region Properties

    string Name { get; set; }

    int Whatsit { get; set; }

    #endregion
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IFactory 
{
    #region Methods

    IManager CreateManager();

    #endregion
}

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IManager
{
    #region Properties

    IArrayItem[] Array { get; }

    #endregion
}

public static class MyComLibConstants
{
    public const string FactoryProgId = "MickyD.MyComLib.Factory.1";
    
}

现在对于工厂模式:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof (IFactory))]
[Guid("...")]
[ProgId(MyComLibConstants.FactoryProgId)]
public class Factory : MarshalByRefObject, IFactory
{
    #region IFactory Members

    /// <summary>
    /// Creates the manager.
    /// </summary>
    /// <returns></returns>
    public IManager CreateManager()
    {
        return Manager.Instance;
    }


    #endregion
}

管理者:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof (IManager))]
[Guid("...")]
internal sealed class Manager : MarshalByRefObject, IManager
{
    private static Manager _instance;

    #region Constructor

    /// <summary>
    /// Prevents a default instance of the <see cref="Manager"/> class from being created.
    /// </summary>
    private Manager()
    {
        const int n = 5000;
        Array = new IArrayItem[n];
        for (int i = 0; i < n; i++)
        {
            Array[i]=new ArrayItem();
        }
    }

    #endregion

    #region Properties

    
    /// <summary>
    /// Gets the instance.
    /// </summary>
    /// <value>
    /// The instance.
    /// </value>
    public static IManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Manager();
            }
            return _instance;
        }
    }

    #endregion

    #region IManager Members

    /// <summary>
    /// Gets the array.
    /// </summary>
    /// <value>
    /// The array.
    /// </value>
    public IArrayItem[] Array { get; private set; }

    #endregion
}

一个测试应用程序。 这应该引用MyComLib.Contracts.dll不是 MyComLib.dll

class Program
{
    static void Main(string[] args)
    {
        var type = Type.GetTypeFromProgID(MyComLibConstants.FactoryProgId);
        var factory = Activator.CreateInstance(type) as IFactory;
        var manager = factory.CreateManager();
        var x = manager.Array[500].Whasit;
    }
}

最后一步是将此进程内COM 服务器更改为进程外COM 服务器,以便多个进程共享同一个Manager并且不会创建自己的单例。 换句话说,一个跨越进程的单例。 Manager运行时,它本质上是在它自己的进程空间中,与所有其他客户端进程分开。

为此,您需要配置一个 COM 代理,这里有详细说明。

文件映射/共享内存

最后,文件映射允许您操作文件,就好像它只不过是进程地址空间中的一大块内存。 没有繁琐的文件查找; 读/写操作。 只需抓住一个指向内存块的指针并开始读/写。 系统将完成剩下的工作。

微软:

您可以使用文件映射的特殊情况在进程之间提供命名共享内存。 如果在创建文件映射对象时指定系统交换文件,则文件映射对象将被视为共享内存块。 其他进程可以通过打开相同的文件映射对象来访问相同的内存块。 告诉我更多

可悲的是,它仍然需要您首先写入数据,并且要使其最有效,您需要更改应用程序以将内存块视为真实来源,而不是内存中的数组。 否则你将一直在序列化。

但是,通过交换文件共享内存在技术上确实允许您消除客户端-服务器应用程序之间的任何序列化-反序列化以及“堆上”数据的重复。 尽管正如我所说,您可能需要调整您的应用程序以使用原始内存缓冲区而不是对象。

告诉我更多

注意:与流行的看法相反,.NET Remoting并没有完全过时 它的一种现代用途是同一进程内不同AppDomains中的对象之间的通信,这是您通常在plug-in systems

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM