简体   繁体   English

从Windows服务处理长时间运行的操作

[英]Processing long-running operations from a windows service

Edit (again): Let me simplify my problem. 编辑(再次):让我简化我的问题。 I have a Windows Service that exposes some WCF endpoints with methods like: 我有一个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)
}

What is the best way to implement the ExecuteQuery method? 什么是实现ExecuteQuery方法的最佳方法? Should I just call ThreadPool.QueueUserWorkItem to get my query going? 我应该只调用ThreadPool.QueueUserWorkItem来查询吗?

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浏览器,它可以同时下载多个文件,并且您有一个下载管理器,可以跟踪每个文件的状态。

Take a look at Microsoft Message Queuing (MSMQ) : 看一下Microsoft消息队列(MSMQ)

Microsoft Message Queuing (MSMQ) technology enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline. Microsoft消息队列(MSMQ)技术使在不同时间运行的应用程序可以跨可能暂时脱机的异构网络和系统进行通信。 MSMQ provides guaranteed message delivery, efficient routing, security, and priority-based messaging. MSMQ提供有保证的消息传递,有效的路由,安全性和基于优先级的消息传递。 It can be used to implement solutions for both asynchronous and synchronous messaging scenarios. 它可用于为异步和同步消息传递方案实现解决方案。

It's good to know that Windows Communication Foundation (WCF) can leverage queuing services offered by MSMQ. 很高兴知道Windows Communication Foundation(WCF)可以利用MSMQ提供的排队服务。

I use a Windows service for a very similar task and it works very well. 我将Windows服务用于非常相似的任务,并且效果很好。 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. 我使用数据库表对请求和响应进行排队,因为它为我提供了一个持久队列,可以从远程ASP.Net应用程序通过网络访问该持久队列,并通过事务进行并发控制。

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. 登录到单独的表是查看Web应用程序和本地管理应用程序发生情况的便捷方法。

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. 我不会将ThreadPool用于长时间运行的线程,而是创建一个在其自己的线程中运行并使用回调方法用进度和完成状态更新主管的工作器类。

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. 这是一个棘手的问题,也不是一件容易的事…… ThreadPool.QueueUserWorkItem是有关要同时执行一段代码的最简单方法。 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 ? 因此,如果这不是一个棘手的问题,那么您是否正在确切地询问如何在ThreadPool.QueueUserWorkItem传递查询?

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. 如果需要考虑将来的可扩展性,那么除了考虑MSMQ的问题外,您还可以考虑使用企业服务总线(ESB)处理这类事情。 Check out NServiceBus for one .NET example. 查看NServiceBus中的一个.NET示例。

I would use WWF (4.0): 我会使用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... 您可以启动可以在几台机器中处理的长期运行的事务,并行执行任务,故障支持,友好的编码,您可以使用appfabric对其进行管理,它是免费的...

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

相关问题 使用多个长时间运行的操作优化ASMX Web服务 - Optimizing an ASMX web service with Multiple Long-Running Operations 在Windows服务中运行长时间运行的任务 - Running a long-running Task within a Windows Service 关闭Windows服务中的长时间运行的进程 - Shutting down a Long-running process in a Windows Service 长期运行的WCF服务即将终止的原因是什么? - Reasons for long-running WCF service dying? 创建长期运行的工作流服务 - Creating a long-running Workflow Service 如何取消长时间运行但单线程的操作 - How to cancel long-running but single-threaded operations 在长期运行的Windows服务中进行依赖注入-组合根源是正确的主意吗? - Dependency injection in long-running Windows service - Composition root the right idea? Windows Service Application仅执行一次长时间运行的任务,并在完成后自行停止 - Windows Service Application to perform long-running task only once and stop itself after completion 如何使用Entity Framework处理长时间运行的操作? - How to handle long-running operations with the Entity Framework? 长时间运行的异步操作,无IO线程 - Long-Running Asynchronous Operations, Without IO threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM