Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate with a windows service?

I want to create a windows service that validates data and access it from another windows application, but I'm new to services and I'm not sure how to start.

So, while the service is running, a windows application should somehow connect to the service, send some data and get a response, true or false.

like image 939
Alexandru Pupsa Avatar asked Dec 15 '10 14:12

Alexandru Pupsa


People also ask

How do Windows communicate with services?

To do this, what you want to do is write your Windows service and a front-end Windows application. To provide the communication bridge between the two, I would strongly recommend using Windows Communication Foundation (WCF). To create a C# Windows service, you can follow the step-by-step instructions here.

What can you do with a Windows service?

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.

How do I run a Windows service?

Start and run the serviceIn Windows, open the Services desktop app. Press Windows+R to open the Run box, enter services. msc, and then press Enter or select OK.

What does it mean to run something as a Windows service?

A Sitecore Host application can run as a Windows Service. A Windows Service is an executable application that the operating system runs in the background. It does not require a logged-in user session to run. In Windows, the Service Control Manager (SCM) manages all Windows service processes.


2 Answers

I could successfully handle the (almost) same issue as yours doing the following:

In your Class : ServiceBase, that represents your Service class, you might have:

public Class () //constructor, to create your log repository     {       InitializeComponent();        if (!System.Diagnostics.EventLog.SourceExists("YOURSource"))       {         System.Diagnostics.EventLog.CreateEventSource(            "YOURSource", "YOURLog");       }       eventLog1.Source = "YOURSource";       eventLog1.Log = "YOURLog";     } 

Now, implement:

protected override void OnStart(string[] args) {...} 

AND

protected override void OnStop() {...} 

To handle custom commands calls:

protected override void OnCustomCommand(int command)     {       switch (command)       {         case 128:           eventLog1.WriteEntry("Command " + command + " successfully called.");           break;         default:           break;       }     } 

Now, use this in the application where you'll call the Windows Service:

Enum to reference your methods: (remember, Services custom methods always receive an int32 (128 to 255) as parameters and using Enum you make it easier to remember and control your methods

private enum YourMethods   {     methodX = 128   }; 

To call a specific method:

ServiceController sc = new ServiceController("YOURServiceName", Environment.MachineName); ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "YOURServiceName");//this will grant permission to access the Service     scp.Assert();     sc.Refresh();      sc.ExecuteCommand((int)YourMethods.methodX); 

Doing this, you can control your service.

Here you can check how to create and install a Windows Service. More about the ExecuteCommand method.

Good luck!

like image 176
user2750836 Avatar answered Sep 18 '22 08:09

user2750836


If you are using .Net Framework 4, then memory mapped files provide a fairly easy way of implementing cross process communication.

It is fairly simple, and well described in documentation, and avoids the overhead (at runtime but also in terms of development effort) of using WCF or other connection/remoting based interactions, or of writing shared data to a central location and polling (database, file, etc).

See here for an overview.

like image 28
Rob Levine Avatar answered Sep 18 '22 08:09

Rob Levine