Skip to main content

Custom Windows OS Service Experience

[PLACEHOLDER]



Introduction:

In this article we present some lessons learned and tips experienced through a Windows service development. This article is not going to show you how to create the Windows service project, rather it will talk about converting a console app into a Windows service for debugging purposes. It will also present some tips and references links that were checked by the team while they were going through the development.

For this and any project some basic items:
  • coding: always try to have in your solution a Unit testing project, at least to cover some of the main components. In normal development shops this is probably the norm, but in ad agencies, sometimes because budget and project size they skip this important step. With it you can verify the main functions and what may take time at the beginning to setup, it actually payed-off throughout the development and implementation of the service. 

  • coding-structure: Code that is well organize, different layers and libraries with specific functionality, definition of contracts that make sense. 

  • coding - best practices and used of patterns: Following the item,  mentioned above, you would have an easier code to maintain if you implement the right rules and bend the ones that can. As an example for the Windows service that I was creating I had the need of writing to the eventlog (system.diagnostic) and also using the log4net library for writing additional logs. At the starting point there was repeating code in different functions.


A refactor was in place for reducing the duplicated code. Using the .net delegate and event made possible to do some decoupling as well. Just to mention an scenario in our solution, when a notification got triggered it called the event for tracking the action into the logs (let’s say for debugging and troubleshooting purposes in case something went wrong). If we think about it this could be an example about implementing the Observer pattern(* You can read more by following one of the links in the references section at the end of the article).

Here is
a first version of a piece of the code, showing an example of using delegates and events (Since then the code has changed but at least it shows the idea):

namespace ClassNotification {
public delegate void NotificationEventHandler(object sender, NotiEventArgs e);
public class NotificationCaller
{
#region "variables"

private log4net.ILog Logger;
private System.Diagnostics.EventLog eventLog1;
#endregion
#region "Event section"
public event NotificationEventHandler Noti;

protected virtual void OnNoti(NotiEventArgs e)
{
    if (Noti != null)
     Noti(this, e);
}
#endregion

#region "notification delivery - mail"
public NotificationCaller(log4net.ILog logger, System.Diagnostics.EventLog eventlog)
{
   this.Logger = logger;
   this.eventLog1 = eventlog;

}
private bool SendNotification(string message)
{
   string TheSender = Configu.FromAddress;
   string TheRecipient = Configu.ToAddress;
   bool answer = ClassNotification.Notification.EmailSend(SOMETHING SOMETHING", message, TheSender, TheRecipient);

   NotiEventArgs notieventArgs = new NotiEventArgs(answer, Logger, eventLog1);
   OnNoti(notieventArgs);

   return answer;
}

  public bool SendNotificationExecution(string response, string StatusOfExecution)
  {
   EventListener listener = new EventListener(this);

   bool answer = false;
   answer = SendNotification("SOMETHING " + StatusOfExecution + ". Date: " + DateTime.Now.ToLongDateString());
   return answer;
  }

#endregion
}




/// the event listener class
///

class EventListener
{
private NotificationCaller notificationCaller;

public EventListener(NotificationCaller notiCaller)
{
   notificationCaller = notiCaller;
   notificationCaller.Noti += new NotificationEventHandler(NotiCalled);
}

private void NotiCalled(object sender, NotiEventArgs e)
{
   if (e.Answer)
   {
     e.Logger.Info("Info: Notification delivered!");
     e.eventLog1.WriteEntry("PROJECT- Notification delivered!" + DateTime.Now.ToShortDateString(), EventLogEntryType.Information);
   }
else
{
   e.Logger.Info("Error with transaction: Notification delivery failed. Check mail server");
   e.eventLog1.WriteEntry("PROJECT- Notification delivery failed. Check mail server" + DateTime.Now.ToShortDateString(), EventLogEntryType.Error);

  }

 }

}



/// the custom eventArgs class for the notification event
///

public class NotiEventArgs : EventArgs
{
   public bool Answer;
   public log4net.ILog Logger;
   public System.Diagnostics.EventLog eventLog1;
   public NotiEventArgs(bool answer, log4net.ILog logger, System.Diagnostics.EventLog eventlog1)
{
   this.Answer = answer;
   this.Logger = logger;
   this.eventLog1 = eventlog1;
  }
 }
}

For a quick implementation you could do something like this:
Notification.NotificationCaller oNoticaller = new Notification.NotificationCaller(logger, eventLog1); bool answer=false; answer = oNoticaller.SendNotificationExecution(response, StatusOfExecution);

  • Debugging the Windows service: You can start by building a console app, for simplicity and quick debugging. After the basic functionality is done the next step is to convert it into a Windows service. If you search for it on the web you will noticed that others have gone through the same.  

    By using this approach you can switch and run as a console app or as a windows service whenever you need to:
    static void Main(string[] args)
    {

        ServiceBase[] programService;
           programService = new ServiceBase[]
           {
              new Program1()
           };
              if (Environment.UserInteractive)
              {
                 //for debugging purposes
                 RunInteractive(programService);
              }
              else
                  {
                     ServiceBase.Run(programService);
                  }
              }
    Obviously for a service you have add the required methods like OnStart(), OnStop(). Note: More about this in my reference links. 

  • Debugging a Windows service: The previous item gives a useful option (having it as a console app).


Another option would be installing the service and then, using Visual Studio, attaching the process from the debug menu and debug away!

  • Installing a Windows service: installUtil would be a manual installation. Which usually you’ll find it in [DRIVE]:\Windows\Microsoft.NET\Framework64\v4.0.30319. An example would be:

installutill [drive]:\[location]\service.exe

keep in mind that you need to add an installer to your project. If you want the details I would advice to go to this link http://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx and follow the setps in the adding Installers to the Service.
another installation approach, aside using the installUtil, is by using InstallShield Limited Edition Project; which would be an automatic way of installing it. This is as close as an MSI it can get… I believe, at least until the date of this article. I haven’t done it, so I cannot guarantee the success on this one. But I left you a link under the reference section for you to explore. 
Final Words:
This solution was right for us at the moment. Based on requirements check your options and evaluate if a Windows service, API, SSIS, or whatever that you have in mind is the correct decision for the case considering the technology, maintenance, scalability and budget.

But whatever you do always follow best practices and value the lessons learned, as that experience can be applied for future projects, helping you improve on your delivery.

Reference:

TDD (Test-driven development):


Observer pattern - delegates & events:

.Net application console as a Windows Service:

about thread-safe tips:


Debug a Windows service app:

How to create a setup project for Window service


Install and uninstall services using InstallUtil:

Add installers to your service:
http://msdn.microsoft.com/en-us/library/ddhy0byf(v=vs.110).aspx


Trending posts

AGILE For DIGITAL AGENCIES

Introduction Some Digital agencies have a project process where waterfalls still plays a big part of it, and as far as I can tell, the tech team is usually the one suffering as they are at the last part of the chain left with limited budget and time for execution. I do believe that adopting an Agile approach could make a Digital Agency better and faster. In this article I’m presenting you just another point of view of why it make sense looking at Agile Methodology.  Why Agile for a Digital Agency? The Agile movement started in the software development industry, but it has being proven to be useful in others as well. It becomes handy for the type of business that has changing priorities, changing requirements and flexible deliverables. In the Digital Agency of today you need a different mindset. Creative will always play a huge role (“the bread and butter”). But the “big guys” need to understand that without technology there is no Digital Agency. Technical resources are

Key insights from "Atomic Habits" by James clear

I recently finished reading "Atomic Habits" by James Clear. The book was incredibly insightful. If you are looking to improve your habits, and achieve results while you are at it, then this book is for you. It may help you form new habits, and break bad one. Without further due, here are my top three takeaways. Photo by Nataliya Vaitkevich via Pexel, adapted by Beolle Takeaway 1:  The habit-forming loop: James outlines that the habit-forming loop consists of four stages Cue . The cue triggers the brain to expect a reward and is crucial for building automatic habits. It is typically associated with time, place, or feeling. For example, feeling bored could be a cue to the habit of using social media. Craving . This is the urge resulting from the cue. Using the above example, opening the social media app is the craving initiated by the cue of boredom. Response . An example of a response is the action of opening the social media app and using it. Reward . An example of reward i

AI with great power comes responsibility

Generative AI continues to be front and centre of all topics. Companies continue to make an effort for making sense of the technology, investing in their teams, as well as vendors/providers in order to “crack” those use cases that will give them the advantage in this competitive market, and while we are still in this phase of the “AI revolution” where things are still getting sorted.   Photo by Google DeepMind on Unsplash I bet that Uncle Ben’s advise could go beyond Peter Parker, as many of us can make use of that wisdom due to the many things that are currently happening. AI would not be the exception when using this iconic phrase from one of the best comics out there. Uncle Ben and Peter Parker - Spiderman A short list of products out there in the space of generated AI: Text to image Dall.E-2 Fotor Midjourney NightCafe Adobe Firefly

Apple's App Tracking Transparency sealing Meta's fate

If you have been following the recent news on Meta (formerly Facebook) you may have read that Meta recently projected their ad revenue will be cut by a staggering $10 billion in 2022 due to Apple’s new App Tracking Transparency feature (also known as ATT). This has resulted in Meta’s stock to plummet by over 20%. Photo by julien Tromeur on Unsplash - modified by Beolle So what is Apple’s ATT and how does it impact ad revenue? Apple has been releasing multiple privacy features for the last few years. This included Apple’s Mail Privacy Protection and Apple’s App Tracking Transparency feature. You can learn more about Apple’s Mail Privacy Protection in our earlier post by clicking here .  Apple’s App Tracking Transparency (ATT) was launched in iOS 14.5 and iPadOS 14.5 where it prompted users to select if they wanted the app to track their activities across other apps on the device. The prompt is displayed when the user opens an app like Facebook or Instagram for the first time on iO

This blog uses cookies to improve your browsing experience. Simple analytics might be in place for pageviews purposes. They are harmless and never personally identify you.

Agreed