Realization of Windows service running status monitoring based on SIGNALR for B/s system

Source: Internet
Author: User
Tags export class

Generally speaking, a BS project must be more than a separate BS application, which may involve a lot of back-office services to support the operation of BS, especially for some long-time tasks, the Windows service is definitely necessary, we also need to use B/s to interact with Windows services, To achieve a better user experience, with redis,memcached and so on to achieve distributed cache, Message queue processing and so on ...

But usually we at the B/s side is unable to know its dependence on the Windows service is currently in what kind of running state, only through to the server to view, or through other channels!

Today we use SIGNALR to achieve a B/s end of the Windows service running state monitoring, here we used signalr Selfhost, do not rely on IIS, and then use Topshelf to deploy SIGNALR server as a Windows service, Then on the B/S side through the SIGNALR JS client to connect to get service running state!

First create a console application,. NET 4.5,nuget Add Microsoft.AspNet.SignalR.SelfHost Microsoft.Owin.Cors topshelf (for Windows Service installation)

Specific new SignalR Selfhost server method can see my previous blog: SignalR selfhost Real-time messages, integrated into the Web for server message push

Create a new Hub named Servicemonitorhub, inherit Microsoft.AspNet.SignalR.Hub, we want to implement service status 1 seconds push

The specific code is as follows

 1 using System.Linq; 2 using System.Threading; 3 4 Namespace WxRbt.Service.Realtime.Hub 5 {6//<summary> 7//Service Monitor 8//</summary> 9 p             Ublic class ServiceMonitorHub:Microsoft.AspNet.SignalR.Hub10 {One-by-one static Servicemonitorhub () 12 {13                     New Thread (new ThreadStart () =>14 {16 while (true) {17 Get all service names start with WXRBT service-var services = System.ServiceProcess.ServiceController.GetServices ( ). Where (t = = T.servicename.startswith ("WXRBT")) 19.                             Select (t = new Model.service20 {DisplayName = t.displayname,22                         ServiceName = t.servicename,23 Status = (int) t.status24 }). ToArray (); microsoft.aspnet.signalr.globalhost.connectionmanager.gethubcontext<servicemonitorhub& Gt(). Clients.All.refresh (services); 26//Hibernate for one second, enabling push service per second running status System.Threading.Thread.Sle EP (1000); 28}29}). Start (); 30}31}32}

Now let's use Topshelf to install the current console as a Windows service

Create a new class Servicemonitorservice, inherit the Topshelf.servicecontrol interface, implement its start and stop method, the specific code is as follows

 1 using Microsoft.AspNet.SignalR; 2 using Microsoft.Owin.Cors; 3 using Microsoft.Owin.Hosting; 4 using Owin; 5 using System; 6 using Topshelf; 7 using System.Configuration;         8 9 Namespace WxRbt.Service.Realtime.Service10 {One public class Servicemonitorservice:servicecontrol12 {13 Private IDisposable app;14 private static string domain= "http://*:3333"; CE () {configurationmanager.appsettings[= "domain"]? domain;18 Console.WriteLine ("Get Configuration: "+domain"),}20 public bool Start (Hostcontrol Hostcontrol), {Console.WriteLine                 ("The fact message service runs on:" +domain); app = Webapp.start (domain, builder =>26 {27 Builder. Usecors (Corsoptions.allowall); MAPSIGNALR (new HubConfiguration29 {Enablejsonp = true,31 Enable Detailederrors = true,32                     Enablejavascriptproxies = true33}); return true;36                 }37-public bool Stop (Hostcontrol Hostcontrol) (app! = null) {41 App. Dispose ();}43 return true;44}45}46}

This gives a default listening domain name, and then reads the configured listener domain name from App. Config.

Finally open the Progarm.cs file with the following code:

1 using Topshelf; 2  3  4 namespace WxRbt.Service.Realtime 5 {6     class program 7     {8         static void Main (string[] args) 9
   
    {10             Hostfactory.run (s = = {                 s.service<service.servicemonitorservice>);                 Setdisplayname ("Real-time messaging service");                 s.startautomatically ();         }16     }17}
   

Debug Run Program,

Above the server has been completed, below, we have to implement the client:

Create a Mvc4.0web Empty project (casual, personal hobby), NuGet references Microsoft.AspNet.SignalR.JS, the JS relies on jquery, will automatically download jquery, Write typescript students can take the two JS of the D.ts file

Then create a homecontroller and return to view in index

Views folder Create home folder, create a index.cshtml razor attempt, reference jquery with Signalrjs

Then create a separate JS, try not to write JS to the page inside

Here I write a message module with Typescript

 1//<reference path= ". /.. /..         /scripts/typings/signalr/signalr.d.ts "/> 2 3 module Wxrbt.manager {4 Export const enum ServiceStatus {5 /** Service Stop */6 Stopped = 1, 7/** is running */8 startpending = 2, 9/** is stopping */10 Stoppendin         g = 3,11/** running in */12 Running = 4,13/** is continuing */14 continuepending = 5,15/** pausing */16 pausepending = 6,17/** paused */18 Paused = 7,19}20 interface IService {Displaynam e:string;22 servicename:string;23 status:servicestatus24}25/** Management Services */26 Export class Servi         Ce {proxy:signalr.hub.proxy;28 private $: jquerystatic;29 private ip:string;30 Private Port:number;31 Constructor (ip:string, port:number) {This.ip = ip;33 this.p       ORT = port;34}35/**36 * Open service operation status Monitoring PNs * @param {Services} callback38   */39 Start (callback: (services:array<iservice>) = void) {+ Jquery.getscript ("//" +  This.ip + ":" + this.port + "/signalr/hubs", () = {JQuery.connection.hub.url = "/http" + This.ip + ":" + this.port + "/signalr", This.proxy = JQuery.signalR.hub.createHubProxy ("Servicemonitorhub"); 43                     44//Refresh data callback for each this.proxy.on ("Refresh", (services:array<iservice>) + = {46 Callback (services); JQuery.connection.hub.start (). Fail (() =&G T {on alert ("Connect live Message Service period: HTTP//" + This.ip + ":" + This.port + "failed, please verify that the message service is configured correctly and open properly!) "); 52}); 53}); 54}55}56}

The following is the module call I implemented in conjunction with Requirejs

1 require (["message"], () = {2  3     jquery (() = {4  5         var $service = jQuery ("#serviceList"); 6         V Ar msg = new Wxrbt.manager.service ("127.0.0.1", 3333); 7         Msg.start (services=>{8             $service. Empty (); 9 for             (let service of services) {Ten                 var isrunning = Service. Status = = Wxrbt.manager.servicestatus.running;11                 var statuscls = isrunning? "Success": "Warning";                 var statustxt = isrunning? "In operation": "Stopped";                 var status = ' <label class= ' label Label-${statuscls} ' >${statusTxt}</label> '; 14                 $service. Append (' <li><a href= ' javascript:; ' ><i class= ' Icon-check ' ></i>${service. Displayname}${status}</a></li><li class= "divider" ></li> "),             }16         }); 17 18     }); 19 20 21});

Last Run Page view effect:

The only thing that's not enough is 1 seconds. This DropDownList will flash once, I here is first clear again append come in, so will appear this situation, if using DOM node recursive update state will not have this problem!

Because it is a company project, no way on the source! There is no clear can leave a message!

Here are the services running on the Windows Server

Http://www.cnblogs.com/263613093/p/5887636.html

Realization of Windows service running status monitoring based on SIGNALR for B/s system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.