Wednesday, October 30, 2013

Debugging a Windows Services

We cannot run Windows Services, directly like running a Windows or Web application; we need to install the Windows Service in the service console for it to operate. Hence we cannot debug windows services directly, use the following procedure to debug a windows service. 

Using ServiceController to Manage a Windows Service

We can use the ServiceController component to Start/Stop a service from an application without having to open the services panel. This can be used in Admin / Dashboard applications which needs to monitor and manage a set of services in a server.

The ServiceController is part of the “ System.ServiceProcess” assembly and has methods to start/stop or perform other operations on a Windows Service. In this post we shall see on how to use the ServiceController to manage the state of our Windows Service “NotificationServices”

Windows Services with custom User account

A Windows service runs under a specific account, the Account property needs to be set in the ServiceProcessInstaller. The property can take any of the below 4 values.

LocalService
NetworkService
LocalSystem
User

Creating a Deployment Project to Install a Windows Service

In the post Installing and Uninstalling a Windows Service, we have seen on how to Install and Un-Install a windows service using the InstallUtil tool, however when you want your service to be packaged for deployment into end user computer this approach might not work as the end user will not be aware of how to run the commands to install the service, in these cases we can create a deployment project which will automatically deploy the service to the computer.
 
In this post we shall see on how to create a Setup Project which will create a setup file to deploy the service. 
1.    Open the Windows Service Project Solution in VS.Net
2.    Add a new project of type “Setup Project” to the solution.
3.    I will name the setup project as NotificationServiceSetup. 
4.    You can set the Properties like Company, Author etc in the Set up project Properties.
5.    Once the Setup Project is created, right click on the project and select Add -> Project Output 
6.    Select the NotificationService project in the Projects dropdown and select, “Primary output” in the list box below. Click OK 
7.    Once again right click on the Setup Project and select View -> Custom Actions
8.    In the Custom Action panel, right click on Custom Actions and select “Add Custom Action” 
9.    Select “Application Folder” click OK 
10. Select “primary output from NotificationService (Active)”, click OK
11. Notice that the “Primary output from NotificationService” is added under Install , Commit, Rollback and Uninstall options.
12. Right click on the NotificationServiceSetup project and build the project
13. Once the build is completed, the setup file to install the service is created in the /Bin folder of the project. 
14. That’s it the setup project is ready, just double click on the .msi file and follow the instructions of the setup Wizard to install the Project.
15. Once the installation completes the NotificationService will get added to the list of services on the computer.

Installing and Uninstalling a Windows Service

Windows services can be installed and un-installed from the list of services, using the Utility installutil.exe, this utility comes with the framework and can be used to configure services. 
Follow the below instructions to use the installutil.exe utility to Install / UnInstall a windows service.

Adding Service Installers to the Windows Service

We have created the windows service and implemented the Start and Stop methods of the service, but for the service to start working it needs to be installed to the list of Window services in the server, to do that we need to create a Service Installer which can install / uninstall the service.

In this post we shall create an installer for a Windows Service.

Logging errors to Windows Event Log

A windows service, gets added to the list of services and gets executed automatically, hence we need a good set of logs to identify issues with the service. We shall see on how to write appropriate logs into the Windows event log for each action performed in the Windows service.

We shall add a new utility class LogHelper which will log the events to the Windows Event Log and call the methods in this helper class to log the status of the windows service.

Add a new class called LogHelper.cs and add the following logging methods to the class.

Setting Timer for a Windows Service

A windows service is supposed to perform a set of operation repeated after a pre-defined interval of time. For the service to trigger the operation after a pre-defined interval we need to define a timer and call the implementation method when the timer elapses.

We will create a times and define the interval when the service gets started, there after whenever a time elapses the timers Elapsed event gets triggered, we can bind our implementation method to this event so that the method gets called every time the time elapses.

We will use the same windows service which we created in the post Creating a Basic Windows Service
 and add a timer to the service.

Creating a Basic Windows Service

Microsoft Windows Services, enable us 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.


Visual Studio.Net provides us pre-defined templates which can be used to create Windows Services, in the following example we shall see on how to create a basic windows service.

Windows Service Events

A Windows Services class should inherit from the System.ServiceProcess.ServiceBase class. We should override methods of this class and define functionality for them to determine how your service behaves.

The following are the main methods which should be overwritten to implement the functionality of a Windows service.

What is 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. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

The compiled executable file that a service application project creates must be installed on the server before the project can function in a meaningful way

Windows Service applications are based on a class that inherits from

Sunday, October 20, 2013

Distributed Caching

Assembly Overview

PerfView Overview

Performance Monitor Overview

CLR Profiler Overview

Garbage Collection Overview

.Net Memory Management Overview

Object Oriented Concepts in C#

Interface Overview

Class Modifiers in C#

Access Modifiers

Properties Overview

OOPs Overview