[dba-VB] A windows Service or Not a service?

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Thu Jun 16 12:50:06 CDT 2011


Hi Francisco,

I have written a few real life WinServices working 24x7x365 for a few years.
When a Windows Service Project is created its base class has code snippet as
the following:

public partial class MyService : ServiceBase
{
 ...
       protected override void OnCustomCommand(int command)
        {
	switch(command)
             {
                case 503:
                      CustomCommand503();
                      return;
                 ...
              }	
        }
} 

It allows to define as many custom commands as you wanted to have.
You comunicate with a Windows Service using that custom commands' IDs - here
is a code snippet showing how to find your installed and runing Windows
Service and to pass to it your custom command ID:

public static void RunCustomServiceCommand(string serviceName, int
commandId)
{
    foreach (System.ServiceProcess.ServiceController service in
                System.ServiceProcess.ServiceController.GetServices())
    {
        // juts a test output - commented it when not needed
        System.Console.WriteLine("[{0}].[{1}].[{2}]", 
                                    service.MachineName, 
                                    service.ServiceName, 
                                    service.DisplayName);     
        if (string.Compare(service.ServiceName, serviceName, true) == 0)
        {
            service.ExecuteCommand(commandId);  
        }
    }
}

When I need to pass to a Windows Service more parameters than command id
then I use inderect/intermediate media: files, database tables, ... 

Another issue is to smoothly install, run, stop, uninstall  a Windows
service - there should be more info on Internet and MSDN about that
issues...

Thank you.

--
Shamil
 
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Francisco Tapia
Sent: 16 ???? 2011 ?. 19:58
To: Discussion concerning Visual Basic and related programming issues.
Subject: [dba-VB] A windows Service or Not a service?

I wrote a simple application that has a form and two buttons (along with a
timer).  My initial idea was that I would write this to be a windows service
so it would auto-start whenever a specific server would reboot etc.  The
purpose is that I need to get new records created by a "Vendor" application
and have them sent to a Second "Vendor" system.  I call a custom view that I
wrote that collects that last set of changes, then record by record calls a
webservice on our SAP system and pushes in the new changes.  My sql server
version is Sql Server 2000 so it's not as easy as just writing a soap object
to make the webservice call, I think it was much easier to just write the
app in c# and make the call that way, which works.  NOW for the tricky part.
I wanted to write up the app to create a windows service installer so that
my admins could log into the windows server and either start or stop the
application, I also wanted to provide a gui view to the windows service so
that they could choose a different timer so it could run every 1 minute
(every 2 minutes, etc..) what ever they would choose.

after reading MS's description on windows service classes, I noticed that
supposedly the .Net Studio does not support this, but I've seen other apps
that DO provide a gui view to the windows service that they introduce.  so
my question to the group is, have you done this before? and if so how did
you go about it?

--
http://msdn.microsoft.com/en-us/library/d56de412%28v=VS.90%29.aspx

The Windows service classes supported by the .NET Framework do not support
interaction with interactive stations, that is, the logged-on user. The .NET
Framework also does not include classes that represent stations and
desktops. If your Windows service must interact with other stations, you
will need to access the unmanaged Windows API. For more information, see
Window
Stations<http://msdn.microsoft.com/en-us/library/ms687096%28v=VS.90%29.aspx>
and
Desktops<http://msdn.microsoft.com/en-us/library/ms682573%28v=VS.90%29.aspx>
in
the Platform SDK documentation.


-Francisco





More information about the dba-VB mailing list