Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Windows Service Programming, Debugging, Installing in C#.Net
Rating: 4.2 out of 5(452 ratings)
17,889 students

Windows Service Programming, Debugging, Installing in C#.Net

Windows Service Tutorial: How to Create Windows Services in C# .Net/Visual Studio, Windows Service debugging techniques
Created byNaeem Akram
Last updated 3/2020
English

What you'll learn

  • Create new Windows Services in C# .Net which run in background
  • Debug & Maintain Existing Windows Services in C#
  • Use Apache Log4Net logging library in a C# Project
  • Use InnoSetup to create setup installer for installing Windows Services on non production machines

Course content

6 sections29 lectures1h 41m total length
  • Introduction to Windows Service Programming in C#2:58

    Hello and welcome to my course... creating Windows Services using C# .Net.

    In this course... you will learn a very useful skill: creating and debugging Windows Service applications.

    I am Naeem Akram, I have done Windows Service programming as a full-time job for several years.

    My course is divided into various sections.

    In section one, you will learn the definition and anatomy of a Windows Service. You will also understand the difference between a Windows Service and other application types like Windows Forms.

    In section two, you will create a Windows Service project and utilize installutil.exe to install or uninstall a service.

    After this, you will open the Event Viewer to see the events... generated by your newly born service.

    In section three, you will learn how to debug a Windows Service.

    You will also see how to integrate Log4Net logging library into your service project. It's a free & open source library, lets you generate rolling log files.

    The next section will look into real-world scenarios like file synchronization and periodic data update services. You will also learn how to create an installer project for easy setup and installation of a Windows Service.

    In section 5, we will create an admin application for a Windows Service. This application will be able to start/stop a service and change the configuration file of the service.

    Section 6 was added by popular demand. This section will show you how to create a setup installer for your Windows Service. I am going to use Inno Setup for this purpose.

    Please note that this course will not freeze in time. I will keep adding interesting and useful ideas with time.

    I learned Windows Service programming the hard way, on my own. You can take advantage of my experience and learn it quickly by joining this course.

  • What are Windows Services1:49

    So now I am going to explain some very important concepts about Windows services.

    First, open the services control manager SCM.(+pan into Run and show hint on screen but don’t go on with narration).

    First of all, the columns name and description are self explanatory.

    The column status shows the current status of the service, which might be any of the those shown on the screen.

    Stopped

    Started

    Paused

    Pending Start

    Pending Stop

    Pending Pause

    The column Startup Type tells whether a service is started automatically by Windows or it is stopped, it is possible to disable a service as well and in that case this column will contain the word “Disabled”. In short the four possible values of a service start up type are shown on screen. That is

    Manual

    Automatic

    Disabled

    Automatic(Delayed Start)

    The last column “Log On As” shows the username of the Windows account a particular service is going to use. Possible values of this field are:

    Local System

    Network Service

    Local Service

    Specific User

    More information can be found on the link mentioned in the description of this lecture. https://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount(v=vs.110).aspx

    Having a specific set of credentials to logon makes it possible for Windows Services to start before a human user logs-in and stay operational even when a human user is gone.

    Among these types of login credentials, LocalService is the one with the fewest powers. It’s a non-privileged user on the local machine.

    LocalSystem on the other hand is the most powerful type of user and it can do almost anything you want the the machine.

    Third type NetworkService is slightly more powerful than LocalService, and it can interact with various network services. Details are not relevant from the perspective of this course so I would prefer not to stress too much on the definition.

    We can perform various service operations by clicking on a service entry in SCM and going to the Action menu.

    There are some shortcuts on the top left as well.

    It is also possible to right click a service and select ‘Properties’ from the dropdown.

    The properties dialog not only allows you to start/stop/pause/resume a service, it also shows you the file path from where a specific service is running.

  • Windows service statuses, start-up types, and login accounts3:48

    In this video, I will show you some major differences between Windows Service executables and simple exe files.

    Let’s open a console application project

    First of all I will press ‘Control+F5’

    You see the console application starts working right away

    Now let’s go to a Windows Service project and try the same.

    Let me explain a couple ‘short things first

    OnStart method of a windows service is being shown on the screen

    The OnStart method of the service is called by Windows when a user clicks “Start” link on the Service Control Manager

    In our demo code, we are writing some text to the debugging console in this method

    Let me press ‘Control+F5’ keyboard shortcut once again

    Boom! a message box says you can’t run a Windows Service from debugger baby.

    This is the first difference

    Now let’s head back to the console application project

    Inside the main method, I will place a breakpoint on a console dot writeline which prints Hello World!

    And I will click ‘Debug’ menu and select the menu item ‘Start Debugging’

    You see, the breakpoint is hit instantly

    I’ll press F5 so that the application continues execution

    Let’s try to do the same with the Windows Service project

    I will place a on Debugger dot writeline

    Go to ‘Debug’ menu and click ‘Start Debugging’

    The same error shows up again

    This is difference number two

    Finally, let us go to the output directory of our console application

    Double click HelloWorld.exe file

    You see, the application starts working right away

    Let’s go to the output directory of the service project and try to run it by double clicking the .exe file in a similar fashion

    Oops! It won’t start dancing to your tunes

    This is the third difference

    We need to install the service before we can run it

    You will learn how to do it in the next section

    The differences shown in this video can be downloaded in pdf form through the downloads section of this course. I’ve written these in the description of the lecture as well.

  • Difference between console applications and Windows Services3:36

    IMPORTANT: The source code for section 1 can be downloaded here.

    In this video, I will show you some major differences between Windows Service executable and simple exe files.

    Let’s open a console application project

    First of all I will press ‘Control + F5’

    You will see the console application starts working right away

    Now let’s go to a Windows Service project and try the same.

    Let me explain a couple ‘short things first

    OnStart method of a windows service is being shown on the screen

    The OnStart method of the service is called by Windows when a user clicks “Start” link on the Service Control Manager

    In our demo code, we are writing some text to the debugging console in this method

    Let me press ‘Control+F5’ keyboard shortcut once again

    Boom! a message box says you can’t run a Windows Service from debugger baby.

    This is the first difference

    Now let’s head back to the console application project

    Inside the main method, I will place a breakpoint on a console dot writeline which prints Hello World!

    And I will click ‘Debug’ menu and select the menu item ‘Start Debugging’

    You see, the breakpoint is hit instantly

    I’ll press F5 so that the application continues execution

    Let’s try to do the same with the Windows Service project

    I will place a on Debugger dot writeline

    Go to ‘Debug’ menu and click ‘Start Debugging’

    The same error shows up again

    This is difference number two

    Finally, let us go to the output directory of our console application

    Double click HelloWorld.exe file

    You see, the application starts working right away

    Let’s go to the output directory of the service project and try to run it by double clicking the .exe file in a similar fashion

    Oops! It won’t start dancing to your tunes

    This is the third difference

    We need to install the service before we can run it

    You will learn how to do it in the next section

    The differences shown in this video can be downloaded in pdf form through the downloads section of this course. I’ve written these in the description of the lecture as well.

  • Basic concepts about Windows Services

Requirements

  • Basic C# .Net programming language

Description


This course teaches you how to create Windows services in Visual Studio in C# .Net. Multiple techniques to debug a Windows Service in Visual Studio. Windows Services are a vital component of the Microsoft Windows operating system.

The course is divided into various sections.

Introduction to Windows Service Programming in C#

You will learn the definition and anatomy of a Windows Service application. In the past, NT service was used for a Windows Service. You will also understand the difference between a Windows Service and other application types like console applications. This section is not dependent on any programming language like C#.Net or C++. Finally, you will learn what Windows Service Control Manager(SCM) is and how to run it from the run prompt. SCM shows a Windows Services List.

Windows Service Programming in C# .Net Primer

In section two, you will create a new Windows Service project in Visual Studio and utilize installation to install or uninstall a Windows Service. InstallUtil comes bundled with the .Net framework. Next, I'll show you what Windows Service Control Manager is. What are Windows Service statuses, login types, and service startup types? A Windows server service is the same as a Windows NT service.

After this, you will open the Windows Event Viewer to see the events generated by your newly born service.

Windows Service Debugging Techniques in Visual Studio and C# .Net

Section three will teach you how to debug a C# .Net-based Windows Service program in Visual Studio.

Techniques for setting breakpoints and getting inside the C# .Net code to add watches for debugging will be shown.

You will also see how to integrate the Apache Log4Net logging library into your C# .Net Windows Service project. Apache Log4Net is a free & open-source library that lets you generate rolling log files based on the Log4J library.

Practical Use of Windows Service

In the next section, you will look into a few real-world scenarios like a file synchronization Windows service with FileSystemWatcher and a periodic data update service(similar to an ETL) with System.Threading.Timer. File sync-type services were once top-rated among password synchronization applications for updating domain passwords into online portals based on password filter-related events.

Advanced Windows Service Programming

The following section concerns changing values in Windows Service configuration(app. config) and using the .Net framework ServiceController class. This section will create an admin application for a Windows Service. This application will be able to start/stop service and get service status as well. We will use C# .Net and WinForms for this purpose. The concepts shown herein will apply to WPF applications as well.

Installing a Windows Service with Inno Setup

The last section was added on student demand. It is about creating an installer to install Windows Service on a client machine. We will use the free software InnoSetup for this purpose. Then, A Windows service is a program that runs in the background of a Windows operating system and performs specific tasks. It is designed to start automatically when the computer boots up and can operate without any user interaction. Windows services are typically used to provide functionality to other programs or to perform system-level tasks. They can be managed through the Windows Services console, which allows users to start, stop, pause, and resume services as needed. Overall, Windows services are an essential part of the Windows operating system and help to ensure that it runs smoothly and efficiently. I'll show you how to use SC.exe to perform Windows Service control-related operations from the command line. It is mandatory to know how to create a Windows service command line using the SC command. I will also show you how to install a Windows service without installation.

Creating & maintaining Windows Services in C# .Net is easier than C++-based services. Windows Services are run not only on Windows Servers but on desktop machines. Their usefulness is universal.

Windows Services is an advanced C# and OS concept. I learned Windows Service programming the hard way, on my own. You can take advantage of my experience and learn it quickly by joining this course. So sign up, and I will see you inside!


It is possible to create a Windows Service in VB .Net too but it is not recommended.

Who this course is for:

  • Fresh or experienced programmers with little to no experience in C# .Net Windows Service programming
  • Programming students who want to learn how background services work