Today, I wrote a Windows service application. Because part of my day job is teaching C# and .NET, I was inspired to write a "how to" document. The Walkthrough: Creating a Windows Service Application in the Component Designer article found on MSDN is quite helpful, if you know your way around C# and .NET. Anyone who doesn't understand C# and .NET enough to make short work of the document arguably has no business writing a service; but, we all have to learn sometime.
UPDATED 29 MAY 2012 - Revised several areas for clarity and simplification, and updated formatting.
Showing posts with label Deployment. Show all posts
Showing posts with label Deployment. Show all posts
16 December 2010
17 June 2010
Installing an Event Source
at
01:16


Writing events to the event log is easy, but you must register an event source with the system. Because these are simply registry keys, the application must be executing under administrative privileges, to create the keys. This is a problem frequently encountered by client-server, service, and middleware .NET programmers.
The one time you are guaranteed to have administrative control over the registry is during installation of the application. Adding a custom action to a Visual Studio Setup project will create your EventSource, during installation, without encountering permissions issues.
The Installui.exe utility that we run from the Visual Studio Command Line, especially when testing services, executes during the setup process. We can tell it to look for RunInstaler attribute decorated classes, whose input parameter value is true, and call the method. (Cool!)
Configuration Steps:
The one time you are guaranteed to have administrative control over the registry is during installation of the application. Adding a custom action to a Visual Studio Setup project will create your EventSource, during installation, without encountering permissions issues.
The Installui.exe utility that we run from the Visual Studio Command Line, especially when testing services, executes during the setup process. We can tell it to look for RunInstaler attribute decorated classes, whose input parameter value is true, and call the method. (Cool!)
Configuration Steps:
- Add a class to your application named InstallActions.cs
- The file content should resemble this:
using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; [RunInstaller(true)] public class InstallEventLog : Installer { public const string EventSource = "MyEventSource"; public InstallEventLog() { var eventLogInstaller = new EventLogInstaller(); eventLogInstaller.Source = EventSource; Installers.Add(eventLogInstaller); } }
Notice the method is decorated with the RunInstaller attribute. This tells the compiler the method is to be accessible to the setup project. Of course, this class may be included in any project in the solution. - Add a Visual Studio Setup project to your solution, if one doesn't already exist.
- Right-click the Application Folder node, in the left window pane
(The context menu appears) - In the context menu, select Add > Project Output...
(The Add Project Output Group dialog appears)
- Select the relevant output required for installation (typically Primary Output and Active configuration)
- Click the OK button
- Right-click the setup project node in the Solution Explorer
(The context menu appears) - In the context menu, select View > Custom Actions
- Right-click the Install node
(The context menu appears) - In the context menu, select Add Custom Action...
(The Select Item in Project dialog appears)
- In the dialog window, double-click the Application Folder node, or select it from the drop-down list
- In the dialog window, double-click select the Primary output from PROJECT_NAME (Active) node
(The dialog closes, and a new child node appears under the Install node, entitled, Primary output from WindowsService1 (Active))
You are done! The setup project is now configured to execute all classes inheriting Installer, and have the RunInstaller(true) attribute.
I generally keep the EventSource name stored in an application setting (app.config) file. Retrieving this value for use in the source code provided above takes a little more effort; because, the InstallEventLog class is not in a namespace.
I generally keep the EventSource name stored in an application setting (app.config) file. Retrieving this value for use in the source code provided above takes a little more effort; because, the InstallEventLog class is not in a namespace.
Subscribe to:
Posts (Atom)