IPC: WM_COPYDATA +序列化/反序列化
我目前正在开发 2 个必须相互通信的 .NET 应用程序。选择了简单的 Windows 消息传递,目前效果很好。 消息中发送的数据是一个简单的字符串,但现在我创建了一个消息类,其中包含命令(枚举)和数据(字符串)成员,以及稍后可能的其他成员。
当我发送此类消息类的实例时,它会被序列化为字节,然后转换为 Base64 字符串。然后使用 Windows 的 SendMessage() 将其发送出去。 在另一边我做相反的事情。最终,原始对象将被恢复并在其他应用程序中可用。
虽然这种机制似乎有效,但我想知道这样做是否安全。 事实上,有一些开销,base64 字符串比原始字符串解决方案长得多(但我必须手动解析这个字符串以获取命令和数据部分) 使用 SendMessage 发送的消息是否有最大大小?
另外,我宁愿在这个项目中远离 .NET 远程处理,而继续使用 SendMessage 解决方案。
有什么想法吗?也许使用 JSON 来限制开销?
谢谢。
鼠兔
I am currently working on 2 .NET apps which must communicate with each other. Simple Windows Messaging was chosen and this works nicely at the moment.
The data that is sent in the messages is a simple string, but now I have created a message class which contains a command (enum) and a data (string) member, and later possible other members.
When I send an instance of such an message class, it is serialized to bytes and then converted to a base64 string. This is then sent out using Windows' SendMessage().
On the other side I do the opposite. Eventually, the original object is restored and available in the other app.
While this mechanism seems to work, I was wondering if this is safe to do.
Indeed, there is some overhead, base64 string is much longer than the original string solution (but I would have to parse this tring manually to get command and data part)
Is there a maximum size for messages that can be sent with SendMessage ?
Also I'd rather stay away from .NET remoting for this project and stay with the SendMessage solution.
Any idea's ? Maybe use JSON instead to limit the overhead ?
Thanks.
Pika
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用支持 WM_COPYDATA、命名管道和 IOStream 消息传递的 XDMessaging (http://xdmessaging.codeplex.com/) - 提供高度灵活、强大且经过测试的解决方案,而不是自己滚动一些东西。
如何在您的类上使用二进制序列化 (http: //msdn.microsoft.com/en-us/library/4abbf6k0(v=VS.71).aspx) 与 XDMessaging?它将非常紧凑并且非常容易实现。
Use XDMessaging (http://xdmessaging.codeplex.com/) which supports WM_COPYDATA, Named pipes and IOStream messaging- providing a highly flexible, robust and tested solution, instead of rolling something yourself.
How about using Binary Serialization on your class (http://msdn.microsoft.com/en-us/library/4abbf6k0(v=VS.71).aspx) with XDMessaging? It would be quite compact and very easy to implement.
如果您只需要发送消息,请查看消息总线架构,例如 Rhino Service Bus 或NServiceBus。他们将提供相对简单且更健壮的消息传递实现。
If you just need to send messages look at a message bus architecture like Rhino Service Bus or NServiceBus. They will provide a relatively simple and much more robust messaging implementation.
我真的建议使用更现代、可扩展的方法。例如,您可以轻松使用命名管道 进行通信,只需使用标准流功能,而不是尝试发送需要编码的消息。
(就我个人而言,我可能会使用 WCF,因为这样有很多好处,包括允许它免费在不同的机器上工作......)
I'd really recommend using a more modern, scalable approach. For example, you could easily use named pipes to communicate, and just use standard stream functionality instead of trying to send messages that need to be encoded.
(Personally, I'd probably use WCF, as that has so many benefits, including allowing this to work across different machines for free...)