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.
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!
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.
You could accomplish this very easily by making the service host a WCF service and connecting to it from your application.
We use Named pipes for this purpose. But our client implemented with C++. If your service and application are implemented in .Net, you can use .Net remoting.
In older versions of Windows, you could configure your Windows service to interact with the desktop. This allowed you to add user interface elements directly to your service that could be presented to the user. Beginning with Windows Vista, services can no longer interact directly with users, i.e., no user interfaces.
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.
Think it as a remote database owner. Say you have 1 database yet 10 applications that requires different data's from the database and also you don't want to open all your data to each of the applications.. Also your applications will be independent from your database, your data layer will only be implemented in your service, your applications will not hold that logic. You can write a service and open your service to your other applications.
How to write your first windows service can help you.
Your service, while it is processing, can add events to the EventLog.
You can create another console application that runs paralel to the service, and that listens to that EventLog with the event handling mechanism:
var log= new EventLog("[name of the eventlog]");
log.EnableRaisingEvents = true;
log.EntryWritten += Log_EntryWritten;
Then you handle it immediately:
private static void Log_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
{
Console.WriteLine("Event detected !");
}
You can read the EntryWrittenEventArgs object to get all event details, and to show what you want in your console app. If you stop the console app the service continues to run, and still logs to the event log.
来源:https://stackoverflow.com/questions/4451216/how-to-communicate-with-a-windows-service