从 Windows 服务处理长时间运行的操作
编辑(再次):让我简化我的问题。我有一个 Windows 服务,它公开一些 WCF 端点,其方法如下:
int ExecuteQuery(string query) {
// asynchronously execute query that may take 1 second to 20 minutes
return queryId;
}
string GetStatus(int queryId) {
// return the status of the query (# of results so far, etc)
}
实现 ExecuteQuery 方法的最佳方法是什么?我应该调用 ThreadPool.QueueUserWorkItem 来让我的查询继续吗?
请注意,执行查询背后的实际工作是由负载平衡黑匣子完成的。我希望能够同时进行多个查询。
类比是一个同时下载多个文件的网络浏览器,并且您有一个可以跟踪每个文件状态的下载管理器。
Edit (again): Let me simplify my problem. I have a Windows Service that exposes some WCF endpoints with methods like:
int ExecuteQuery(string query) {
// asynchronously execute query that may take 1 second to 20 minutes
return queryId;
}
string GetStatus(int queryId) {
// return the status of the query (# of results so far, etc)
}
What is the best way to implement the ExecuteQuery
method? Should I just call ThreadPool.QueueUserWorkItem
to get my query going?
Note that the actual work behind executing a query is done by load-balanced black box. I want to be able to have several queries going at the same time.
The analogy is a web browser that is downloading multiple files simultaneously and you have a download manager that can track the status of each file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看一下 Microsoft 消息队列 (MSMQ):
很高兴知道 Windows Communication Foundation (WCF) 可以利用 MSMQ 提供的队列服务。
Take a look at Microsoft Message Queuing (MSMQ):
It's good to know that Windows Communication Foundation (WCF) can leverage queuing services offered by MSMQ.
这要么是一个棘手的问题,要么是一个简单的问题...当您想要同时执行一段代码时,
ThreadPool.QueueUserWorkItem
是最简单的方法。我相信您已经知道这一点,所以从技术上讲您已经回答了自己的问题。因此,如果这不是一个棘手的问题,那么您到底是在问如何在 ThreadPool.QueueUserWorkItem 中传递查询吗?
Either this is a trick question or a no-brainer...
ThreadPool.QueueUserWorkItem
is about the easiest way to go when you want to execute a piece of code concurrently. I'm sure you already knew that, so technically you have already answered your own question.So if this is not a trick question, then are you asking exactly how to pass the query in the
ThreadPool.QueueUserWorkItem
?我使用 Windows 服务来完成非常类似的任务,并且效果非常好。我使用数据库表对请求和响应进行排队,因为它为我提供了一个持久队列,可以通过网络从远程 ASP.Net 应用程序访问该队列,并通过事务进行并发控制。
每当传入的请求需要服务时,计时器上的主管线程就会生成工作线程。我使用单独的数据库表进行配置和控制,以便我可以管理服务并从应用程序中暂停主管,而无需让服务核心保持运行。记录到单独的表是查看 Web 应用程序和本地管理应用程序发生的情况的便捷方法。
我不会将 ThreadPool 用于长时间运行的线程,而是创建一个在自己的线程中运行的工作器类,并使用回调方法来更新主管的进度和完成状态。
I use a Windows service for a very similar task and it works very well. I use database tables to queue requests and responses, as it gives me a persistent queue that can be accessed over the network from remote ASP.Net applications, and concurrency control through transactions.
A supervisor thread on a timer spawns workers whenever incoming requests need servicing. I use a separate database tables for configuration and control so that I can administer the service and pause the supervisor from an application without while leaving the service core running. Logging to a separate table is a convenient way to see what's happening from web apps and a local admin app.
I wouldn't use the ThreadPool for long-running threads, but instead create a worker class that runs in its own thread and uses callback methods to update the supervisor with progress and completion status.
除了 MSMQ 答案之外,如果未来的可扩展性是一个问题,您可以考虑使用企业服务总线 (ESB) 来处理此类事情。查看 NServiceBus 了解一个 .NET 示例。
Adding to the MSMQ answer, you could think about looking at using an Enterprise Service Bus (ESB) to handle these sorts of things, if future scalability is a concern. Check out NServiceBus for one .NET example.
我会使用WWF(4.0):
您可以启动可以在几台机器上处理的长时间运行的事务,并行执行任务,故障支持,友好的编码,您可以使用appfabric管理它,它是免费的......
I would use WWF (4.0):
You can start long running transactions that can be handle in a few machines, execute task in parallel, failure support, friendly coding, you can manage it with appfabric, it is free...