网站与 Windows 服务之间的通信
我有一个用 C# (MVC) 编写的网站和一个用 C# 编写的 Windows 服务。两者都运行在同一台机器上。想法是让他们共享同一个数据库。
Windows 服务充当引擎,而网站更像是显示引擎执行的计算结果的前端。通信由从 Web 服务器到 Windows 服务的命令组成,数据很少。预计每秒的命令很少。
他们沟通的好方式是什么?
I have a web site written in c# (MVC) and a Windows service written in c#. Both running on the same machine. Idea is for them to share the same database.
The Windows service acts as the engine and the web site is more of a front end showing results of calculations performed by the engine. Communication consist of commands with little data from web server to the windows service. Very few commands / second is to be expected.
What would be a good way for them to communicate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,Windows 端最快的是 LRPC。根据您的需求,WCF 可能是一个真正的问题。我们使用 Win32 LRPC + protobuffers 使从 Web 服务器到后台服务的吞吐量提高了约 40 倍。只是取决于你的需要。
(请参阅WCF 与 Protocol Buffers + RpcLibrary 的基准测试
) href="http://code.google.com/p/protobuf-csharp-port/" rel="nofollow">protobuf-csharp-port 有定义服务和消息所需的大部分内容。然后使用 protobuf-csharp-rpc 通过 Win32 LRPC 传输层。
一旦你完成了在 protobuffers 中定义协议的工作,这真的很容易。在客户端/服务器上只需要几行代码即可创建连接和代理类。
同样,这完全取决于您要寻找什么;然而,恕我直言,目前 .NET Framework 还没有更好的 RPC 架构。当然,我的观点有偏见,但自 2003 年以来我们一直在使用它的变体,而且它确实有效。
PS:如果您从头开始构建服务,您可以查看我的指南 构建 Windows 服务项目模板。通过真实事件日志记录、安装和命令行测试,它可以让您相当快速地启动和运行。
Generally the fastest on the Windows side is going to be LRPC. Depending on your needs WCF can be a real problem. We use Win32 LRPC + protobuffers to get about 40x faster throughput from web server to background service. Just depends on you're needs.
(see Benchmarking WCF compared to Protocol Buffers + RpcLibrary)
The protobuf-csharp-port has most of what you need for defining the service and messages. Then using protobuf-csharp-rpc to provide the message serialization over a Win32 LRPC transport layer.
Once you get past defining the protocol in protobuffers this is really easy. It takes only a few lines of code on the client/server to create the connection and proxy class.
Again, it all depends on what you're looking for; however, IMHO there is not currently a better RPC architecture out there for the .NET Framework. I am, of course, biased in my opinion but we've been using variations of this since 2003 and it just works.
PS: If you're building the service from scratch you might look at my guide to Building a Windows Service Project Template. It will get you up and running fairly quickly with real event logging, installation, and command-line testing.
这完全取决于你想做什么。有许多可用选项:您可以在 Windows 服务中托管(无论是否稳定)Web 服务,您可以使用远程处理或 WCF 等。
但这一切都始于一个问题:为什么您需要它们两者?是什么阻止您将 Windows 服务的功能转移到 Web 应用程序中?
It all depends on what do you want to do. There is a number of options available: you can host a (restful or not) web service in Windows Service, you can use Remoting or WCF, etc.
But it all starts with a question: why do you need them both? What prevents you from moving the functionality of the Windows service into the Web App?
您始终可以“以其他方式”执行此操作 - 让您的网站运行 Web 侦听器并让 Windows 服务连接到它。
You could always do it 'the other way' - have your web site run a web listener and have the Windows Service connect to it.