Linked by Will Senn on Mon 5th Jan 2004 19:35 UTC
.NET (dotGNU too) OK, so you want to create a windows service and you're not a .NET guru? This is exactly what got me in trouble with my wife. It started off easy enough, but before the weekend was through, my wife was getting on to me for spending so much time at the computer. She thought that I should be spending quality time with our family, imagine that. I told her that I was doing some 'personal' research, that, "no, it's not work honey" and "I'm trying to learn some new technology", "think of it as reading a book, only on the computer..." Inanities like that, she wasn't having any of it, of course. Regardless, I am glad to report, I figured it out and just in the nick of time, too.
E-mail Print r 0   · Read More · 31 Comment(s)
Order by: Score:
v Interesting article
by Anonymous on Mon 5th Jan 2004 19:49 UTC
v Re : Interesting article
by nego` on Mon 5th Jan 2004 19:51 UTC
v ...
by nego` on Mon 5th Jan 2004 19:53 UTC
v Re : Interesting article
by Eugenia on Mon 5th Jan 2004 19:53 UTC
A question about .NET on Windows.
by Eugenia on Mon 5th Jan 2004 19:56 UTC

I have VS.NET 2003 but I don't seem to find the motivation to install it as it takes a long time (I need to install 2002 first and then upgrade it).

I know that MS is offering a .NET SDK and C# compiler and tools free of charge, but I am not sure if it includes a debugger and if I can use these with SharpDevelop free IDE.

Great Article / Resource
by nego` on Mon 5th Jan 2004 20:02 UTC

I know how much resource it can take to figure out a (what seems as) simple task when it's not documented too well. Thank you for your hardwork, I know i will find it useful !

(Sorry Eugenia for earlier)

RE: A question about .NET on Windows.
by Anonymous on Mon 5th Jan 2004 20:08 UTC

No, the SDK does not include a free debugger as far as I know.

Free Debugger
by Anonymous on Mon 5th Jan 2004 20:15 UTC
Thanks
by KRC on Mon 5th Jan 2004 20:27 UTC

This is a good guide to help folks get off the ground when developing win32 .NET console-based services.

I find it frightening how heavily dependent this process is on the visual studio .NET GUI - all just to make a console service?

For Windows users curious how the same could be accomplished on Linux or BSD, one can use the 'cron' daemon to run a command every minute, hour, day, month, or day-of-the-week.

For example, the following command will achieve the same result:

echo "[`date '+%D %r'`]: I am ALIVE!" >> ~/time_service.log

It appends this line:

[01/05/04 12:10:11 PM]: I am ALIVE!

to the 'time_service.log' file in your home directory.


To make the 'cron' daemon run this command every minute, type 'crontab -e' and add the following:

* * * * * echo "[`date '+%D %r'`]: I am ALIVE!" >> ~/time_service.log

The 5 stars represent minute, hour, day, month, and day of the week. In this case, we want to run our command every minute, all the time.

Or, if your service is not interval based and instead is action/reaction based, you can run your executable/script in the background, like this:

> command &

The '&' sends 'command' into the background.

If you're an administrator, you can add your executable to a start up script in /etc, which is similarly easy - just add a single line to a text file.

RE: Thanks
by Anonymous on Mon 5th Jan 2004 20:39 UTC

Does this service you made run in kernel space?

FYI
by Gary on Mon 5th Jan 2004 20:52 UTC

There are a number of other sample on creating .Net based services on www.codeproject.com.

To Anonymous, the service runs with the permissions level of the user under whose account the service is running (set during the install, or later via the component manager). Think of it running as a background user.

To the author of the article, can you explain why the timer1 is being contained by the installer design, rather than the service class?

Eugenia (IP: ---.osnews.com)
by Anonymous on Mon 5th Jan 2004 21:08 UTC

If you have both 2002 and 2003 why deal with SharpDevelop?? I mean its a very nice FREE IDE but doesn't compare to vs.net.

I know it doesn't compare. But I don't want to spend 2 hours installing VS.NET and spend 6 GB of space.

In the meantime since my last message I have already installed the 1.1 SDK and #develop. It took about 500 MB and 20 minutes overall and it will be good enough for a beginning.

v Exactly why ...
by Anonymous on Mon 5th Jan 2004 21:16 UTC
Upgrading VS.Net 2003
by DCMonkey on Mon 5th Jan 2004 21:27 UTC

You should be able to install the VS.net 2003 upgrade first and feed it the 2002 disk to prove you have the previous version when the install routine asks for it.

Debugging those services
by Jeremy on Mon 5th Jan 2004 21:32 UTC

Took me a while to find when I was developing my first Windows Service...

When you are ready to start debugging the .NET Windows Service it's nice to override the OnStart() method and add a sleep (10-15 seconds) there to give you time to attach the debugger before the service executes your code.

Also, when you start the service go back into Visual Studio (I use 2002) and select Debug->Processes. In the window find your service (by executable name) and click the Attach button. Click Close and you will be debugging your service. You can set breakpoints now that will be hit etc. When you are done debugging go back into the Processes window detach from the process.

Hope that helps someone... :-)

Re: RE: Thanks
by Matthew Bishop on Mon 5th Jan 2004 22:04 UTC

Does this service you made run in kernel space?

No, a cron job does not run in kernel space. In fact, almost nothing runs in kernel space on a typical unix machine. While there can be some (minor) performance gain from running code in kernel space, it is considered bad to put code into the kernel if it doesn't have to be there. Why? Because any bug in kernel code can easily lead to a crashed system or a security vulnerability. User space is good because it lets the OS do its job--protecting processes from one another.

RE: Thanks
by Jeremy on Mon 5th Jan 2004 22:13 UTC

Just a few comments on the cron ideas (I'll just comment on them inline):

I find it frightening how heavily dependent this process is on the visual studio .NET GUI - all just to make a console service?

Well, you can write a .NET service as easy as writing the code in a .cs file and compiling it using csc.exe with the correct command-line parameters. No GUI needed.

For Windows users curious how the same could be accomplished on Linux or BSD, one can use the 'cron' daemon to run a command every minute, hour, day, month, or day-of-the-week.[i]

Windows provides a command 'at' which is similar to cron (granted not as powerful) which you <u>can</u> use to schedule tasks to run at various times using it. Just run 'at /?' to see.

[i]Or, if your service is not interval based and instead is action/reaction based, you can run your executable/script in the background, like this:

> command &

The '&' sends 'command' into the background.

If you're an administrator, you can add your executable to a start up script in /etc, which is similarly easy - just add a single line to a text file.


If you run a command using '> command &' does it continue to run if you log out? A Windows Service can be set to run as soon as Windows boots even before any users logs in. Also, with a Windows Service you can configure it to run as any user you'd like.

I think you could loosely compare a Windows Service to a daemon in Unix.

re: Thanks by KRC
by Anonymous on Mon 5th Jan 2004 23:20 UTC

Hi,

The reason that it is so GUI oriented is because, I wanted to show how 'easily' it could be done using the GUI - there are a lot of code based tutorials around. Believe it or not, you would think that this type of information would be easily found, but it really isn't - least not that I could find.

The do something part is simple on purpose, as well - rocket science is left to the user - I provided a shell not the meat.

Your example using echo could be similarly managed in windows with the AT command and a batch file.

Again, it's just a tutorial - it is educational not robust or useful.

Thanks for the comments.

Will

Cool ... cool ....
by WorknMan on Tue 6th Jan 2004 02:13 UTC

Worked as advertised, cept my executable ended up in <pathname>binDebug .... is that normal?

Good tutorial, BTW ;)

Dammit ...
by WorknMan on Tue 6th Jan 2004 02:14 UTC

I meant ... my executable ended up in <pathname>binDebug (after I clicked on Build Solution, there is no executable in the root directory of my project directory).

re:is that where it was supposed to go?
by Will Senn on Tue 6th Jan 2004 02:43 UTC

WorknMan:

It should have been <ProjectDir>/bin/Debug if you built it in debug mode - the default and didn't specify a different directory. To specify the directory simply select Project-Properties from the main menu and select Configuration Properties in the dialog that pops up. Under Outputs you can change the Output Path (bin/Debug) to whatever you like - under the project directory.

In order to actually install it in production (or just to test from another location) simply copy it where you want it to reside and run installutil nameofexe from that location. installutil comes with the .NET framework.

Thanks,

Will

re:Debugging those services
by Will Senn on Tue 6th Jan 2004 02:52 UTC

Jeremy,

Stellar hint! I didn't think of doing it that way, I just attached and then stopped the process - I know, pretty random, yours is a much better way and allows debugging of the startup code.

Thanks,

Will

You are awesome !!!
by Anonymous on Tue 6th Jan 2004 05:01 UTC

This was a GREAT article and can only wish more were documentated and easy to read as this (the screen shots were excellent)!!! It was not only useful but informative.

Thanks much

:)

How to budle this to install of another computer??
by Anonymous on Tue 6th Jan 2004 05:08 UTC

This article assumes you are installing this on a the local machine - how do I deploy the created service on a third-party computer that does not have VS.NET on it???

Thanks

How to budle this to install of another computer??
by Anonymous on Tue 6th Jan 2004 05:20 UTC

This article assumes you are installing this on a the local machine - how do I deploy the created service on a third-party computer that does not have VS.NET on it???

Thanks

RE: A question about .NET on Windows.
by Nick on Tue 6th Jan 2004 12:45 UTC

SharpDevelop has a good debugger that comes from the .Net SDK. SharpDevelop can be used on the .Net SDK because that is what it was designed for. SharpDevelop didn't create their own compiler just an IDE.

RE: How to budle this to install of another computer??
by Jeremy on Tue 6th Jan 2004 12:56 UTC

This article assumes you are installing this on a the local machine - how do I deploy the created service on a third-party computer that does not have VS.NET on it???

At the least you'll need the .NET Framework installed on any computer that you plan to run .NET applications on. InstallUtil.exe comes with all versions of the .NET Framework, AFAIK, so you can always install a .NET Windows Service.

RE: RE: How to budle this to install of another computer??
by Anonymous on Tue 6th Jan 2004 13:57 UTC

>@Jeremy - At the least you'll need the .NET Framework installed on any computer that you plan to run .NET applications on. InstallUtil.exe comes with all versions of the .NET Framework, AFAIK, so you can always install a .NET Windows Service.

But how do you se InstallUtil.exe ???

RE: RE: How to budle this to install of another computer??
by Will Senn on Tue 6th Jan 2004 14:09 UTC

Anon:

type pathtoinstallutilinstallutil nameofexe
from the directory with nameofexe in it, where pathtoinstallutil is something like:
D:WINNTMicrosoft.NETFrameworkv1.1.4322

and nameofexe is something like:
timedservice.exe

as Jeremy pointed out, you get installutil with the framework.

If you are looking for how to create a professional installer - I hate to be coy, but - that's beyond the scope of my little 15 page tutorial.

Will

Re: Eugenia (IP: ---.osnews.com)
by Anonymous on Tue 6th Jan 2004 15:25 UTC

Yeah.....2 hours of doing something else.....once you start the install.....lwhy don't you go and do something else.....like see the outdoors or something for a few hours......once the install starts its not like you have to sit there.....also the 6GB....most of that is the MSDN and disk space is so cheap.....get over it.