简体   繁体   English

使用序列化在两个进程/应用程序之间进行对话(进程间通信)

[英]Using serialization to talk between two process/applications (Inter Process Communication)

Recently I was asked the following in a Interview:最近我在面试中被问到以下问题:

There is a process/application A which is constantly getting messages every 5 Minutes (Assume)有一个进程/应用程序 A 每 5 分钟不断地获取消息(假设)

The requirement is that there is a process/application B which needs the messages to be transferred from A.要求是有一个进程/应用程序 B 需要从 A 传输消息。

But process/application A should make sure that the message has actually been received by process/application B.但是进程/应用程序 A 应该确保消息实际上已被进程/应用程序 B 收到。

The interviewer wanted to know how can this be implemented in C#.面试官想知道如何在 C# 中实现这一点。 Can we use Serialization here?我们可以在这里使用序列化吗?

From my understanding the best way this can be done is using message queue or pipe mechanism.根据我的理解,最好的方法是使用消息队列或管道机制。

Anything that involves sending messages between processes (or, generally, even between AppDomains in a single process) has to use serialization of some kind, as ultimately the data needs to be mapped down to something that can traverse process spaces, and a "Message" is, by definition, serialized data.任何涉及在进程之间发送消息(或者,通常,甚至在单个进程中的 AppDomains 之间)必须使用某种序列化,因为最终需要将数据映射到可以遍历进程空间的东西,以及“消息”顾名思义,就是序列化数据。 This serialization could be explicit or implicit (automatic, perhaps by a proxy/stub layer - although I'd prefer not).这种序列化可以是显式的,也可以是隐式的(自动的,可能是通过代理/存根层 - 尽管我不喜欢)。

In reality, Process/Application A cannot "make sure" that it has been delivered, since it can't enforce that the second process is running.实际上,进程/应用程序 A不能“确保”它已经交付,因为它不能强制第二个进程正在运行。 It can, however, have some kind of callback confirmation message perhaps.然而,它可能有某种回调确认消息。 A transactional message queue isn't unreasonable , but that only ensures that it gets queued - it doesn't ensure that it gets processed (ever).事务性消息队列并非不合理,但这只能确保它被排队——它并不能确保它得到处理(永远)。 Personally, I'd see if a socket would be sufficient, first.就个人而言,我会先看看套接字是否足够。

There are some possible ways for IPC in .NET: .NET 中 IPC 有一些可能的方法:

  1. Memory-Mapped Files in .NET 4 .NET 4 中的内存映射文件
  2. .NET Remoting .NET 远程处理
  3. Using sockets and the loop-back (127.0.0.1)使用套接字和环回 (127.0.0.1)

The most easiest one is using Memory-Mapped files in .NET 4.0.最简单的一种是在 .NET 4.0 中使用内存映射文件。
You can use serialization or any message representation format eg XML for messaging.您可以使用序列化或任何消息表示格式(例如 XML)进行消息传递。
This is about IPC but if you want to make sure that where the request is sent from (process B or another process) you must use a signature mechanism.这是关于 IPC 但如果你想确保请求是从哪里发送的(进程 B 或另一个进程),你必须使用签名机制。

Serialization is (in short) a process of creation of a Message.序列化(简而言之)是创建消息的过程。 However if you're interested in making sure that the Message reaches its destination (and does it in consistent state), you should rather ask about communication protocol - that's the thing that describes how the Message will actually be sent.但是,如果您有兴趣确保消息到达其目的地(并以一致的状态执行),您应该询问通信协议 - 这是描述消息实际发送方式的内容。 For example UDP protocol doesn't guarantee delivery and TCP does.例如,UDP 协议不保证交付,而 TCP 保证。

It's also worth mentioning that guaranteed delivery means that it is guaranteed that either System B receives the Message intact, or System A will be informed about a failure in delivery (when for example System B is off).还值得一提的是,保证传递意味着可以保证系统 B 完整地接收消息,或者系统 A 将被告知传递失败(例如,当系统 B 关闭时)。 That said, some Message Queues guarantee delivery as well - there's a timeout in which when a Massage in queue did not reach its destination, the Message will be marked as failed(for example by putting it in Failed Messages queue).也就是说,一些消息队列也能保证传递——有一个超时,当队列中的按摩没有到达目的地时,消息将被标记为失败(例如,将其放入失败的消息队列)。

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

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