Wednesday, February 6, 2019

Difference between a Windows Service Solution and a Console Application By Example

Windows Services are the engine that most people never see, these are entirely different beasts. Common differences between Windows Services & Console/Windows/Web applications shown in this image. Learn Windows Service Programming with C# in my Udemy course, here's 50% off discount.

Learn Windows Service Programming on Udemy Message Box

There are some major differences between a Windows Service executable and simple exe files. This difference is also present in Visual Studio Solution. 
Learn Windows Service Programming in C# .Net by enrolling in my Udemy course now, here's a discount coupon code. 

Let’s open a console application project in Visual Studio.
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, when I press F5 the exe does not start to run.

Let me explain a couple short things first.

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

Learn Windows Service Programming on Udemy Message Box

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
Learn Windows Service Programming on Udemy Message Box
Boom! a message box says you can’t run a Windows Service from debugger baby.

This is the first difference

You can't just hit Ctrl+F5 to run a Windows Service Solution. 

Now let’s head back to the console application project
Inside the main method, I will place a breakpoint on a Console.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

Learn Windows Service Programming on Udemy Message Box

This is difference number two

Finally, let us go to the output directory of our console application(any 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 a Windows Service before we can run it

You will learn how to install a Windows Service in next section of my course. Please sign up here. 

MPORTANT: The source code for section 1 can be downloaded from my Windows Service Course .

No comments:

Post a Comment

Feel free to talk back...