使用Windows服务的Web API通过轮询接收命令并执行任务? [英] Using Web API for a Windows Service to Receive Commands and Perform Tasks via Polling?

查看:146
本文介绍了使用Windows服务的Web API通过轮询接收命令并执行任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我需要创建一个Windows服务,该服务在通过命令指示时将执行各种任务.该服务器可以在多台服务器上运行,并且在请求时可以有效地执行相同类型的任务.

I have a project where I need to create a windows service that, when instructed via a command, will perform various tasks. This server would run on multiple servers and would effectively perform the same kind of tasks when requested.

例如,我想拥有一个Web API服务,该服务侦听来自服务器的请求.

For example, I would like to have a Web API service that listens for requests from the servers.

服务器上运行的服务将每25秒左右向Web API发送一次查询,并将其SERVERNAME传递给它.然后,Web API逻辑将查找SERVERNAME并查找各种任务的任何状态更新... IE,如果DELETE命令的状态为1,则服务将删除包含日志文件的文件夹...如果状态为如果ZIP命令为1,则服务会将包含日志文件的文件夹压缩并通过FTP将其传输到集中位置.

The service running on the server would send a query to Web API every 25 secs or so and pass to it its SERVERNAME. The Web API logic will then look up the SERVERNAME and look for any status updates for various tasks... I.E., if a status for a DELETE command is a 1, the service would delete the folder containing log files... if a status for a ZIP command is a 1, the service would zip the folder containing log files and FTP them to a centralized location.

这个概念似乎很简单,我想我需要轻声告诉我这听起来像是一个好的设计.我正在考虑将.NET 4.5用于Windows服务,以便可以将HttpClient对象使用,当然也可以将.NET 4.5用于Web API/MVC项目.

This concept seems simple enough, and I think I need a nudge to tell me if this sounds like a good design. I'm thinking of using .NET 4.5 for the Windows Service, so that I can use the HttpClient object and, of course, .NET 4.5 for the Web API/MVC project.

有人可以帮助我开始介绍基本的Web API外观,为正在运行的Windows服务提供状态更新并向其发出命令...

Can someone please get me started on what a basic Web API woudld look like provide status updates to the Windows services that are running and issue commands to them...

我正在考虑建立一个简单的MVC网站,人们将拥有一个服务器列表(可能基于简单的XML文件或类似的东西),他们可以单击各种单选按钮以打开"DELETE","ZIP"或无论如何,以触发服务上的任务.

I'm thinking of having a simple MVC website that folks will have a list of servers (maybe based on a simple XML file or something) that they can click various radio buttons to turn on "DELETE", "ZIP" or whatever, to trigger the task on the service.

推荐答案

我做了类似的事情.我有一个主要的Web API(Windows服务),该API驱动我的应用程序,并具有一个名为/Heartbeat的资源.

I do something similar. I have a main Web API (a Windows Service) that drives my application and has a resource called /Heartbeat.

我还有第二个Windows服务,该服务每30秒触发一次计时器.每次计时器触发时,它都会调用POST /heartbeat.处理心跳请求后,它将继续查找已计划的任务.

I also have a second Windows Service that has a timer fire every 30 seconds. Each time the timer fires it calls POST /heartbeat. When the heartbeat request is handled, it goes looking for tasks that have been scheduled.

此方法的优点是该服务使监听信号请求非常简单,并且无需更新.与心跳所发生的事情有关的所有逻辑都在主要服务中.

The advantage of this approach is that the service makes the hearbeat request is extremely simple and never has to be updated. All the logic relating to what happens on a heartbeat is in the main service.

服务的胆量是这个.它是旧代码,因此仍使用HttpWebRequest而不是HttpClient,但这很微不足道.

The guts of the service are this. It's old code so it is still using HttpWebRequest instead of HttpClient, but that's trivial to change.

 public partial class HeartbeatService : ServiceBase {
        readonly Timer _Timer = new System.Timers.Timer();

        private string _heartbeatTarget;


        public HeartbeatService() {
            Trace.TraceInformation("Initializing Heartbeat Service");
            InitializeComponent();
            this.ServiceName = "TavisHeartbeat";
        }

        protected override void OnStart(string[] args) {
            Trace.TraceInformation("Starting...");
            _Timer.Enabled = true;

            _Timer.Interval = Properties.Settings.Default.IntervalMinutes * 1000 * 60; 
            _Timer.Elapsed += new ElapsedEventHandler(_Timer_Elapsed);

            _heartbeatTarget = Properties.Settings.Default.TargetUrl; 
        }

        protected override void OnStop() {
            _Timer.Enabled = false;
        }

        private void _Timer_Elapsed(object sender, ElapsedEventArgs e) {
            Trace.TraceInformation("Heartbeat event triggered");
            try {

                var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(_heartbeatTarget);
                httpWebRequest.ContentLength = 0;
                httpWebRequest.Method = "POST";
                var response = (HttpWebResponse)httpWebRequest.GetResponse();
                Trace.TraceInformation("Http Response : " + response.StatusCode + " " + response.StatusDescription); 
            } catch (Exception ex) {
                string errorMessage = ex.Message;
                while (ex.InnerException != null) {
                    errorMessage = errorMessage + Environment.NewLine + ex.InnerException.Message;
                    ex = ex.InnerException;
                }
                Trace.TraceError(errorMessage);
            }

        }


    }

这篇关于使用Windows服务的Web API通过轮询接收命令并执行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆