|
Home - Computerkurs Demos Editpointstatic Folderwatcher Gipsydrive Licenses Shrinkseries V4 More... All Asm Bas C C++ C# Couch CSS HTML Java JS Lisp OPAL PHP Perl PS Py SQL Win Arrays Files Processes RegEx AccessIE Linq WindowsServices SimpleGuiAccess WpfTreeView |
|
Downtown Programmer's Corner - Demos - C#
A Windows service is an application running in it's own context without user interaction from the beginning of the session. A service first must be installed, then it can be started, paused, continued, stopped, uninstalled.
Creating a service application is easy with the help of Visual Studio wizzard, who offers a Service Template for new projects. (But perhaps "the Windows Service template and associated functionality is not available in the Standard Edition of Visual Studio" - as MSDN points out.)
From the tutorials and sample codes I found (see links far below), I liked most the one from Switch On The Code. This one is not based on a Visual Studio Service Template, but it starts as a Console Project. For my taste, this approach has tree advantages: (1) You see more clearly, what really makes a service different from a ordinary application and (2) it gives perhaps more finegrained control and (3) you can perhaps better implement debug features (e.g. toggling between a 'console mode' for debugging and a 'service mode' for running as service then).
.
In folder demoscs/trunk/mywinsvc/MyWindowsService we work off the Switch On The Code tutorial Creating a Simple Windows Service in C# . The tutorial says it all, so there is nothing more to say, except little supplements perhaps.
This project was done under Windows XP SP3 with .NET Framework 4.0 plus the .NET Framework 4.0 SDK installed. The IDE was SharpDevelop run from Gipsydrive.
Recall the steps in detail:
(Step 1) Create a console application.
(Step 2) Add using System.ServiceProcess; (so we can inherit from ServiceBase).
(Step 3) Make the program class it inherit from ServiceBase.
(Step 4) Overwrite OnStart method.
(Step 5) Overwrite OnStop method.
(Step 6) Supplement constructor, giving the service a name.
(Step 7) Create an instance of the service and run it.
Sure, now we execute our newly built executable:
After implementing the service, but not yet the service installer,
you have a unique chance to see this Windows dialog.
The Windows error dialog telling us the service installer is missing.
(Step 8) Add installer class.
(Step 9) Add using System.Configuration.Install;.
(Step 10) The class needs to extend Installer (inherit from).
(Step 12) Add using System.ComponentModel; for attribute [RunInstaller(true)].
(Step 11) Prefix the class with attribute [RunInstaller(true)] because the installer looks for it.
(Step 12) Fill code into installer class constructor.
(Step 12.1) Create a ServiceProcessInstaller.
(Step 12.2) Create a ServiceInstaller.
(Step 12.3) Set account priviledges (e.g. User needs credentials, LocalSystem has much priviledges).
(Step 12.4) Identify service by name.
(Step 12.5) Add ServiceProcessInstaller to the installer collection.
(Step 12.6) Add ServiceInstaller to the installer collection.
The source code is complete now. After some zigzag with the references and usings, the projectis building successfully.
(Step 13) Locate installutil.exe on the machine.
I spotted installutil.exe (at C:\WINDOWS\ServicePackFiles\i386) and inspected it's help screen. Fine.
(Step 14) Run installutil.exe with parameter MyWindowsService.exe.
But when finally attempting the installation, I got the error "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.."
The installer refuses to install MyWindowsService.
What does this mean?! The SharpDevelop project settings are Compiler Version = C# 4.0 and Target Framework = .NET Framework 4.0 Client Profile. I downgrade the project to Compiler Version = C# 3.0 and Target Framework = .NET Framework 3.5 Client Profile.
This helps for above error. Just to hit the next one:
Error "The Source was not found, but some some or all event logs
could not be searched. Inacessible logs: Security."
The two logfiles mentioned in the above console output contain exactly the text we see in the console. So they are no more help. It's something with the acess rights, isn't it? Let's try a console with admin rights! Voila:
Success!
In "Administrative Tools - Services", indeed I find MyWindowsService.
When pressing the Start Button, it starts ...
... and succeeds with the start.
On pressing the Stop Button, guess what, it stops.
And finally, the installer successfully uninstalls the service.
Fine!
What next? My question is, how to run installutil.exe be from within mywindowsservice.exe, so the user (me) needs not explicitly locate it and tinker with it on the commandline. Perhaps the important task may be, to spot installutil.exe on the system, and react if it is not available.
I need to know more about that error, which was cured by downgrading the C# and .NET. How can this be, when on my machine is .NET 4.0 installed, that it does complain about a .NET 4.0 service?!
.
.
General knowhow about Windows Services:
|
|
MSDN: Windows Service Applications and it's subchapters Introduction Walkthrough Architecture Howto: Create Write Installers Security Uninstall Start Pause Continue Debug Log Troubleshooting: Debugging Installing | This is the primary knowledge source about Windows Services. So this pages are often referred in the other example code articles. | Official statements |
| . | . | . | . |
Tutorials and sample codes:
|
|
Switch On The Code: Creating a Simple Windows Service in C# | This example is based on a console application, as opposed to most others which are based on the Visual Studio Service template (which may not always be available). License: MIT-like . | Demo |
|
|
C# Corner: Creating a Windows Service in C# by Mahesh Chand January 23, 2001 | . | (not yet evaluated) |
|
|
The Code Project: Simple Windows Service Sample by Mahmoud Nasr on 2003-July-15 | Simple primer into the topic. License: no explicit license attached, see inside the code. See also MSDN Walkthrough: Creating a Windows Service . | (not yet evaluated) |
|
|
The Code Project: Windows Services in Action I and Windows Services in Action II by Umut Şimşek on 2008-May-21 and 2007-December-28 | Elaborated example, resembling a real-world application, reading a config file, reading a database, sending emails, etc. License: The Code Project Open License (CPOL) | (not yet evaluated) |
|
|
The Code Project: Creating a Basic Windows Service in C# by John Storer II on Jun 2006-June-05 | . License: no explicit license attached (see code). | (not yet evaluated) |
|
|
The Code Project: A Windows Service Application by Shanmuga Sundar.V on 2006-November-29 | . License: CPOL . | (not yet evaluated) |
Imprint : www.trilo.de/imprint.html