Services..

C

Chizl

I created a console app, but most all the code was in classes. I've
recently created a windows service, pulled all the classes over and
everything compiles fine.

Few things I need to understand though. I'm a C++ guy so this service is
much different to me.

1) Where do I spawn the main thread off from?
(a) My first guess is in the Program.cs in the Main() before the
ServiceBase.Run(). However, that doesn't seem right because if I'm only
registering it, then I don't want my service to start processing only
register.
(b) My second guess would be after the ServiceBase.Run(), However
that should stop within Run and never get my thread started.

2) How do I register this service under Windows Services?
(a) In C++ it was just "ExeName.exe /Service". After digging
around I found some Mono code that represents what C# does and it was using
/Install. However it doesn't seem to work, instead I get errors saying I
need to run InstallUtil.exe. I looked at InstallUtil.exe and ran it
against my exe it seems to run successfully, but out side of created 2 more
files, it didn't seem to do anything.
(b) Once I get it install I'll need to know how to remove it from
services, like /UnRegservice does in C++.

3) How to add an icon to my service?
(a) I created a resource file with an icon, but it seems to be
ignoring the icon.
 
M

Mr. Arnold

Chizl said:
I created a console app, but most all the code was in classes. I've
recently created a windows service, pulled all the classes over and
everything compiles fine.

Few things I need to understand though. I'm a C++ guy so this service is
much different to me.

1) Where do I spawn the main thread off from?
(a) My first guess is in the Program.cs in the Main() before the
ServiceBase.Run(). However, that doesn't seem right because if I'm only
registering it, then I don't want my service to start processing only
register.
(b) My second guess would be after the ServiceBase.Run(), However
that should stop within Run and never get my thread started.

The main thread is the thread the application started up on. You spawn child
threads off of the main thread during the OnStart() event, using a delgate
method that a Thread.Start is using, as an example.
2) How do I register this service under Windows Services?
(a) In C++ it was just "ExeName.exe /Service". After digging
around I found some Mono code that represents what C# does and it was
using /Install. However it doesn't seem to work, instead I get errors
saying I need to run InstallUtil.exe. I looked at InstallUtil.exe and
ran it against my exe it seems to run successfully, but out side of
created 2 more files, it didn't seem to do anything.

You use Installutil servcename.exe to install the service.
(b) Once I get it install I'll need to know how to remove it from
services, like /UnRegservice does in C++.

You use InstallUtil -u servicename.exe.
3) How to add an icon to my service?
(a) I created a resource file with an icon, but it seems to be
ignoring the icon.

You can use Google look it up. I am sure you'll find an example.
 
C

Chizl

Yea, I figured out the icon by mistake going through the properties of the
project.. All I had to do is select it..
I did a search to find OnStart, I had never seen it.. Seems it's the .cs
that I double click and it shows me a properties page instead of the actual
code.

However the Installation is still a problem. I run "installutil
myexe.exe", it says success. From what I read it say I then just do a "net
start <service name>" That a problem, because before I can do that, should I
see the service in my service manager? It's not there.
 
M

Mr. Arnold

Chizl said:
Yea, I figured out the icon by mistake going through the properties of the
project.. All I had to do is select it..
I did a search to find OnStart, I had never seen it.. Seems it's the .cs
that I double click and it shows me a properties page instead of the
actual code.

You should be able to see the code of the OnStart(), at least, the place
holder code that is places there for each of the Service code events if you
use one of them.. Maybe, you need to right-click and do a View Code.
However the Installation is still a problem. I run "installutil
myexe.exe", it says success. From what I read it say I then just do a
"net start <service name>" That a problem, because before I can do that,
should I see the service in my service manager? It's not there.

If you don't see the Service listed off of Control Panel/Admin
Tools/Services, then you can do a reboot to see if it shows.
 
C

Chizl

I've created a new project, Window Service, nothing in it at all, run
InstallUtil on the test.exe and it doesnt do anything..

Maybe it's my version.. I'm using Visual Studio 2008 which uses Framework
v3.5. However I noticed that the only InstallUtil I have is for 1.1 and
2.0, there wasn't one for 3.0 or 3.5..
 
W

Willy Denoyette [MVP]

Chizl said:
I've created a new project, Window Service, nothing in it at all, run
InstallUtil on the test.exe and it doesnt do anything..

Maybe it's my version.. I'm using Visual Studio 2008 which uses Framework
v3.5. However I noticed that the only InstallUtil I have is for 1.1 and
2.0, there wasn't one for 3.0 or 3.5..

Your service project is incomplete, you are missing the Installer stuff.
Search the docs (MSDN), it should have a Windows Services tutorial.

Willy.
 
C

Chizl

I have and I'm still not getting it.. I can get it to show up in the
Service Manager now, but when I run it, it's like it never starts it..
Service Manager says it's running, the event log says its running. I have
Log writes I do in Main() and it never writes anything.. Only in service
mode.. If I hit F5 I get the log file info just fine.. Since Main is the
first thing that starts, I'm confused of why it would not execute the first
line which is my Log write..
 
C

Chizl

This is getting insanely annoying.. I'm missing something simple and I've
been all:
http://msdn2.microsoft.com/en-us/library/aa983583(VS.71).aspx
http://www.developer.com/net/csharp/article.php/10918_2173801_1
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx

Based on MS the basics is:
ServiceName, I have..
Installers, I have..
Override the OnStart, I have..

Main is acting as if it's not being called when running as a service and I'm
about go back to C++.. Seems kind of stupid, I click on "Windows Service"
and then I have to add all the service components to make it work.. Why
isn't that by default!!?? The parts of it being a service, is something MS
does anyway, I'm just naming it, give it a description, and threading it off
to my function so it can wait for windows messages..
 
W

Willy Denoyette [MVP]

Chizl said:
I have and I'm still not getting it.. I can get it to show up in the
Service Manager now, but when I run it, it's like it never starts it..
Service Manager says it's running, the event log says its running. I have
Log writes I do in Main() and it never writes anything.. Only in service
mode.. If I hit F5 I get the log file info just fine.. Since Main is
the first thing that starts, I'm confused of why it would not execute the
first line which is my Log write..

If the Service Manager says it's running, it means that "OnStart" has been
called and returned successfully in a timely fashion.
That would mean that there should be at least an information message in the
Application log that reads like:
Service started succesfully.
with the Source set to your "service name", this message is logged by the
SCM after your OnStart returned.
If you are writing to the eventlog from your Main, you should see this
message in the eventlog before the above message.
If you see the SCM message but not your own message, there must be something
wrong with your code that logs to the eventlog.

Willy.
 
M

Mr. Arnold

Chizl said:
This is getting insanely annoying.. I'm missing something simple and
I've been all:
http://msdn2.microsoft.com/en-us/library/aa983583(VS.71).aspx
http://www.developer.com/net/csharp/article.php/10918_2173801_1
http://www.c-sharpcorner.com/UploadFile/mahesh/window_service11262005045007AM/window_service.aspx

Based on MS the basics is:
ServiceName, I have..
Installers, I have..
Override the OnStart, I have..

Main is acting as if it's not being called when running as a service and
I'm about go back to C++.. Seems kind of stupid, I click on "Windows
Service" and then I have to add all the service components to make it
work.. Why isn't that by default!!?? The parts of it being a service, is
something MS does anyway, I'm just naming it, give it a description, and
threading it off to my function so it can wait for windows messages..

You can roll your on too.

http://www.dotnetzone.gr/cs/blogs/sfilip/articles/5484.aspx

BTW, what O/S platform are you trying to develop this on?

You mention VS 2008. Maybe, you need to fallback to VS 2005 to see it work.

And as far as something being the default, it is what it is, and there is
nothing you can do about it -- work with it.

It also sounds like it has blown up and you don't know that it has done
that. You can't debug the Onstart() with the debugger. Maybe you need to do
some try/catch(s) with an Eventlog or dumping of messages in an Eventlog to
tell you where you're at in code. Maybe, if you're developing on a non
Vista NT based O/S, then you can do a NetSend with a messages in code to
tell you where you're at in the code.
 
C

Chizl

Mr. Arnold said:
You can roll your on too.

http://www.dotnetzone.gr/cs/blogs/sfilip/articles/5484.aspx

BTW, what O/S platform are you trying to develop this on?

You mention VS 2008. Maybe, you need to fallback to VS 2005 to see it
work.

And as far as something being the default, it is what it is, and there is
nothing you can do about it -- work with it.

It also sounds like it has blown up and you don't know that it has done
that. You can't debug the Onstart() with the debugger. Maybe you need to
do some try/catch(s) with an Eventlog or dumping of messages in an
Eventlog to tell you where you're at in code. Maybe, if you're developing
on a non Vista NT based O/S, then you can do a NetSend with a messages in
code to tell you where you're at in the code.


I'm running on XP Pro with the latest SP.. I'm one of the Vista users that
went back to XP.. I tried for 9 months and finally gave up.

Here is what I figured out since my last post.. I found that under
ProjectInstaller.cs, the ProjectInstaller() method is being called, but I'm
not doing anything in ProjectInstaller() method.. Am I supposed to?
Based on posts and reading, the OnStart which is in the Service1.cs is where
I'm supposed to thread off. This however is never being called.

I created a bare demo that has nothing in it, but the defaults, added the
installer, added a class I can thread off to.
Here is that code:


Here are my logs..

Hitting F5, I get the error it's a service and cant be run, bla, bla, bla..
Understandable..
2008-01-12 17:34:32:7656,Program.cs->Main() Called
2008-01-12 17:34:32:7656,Service1.cs->Service1() Called
2008-01-12 17:34:32:7656,Program.cs->ServiceBase.Run() Calling
2008-01-12 17:34:34:2500,Program.cs->ServiceBase.Run() Returned
2008-01-12 17:34:34:2500,Program.cs->Main() Leaving

When I run under a service mode, this is all I get..
2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Called
2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Leaving
 
C

Chizl

Does anyone have a sample bare service written in C# 2008? All the samples
I find, put 1000 other things into it, I cant tell what is required for the
service and what is some thing they are doing.

I've gotten my webserver to process 721 requests per seconds. 86538 hits
in 2 minutes. It really sucks I get this far and cant get the stupid thing
to run as a service.

http://70.136.66.85/StressTest.txt
 
W

Willy Denoyette [MVP]

The service gets started, take a look in the eventlog you will see a message
that it was started. Like I said before OnStart is called otherwise your
service would not enter the running state.
The problem is with your logging code, you are writing to the console
window, but a service has NO console window attached, so your messages are
going nowhere. You should log to a file or the eventlog or use a trace
listener that logs to a debugger.

Willy.
 
M

Mr. Arnold

Chizl said:
"Mr. Arnold" <MR. (e-mail address removed)> wrote in message

Here is what I figured out since my last post.. I found that under
ProjectInstaller.cs, the ProjectInstaller() method is being called, but
I'm

I have never used the ProjectInstaller.cs You don't needed in the project,
really. And maybe, that's your problem. I think most people use it, because
it allows one to install a logfile that an Eventlog will use, and maybe,
some other stuff along those lines.

But once I found out that I can create a Eventlog in code and hook the
application to the Eventlog on the fly, I had no more use for the
ProjectInstaller.cs. The Createventlog() is in the Onstart(). :)
 
C

Chizl

My TRACE writes to file as well, guess you missed that.. It's running, but
never hitting OnStart..
 
C

Chizl

Mr. Arnold said:
I have never used the ProjectInstaller.cs You don't needed in the project,
really. And maybe, that's your problem. I think most people use it,
because it allows one to install a logfile that an Eventlog will use, and
maybe, some other stuff along those lines.

But once I found out that I can create a Eventlog in code and hook the
application to the Eventlog on the fly, I had no more use for the
ProjectInstaller.cs. The Createventlog() is in the Onstart(). :)

When I add an installer of which MS tells me I have to do, it creates the
ProjectInstaller.cs.. If there is a better way, please elaborate.. I
just want this to run as a service, I don't really care how at this point.
 
W

Willy Denoyette [MVP]

Chizl said:
My TRACE writes to file as well, guess you missed that.. It's running,
but never hitting OnStart..

Or you are running your service in the "Network Service" account, which has
no privilege to create a file in the current working directory, which is
%sysdir%system32 for a service, or you did not look at the file under
%sysdir%system32, in which case you would have seen something like this:

2008-01-14 00:09:26:0477,Program.cs->Main() Called
2008-01-14 00:09:26:0789,Service1.cs->Service1() Called
2008-01-14 00:09:26:0789,Program.cs->ServiceBase.Run() Calling
2008-01-14 00:09:26:0789,Service1.cs->OnStart() Called
2008-01-14 00:09:26:0789,Service1.cs->OnStart() Leaving
2008-01-14 00:09:26:0946,Control.cs->CallMe() Called with Param: Start this
up
2008-01-14 00:09:31:0943,Control.cs->CallMe() Leaving


So, or you should set your path to a directory that can be accessed by
"Network Service", or you should look in %sysdir%system32. You should check
the return in from WriteLine in TRACE, in order to catch the possible
access denied error.

Willy.
 
C

Chizl

Willy Denoyette said:
Or you are running your service in the "Network Service" account, which
has no privilege to create a file in the current working directory, which
is %sysdir%system32 for a service, or you did not look at the file under
%sysdir%system32, in which case you would have seen something like this:

2008-01-14 00:09:26:0477,Program.cs->Main() Called
2008-01-14 00:09:26:0789,Service1.cs->Service1() Called
2008-01-14 00:09:26:0789,Program.cs->ServiceBase.Run() Calling
2008-01-14 00:09:26:0789,Service1.cs->OnStart() Called
2008-01-14 00:09:26:0789,Service1.cs->OnStart() Leaving
2008-01-14 00:09:26:0946,Control.cs->CallMe() Called with Param: Start
this up
2008-01-14 00:09:31:0943,Control.cs->CallMe() Leaving


So, or you should set your path to a directory that can be accessed by
"Network Service", or you should look in %sysdir%system32. You should
check the return in from WriteLine in TRACE, in order to catch the
possible access denied error.

Willy.

The logs you see above are in .log files, I write to the same folder as the
EXE. I thought I was running under LocalSystem, but as I just looked I
realized I'm running under LocalService. I changed it to LocalSystem, but
I'm still getting the same results.

The source code is posted, it's a 10k download..

Remove the space in the url below.. For some reason the newsgroup or my
reader is removing the link when I have the full link posted so I had to add
a space
http://www.chizl.com/aspuploaded/ WinSrvsTest.zip
 
W

Willy Denoyette [MVP]

Chizl said:
The logs you see above are in .log files, I write to the same folder as
the EXE. I thought I was running under LocalSystem, but as I just looked
I realized I'm running under LocalService. I changed it to LocalSystem,
but I'm still getting the same results.

The source code is posted, it's a 10k download..

Remove the space in the url below.. For some reason the newsgroup or my
reader is removing the link when I have the full link posted so I had to
add a space
http://www.chizl.com/aspuploaded/ WinSrvsTest.zip



The logs I posted are from YOUR project, only change I made was specifying
an absolute path for the log file, following is why I did.
The log file of this *Service* is NOT in the exe path, the log file you see
in the exe path is the one created when running installutil.
You are writing to a file that does not specify a path ( a relative path),
that means you are writing your logfile in the home of the application that
started the service. Services are started by the SCM which has it's home
path set to Windows\system32, your service will try to create/write to
windows\system32.
If your service runs as LocalSystem, you should find the log file in
Windows\system32 (but you should never do this), if it runs as LocalService,
you won't see a logfile at all, this account cannot write to the
Windows\system32, and you fail to check the result of the logging.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top