c# - Using Web API for a Windows Service to Receive Commands and Perform Tasks via Polling? -
c# - Using Web API for a Windows Service to Receive Commands and Perform Tasks via Polling? -
i have project need create windows service that, when instructed via command, perform various tasks. server run on multiple servers , perform same kind of tasks when requested.
for example, have web api service listens requests servers.
the service running on server send query web api every 25 secs or , pass servername. web api logic servername , status updates various tasks... i.e., if status delete command 1, service delete folder containing log files... if status zip command 1, service zip folder containing log files , ftp them centralized location.
this concept seems simple enough, , think need nudge tell me if sounds design. i'm thinking of using .net 4.5 windows service, can utilize httpclient object and, of course, .net 4.5 web api/mvc project.
can please me started on basic web api woudld provide status updates windows services running , issue commands them...
i'm thinking of having simple mvc website folks have list of servers (maybe based on simple xml file or something) can click various radio buttons turn on "delete", "zip" or whatever, trigger task on service.
i similar. have main web api (a windows service) drives application , has resource called /heartbeat
.
i have sec windows service has timer fire every 30 seconds. each time timer fires calls post /heartbeat
. when heartbeat request handled, goes looking tasks have been scheduled.
the advantage of approach service makes hearbeat request extremely simple , never has updated. logic relating happens on heartbeat in main service.
the guts of service this. it's old code still using httpwebrequest instead of httpclient, that's trivial 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"); seek { 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); } grab (exception ex) { string errormessage = ex.message; while (ex.innerexception != null) { errormessage = errormessage + environment.newline + ex.innerexception.message; ex = ex.innerexception; } trace.traceerror(errormessage); } } }
c# asp.net-mvc windows-services asp.net-web-api
Comments
Post a Comment