.NET Forum / Languages / C# / January 2008
Services..
|
|
Thread rating:  |
Chizl - 09 Jan 2008 23:46 GMT 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.
Mr. Arnold - 10 Jan 2008 00:12 GMT >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 [quoted text clipped - 10 lines] > (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 [quoted text clipped - 3 lines] > 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.
Chizl - 10 Jan 2008 04:34 GMT 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.
>>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 [quoted text clipped - 35 lines] > > You can use Google look it up. I am sure you'll find an example. Mr. Arnold - 10 Jan 2008 05:33 GMT > 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.
>>>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 [quoted text clipped - 35 lines] >> >> You can use Google look it up. I am sure you'll find an example. Chizl - 11 Jan 2008 00:52 GMT 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..
> Yea, I figured out the icon by mistake going through the properties of the > project.. All I had to do is select it.. [quoted text clipped - 46 lines] >> >> You can use Google look it up. I am sure you'll find an example. Willy Denoyette [MVP] - 11 Jan 2008 10:39 GMT > 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.
Chizl - 12 Jan 2008 05:09 GMT 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..
>> I've created a new project, Window Service, nothing in it at all, run >> InstallUtil on the test.exe and it doesnt do anything.. [quoted text clipped - 7 lines] > > Willy. Willy Denoyette [MVP] - 12 Jan 2008 09:17 GMT >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.. [quoted text clipped - 3 lines] >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.
Chizl - 12 Jan 2008 05:46 GMT 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/wi ndow_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..
>> I've created a new project, Window Service, nothing in it at all, run >> InstallUtil on the test.exe and it doesnt do anything.. [quoted text clipped - 7 lines] > > Willy. Mr. Arnold - 12 Jan 2008 10:41 GMT > This is getting insanely annoying.. I'm missing something simple and > I've been all: [quoted text clipped - 13 lines] > 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.
Chizl - 13 Jan 2008 00:00 GMT > You can roll your on too. > [quoted text clipped - 14 lines] > 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
Chizl - 13 Jan 2008 00:13 GMT That's weird my link was removed.. It was a link to my website, which is a zip file.. Zip of the solution.. Trying again..
Remove the space before WinSrvsTest.zip.. http://www.chizl.com/aspuploaded/ WinSrvsTest.zip
>> You can roll your on too. >> [quoted text clipped - 41 lines] > 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Called > 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Leaving Chizl - 13 Jan 2008 09:44 GMT 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
> That's weird my link was removed.. It was a link to my website, which is > a zip file.. Zip of the solution.. Trying again.. [quoted text clipped - 47 lines] >> 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Called >> 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Leaving Willy Denoyette [MVP] - 13 Jan 2008 10:09 GMT 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.
> That's weird my link was removed.. It was a link to my website, which is > a zip file.. Zip of the solution.. Trying again.. [quoted text clipped - 47 lines] >> 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Called >> 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Leaving Chizl - 13 Jan 2008 20:09 GMT My TRACE writes to file as well, guess you missed that.. It's running, but never hitting OnStart..
> 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 [quoted text clipped - 58 lines] >>> 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Called >>> 2008-01-12 17:36:12:1093,ProjectInstaller.cs->ProjectInstaller() Leaving Willy Denoyette [MVP] - 13 Jan 2008 23:22 GMT > 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.
Chizl - 14 Jan 2008 01:10 GMT > 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 [quoted text clipped - 16 lines] > > 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
Willy Denoyette [MVP] - 14 Jan 2008 10:30 GMT >> 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 [quoted text clipped - 28 lines] > 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.
Mr. Arnold - 14 Jan 2008 11:34 GMT >>> Or you are running your service in the "Network Service" account, which >>> has no privilege to create a file in the current working directory, [quoted text clipped - 43 lines] > 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. What's wrong writing to his own System Application Eventlog?
Willy Denoyette [MVP] - 14 Jan 2008 11:45 GMT >>>> Or you are running your service in the "Network Service" account, which >>>> has no privilege to create a file in the current working directory, [quoted text clipped - 45 lines] > > What's wrong writing to his own System Application Eventlog? Nothing, but this is not what the OP is doing, he's writing to a regular file not to the Eventlog.
Willy.
Mr. Arnold - 14 Jan 2008 12:11 GMT >>>>> Or you are running your service in the "Network Service" account, >>>>> which has no privilege to create a file in the current working [quoted text clipped - 49 lines] > Nothing, but this is not what the OP is doing, he's writing to a regular > file not to the Eventlog. He should dump it or use the Application Block to write to a log file.
Chizl - 15 Jan 2008 02:58 GMT >>> What's wrong writing to his own System Application Eventlog? >>> >> Nothing, but this is not what the OP is doing, he's writing to a regular >> file not to the Eventlog. > > He should dump it or use the Application Block to write to a log file. When I stress test, I'm writing over 80,000 to the log.. This is a web server that logs everything. EventLog is not meant for that.
I see what your saying about it's not a relative path.. I've also noticed, I'm using / instead of \ so that I can compile this under Mono for CrossPlatform development, but seems as a service, it's having a problem with it..
I added: System.Diagnostics.Debugger.Launch(); so I could start walking through the service and there is all kinds of path issues I have to resolve so that it will run as a service..
What is the best way to get Local Path? I'm using: System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); However, that returns file:// in front of it.. Is there a faster or better way?
Mr. Arnold - 15 Jan 2008 23:25 GMT >>>> What's wrong writing to his own System Application Eventlog? >>>> [quoted text clipped - 5 lines] > When I stress test, I'm writing over 80,000 to the log.. This is a web > server that logs everything. EventLog is not meant for that. Why reinvent the wheel? There is a whole section about using the Application Blocks' logging abilities.
<http://www.microsoft.com/downloads/details.aspx?FamilyID=5A14E870-406B-4F2A-B723 -97BA84AE80B5&displaylang=en>
> I see what your saying about it's not a relative path.. I've also > noticed, I'm using / instead of \ so that I can compile this under Mono [quoted text clipped - 9 lines] > However, that returns file:// in front of it.. Is there a faster or > better Well, I think that's what I use. So you make a String Function that returns what you want out of the string path. :)
Chizl - 16 Jan 2008 03:19 GMT > "Chizl" <Chizl@NoShitMail.com> wrote in message >> What is the best way to get Local Path? I'm using: [quoted text clipped - 4 lines] > Well, I think that's what I use. So you make a String Function that > returns what you want out of the string path. :) What I ended up doing is creating a static property that stores this info in a var, so I'm not executing the same IO.Path each time..
public static String AppPath { get { return m_szAppPath; } set { m_szAppPath = value; } }
Chizl - 15 Jan 2008 04:09 GMT I have a SERVICE!!! WOOT!! It had to do with all the path issues, which finding the debug call while running as a service helped a lot.. I want to thank both Willy and Mr. Arnald.. You guys rock..
But with all this, I think that kind of sucks.. Running as a service doesn't use the same path methods as an application. It's almost as if two different departments wrote C# at MS.
Thanks again..
 Signature /*Chizl*/
Willy Denoyette [MVP] - 15 Jan 2008 09:36 GMT >I have a SERVICE!!! WOOT!! It had to do with all the path issues, which >finding the debug call while running as a service helped a lot.. I want to [quoted text clipped - 3 lines] > doesn't use the same path methods as an application. It's almost as if > two different departments wrote C# at MS. This has absolutely nothing to do with C#, this is how Windows and Windows Services work, the implementation language is not relevant. Window Services must adhere to the rules imposed by the SCM, who's the parent process of all Services. And as it goes with all processes in windows, your Service inherits it's environment from it's parent (the SCM) which is not the same as the environment of a process launched from a command shell or the interactive shell (explorer for instance). Note also that services do not have a profile loaded, cannot (by default) access the visible desktop, nor do they run in the same security context as an interactive logon session (which may or may not be present). Services are background tasks (similar to daemons on **ix), they may run in their own process, or they may share a process with other services, mostly started at boot time and running till shutdown, they have a dedicated task to accomplish, and because of their nature, they must be as robust as possible (especially when they share a process with others).
Willy.
Mr. Arnold - 13 Jan 2008 16:51 GMT > "Mr. Arnold" <MR. Arnold@Arnold.com> wrote in message <snipped>
> 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(). :)
Chizl - 13 Jan 2008 20:11 GMT > 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, [quoted text clipped - 4 lines] > 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.
Mr. Arnold - 14 Jan 2008 11:58 GMT >> 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 [quoted text clipped - 8 lines] > 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. As you see in the two examples, there is no talk about the Projectinstall.cs. You don't need it. You simply create your Windows Service project without the ProjectInstaller.cs, build the exe, and you use InstallUtil to install the service.exe.
Now, if you have to use a ProjectInstaller.cs, then by all means use it. But it doesn't mean that you have to use it.
http://www.aspfree.com/c/a/C-Sharp/Creating-a-Windows-Service-with-C-Sharp-intro duction/ http://www.vbdotnetheaven.com/UploadFile/mahesh/Winservvb11172005233242PM/Winser vvb.aspx
Chizl - 15 Jan 2008 03:00 GMT >>> 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 [quoted text clipped - 20 lines] > http://www.aspfree.com/c/a/C-Sharp/Creating-a-Windows-Service-with-C-Sharp-intro duction/ > http://www.vbdotnetheaven.com/UploadFile/mahesh/Winservvb11172005233242PM/Winser vvb.aspx Thanks, I appreciate all the help, thus far.. Banging my head on the desk, since this is the thing out of all the stuff I've done that doesn't want to work..
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|