使用 WCF 服务对请求进行排队
我有一个 WCF 服务,每分钟(或秒)可以接收多个需要将信息写入数据库的请求。我不想同步写入,而是希望将这些请求放置在服务器上的某种队列中,以便另一个进程可以出现并处理它们。客户端只需要确认已收到请求。我已经阅读了很多有关 MSMQ
和 WCF
等的内容,但似乎使用 MSMQ
您可以从客户端写入队列,而不是写入网络服务,这不是我想要的。
有没有办法在不涉及数据库的 WCF 方法中执行以下操作。也许我还没有正确理解MSMQ
的概念。
public bool ProcessMessage(string message)
{
if(IsValid(message))
return AddToQueue(message);
return false;
}
编辑:我需要在写入队列之前验证消息。
I have a WCF service
that can receive several requests/minute (or seconds) that need to write information to the database. Rather than write it synchronously, I would like to place these requests in some sort of a queue on the server so that another proces can come along and process them. The client just needs an acknowledgement that the request was received. I have read a lot about MSMQ
and WCF
etc, but it seems that with MSMQ
you write to the queue from client and not to the web service, which is not what I want.
Is there a way to do the following inside a WCF method that does not involve a database. Perhaps i have not grasped the concept of MSMQ
right.
public bool ProcessMessage(string message)
{
if(IsValid(message))
return AddToQueue(message);
return false;
}
EDIT: I need to validate the message before writing to the queue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前在我创建的应用程序中执行此操作。 WCF 服务作为 HTTP 服务托管在 IIS 上。它接受调用和数据包,我获取该数据,验证它(告诉调用者它是否错误),然后将数据发送到另一个使用 netMSMQ 绑定的 WCF 服务,然后该服务对数据库进行最终写入。这样做的好处是它将在一个 MSMQ 上排队,并且绑定到该 MSMQ 的 WCF 服务一次弹出一条消息并处理它。然后,HTTP WCF 服务可以处理任意数量的请求,并且不必担心汇集的消息,因为这是 WCF/MSMQ 绑定服务的工作。这种模式的通用名称是 Bridge 框架。
ETA:第二个服务(MSMQ 绑定的 WCF 服务)作为 Windows 服务始终运行。它还处理关注点分离。 HTTP 服务验证并且不关心数据库,其他服务处理对数据库的写入。
I do this currently in an application I created. A WCF service is hosted as an HTTP Service on IIS. It accepts calls, and packets of data, I take that data, validate it (tell the caller it's wrong or not) then send the data to another WCF service that is using netMSMQ binding, that service then does the final writing to the database. The good thing about this is it will queue up on one MSMQ and the WCF Service that is bound to this MSMQ pops off one message at a time and processes it. The HTTP WCF service can then handle as many requests as it wants and does not have to worry about pooled up messages as that's the job of the WCF/MSMQ-bound service. The common name for this pattern is a Bridge framework.
ETA: the second service (the MSMQ-bound WCF Service) is run as a Windows service always on. It also handles separation of concerns. The HTTP service validates and does not care about the database, the other service handles writing to the Database.
使用 MSMQ 的目的应该是让您的服务无需担心任何排队问题。 MSMQ 将保证您的消息以正确的顺序传递,并且您的服务以正确的顺序处理它们。
如果您设置正确,您的服务根本不应该维护队列。
The point of using MSMQ should be to remove the need for your service to worry about queueing anything. MSMQ will guarantee that your messages get delivered in the proper order and that your service processes them in the proper order.
Your service shouldn't maintain a queue at all if you set this up properly.