getting started with windows services

M

michael sorens

(1) I tried to use what seems like a standard line (from Walkthrough:
Creating a Windows Service Application in the Component Designer at
http://msdn2.microsoft.com/en-us/library/zt39148a.aspx):

if (!System.Diagnostics.EventLog.SourceExists("MySource")) { ...

But received this error when attempting to start the service:

System.Security.SecurityException was unhandled
Message: The source was not found, but some or all event logs could not
be searched. Inaccessible logs: Security.

(2) Per the walkthrough, I had set up the account on the
ServiceProcessInstaller to LocalService, which I assumed had insufficent
privilege. So I tried to recompile after setting this account to a
different choice. I then naively tried to install from the setup project
again, but got a message that the "service was marked for deletion" and I
needed to re-run the installer. But re-running gave the same message.

(3) I then tried to use "installutil /u MyNewService" to remove the
service outside of VS so I could re-install. But installutil did not
recognize the "/u" option! It just gave a list of "command/test/option
names". (This is on WinXP Pro.)

(4) So I re-launched VS and tried the installer again and this time it
installed successfully. But unfortunately, when I tried to start the
service, it produced the original error in (1) above.

Questions:
(A) Why am I getting a SecurityException and how can I get around it?
(B) What is the appropriate way to re-install a service?
(C) Why does installutil /u not work as advertised?
 
R

Robbe Morris [C# MVP]

This will probably be helpful. It has self installing capabilities.

Self-Updating Windows Service Infrastructure with Command Pattern Message
Queue Invoker Service

http://www.eggheadcafe.com/articles/20041204.asp

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
 
W

Walter Wang [MSFT]

Hi Michael,

Since creating event log source requires administrative privilege, and it's
only needed to create once, I recommend you create the event log source in
the installer rather than in the service.

We can move the code to create the event log source from MyNewService to
ProjectInstaller:

public partial class MyNewService : ServiceBase
{
public MyNewService()
{
InitializeComponent();

eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();

if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource("MySource",
"MyNewLog");
}

As the walkthrough mentioned, reinstalling the service on Windows 2000 will
needs rebooting since the service control manager will first disable the
service for deleting on reboot; for Windows xp and above, the windows
installer has some feature can reinstall the service without rebooting.
That's why you need to use the generated setup file to re-install the
service.

The installutil will need an assembly and install/uninstall the installers
inside the assembly. In this case, we don't need to use installutil
manually, since we've already created a setup project to call the installer
within the service assembly.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Willy Denoyette [MVP]

| (1) I tried to use what seems like a standard line (from Walkthrough:
| Creating a Windows Service Application in the Component Designer at
| http://msdn2.microsoft.com/en-us/library/zt39148a.aspx):
|
| if (!System.Diagnostics.EventLog.SourceExists("MySource")) { ...
|
| But received this error when attempting to start the service:
|
| System.Security.SecurityException was unhandled
| Message: The source was not found, but some or all event logs could not
| be searched. Inaccessible logs: Security.
|
| (2) Per the walkthrough, I had set up the account on the
| ServiceProcessInstaller to LocalService, which I assumed had insufficent
| privilege. So I tried to recompile after setting this account to a
| different choice. I then naively tried to install from the setup project
| again, but got a message that the "service was marked for deletion" and I
| needed to re-run the installer. But re-running gave the same message.
|
| (3) I then tried to use "installutil /u MyNewService" to remove the
| service outside of VS so I could re-install. But installutil did not
| recognize the "/u" option! It just gave a list of "command/test/option
| names". (This is on WinXP Pro.)
|
| (4) So I re-launched VS and tried the installer again and this time it
| installed successfully. But unfortunately, when I tried to start the
| service, it produced the original error in (1) above.
|
| Questions:
| (A) Why am I getting a SecurityException and how can I get around it?
| (B) What is the appropriate way to re-install a service?
| (C) Why does installutil /u not work as advertised?

In order to help you out with this, we need to see more code.
if (!System.Diagnostics.EventLog.SourceExists("MySource")) { ...
is of little help as this line is not the cause of the exception, probably
what's inside the {... is.
Anyway, your problem is security related, notably a lack of registry
privileges. My guess is that you are trying to create a log, but the
LocalService account (a restricted account!) has no privileges to do so.
Creating logs from withing services is not advisable, you need to create the
log using a separate installer program or a simple script.


Willy.
 
M

michael sorens

As both Walter and Willy suggest that best practice dictates not to create
the event log within the service, I am fine with that. I was merely using
the sample code from Microsoft's primer. So I moved that couple lines of
code from Service1.cs to ProjectInstaller.cs as Walter showed. I then
opened the setup project context menu and selected Uninstall first, then
Install, which I am guessing is the better way to try re-installing. Upon
completion of the Install, my event log was created. (So I assume the
setup project automatically uses a higher privilege than what I set for
the Account property of the ServiceProcessInstaller, yes?) I then used the
ServerExplorer to locate the service, started it, and it successfully
added an entry in the new event log.

So I succeeded in getting my first service to execute properly, but I like
to understand my loose ends.
(4) Why does Microsoft's sample code fail on the security exception--is
that just a case of old code in the walkthrough? (And BTW, the exception
was indeed on the SourceExists method, and the text of the error message I
provided supports that. I had not even gotten to the creation of my new
log, which would most likely also throw a security exception...)
(5) And why does installUtil not work as advertised on my system?

Thanks to Walter and Willy for the good tip here. Thanks also to Robbe for
the intriguing article on managing services without stopping them--it is a
useful tool for serious applications.
 
W

Walter Wang [MSFT]

Hi Michael,

(4) Yes the documentation is somewhat out-of-date.

(5) Previously I said it's no need to use installutil manually since we're
creating a setup which generates .msi file. That doesn't mean installutil
doesn't work as advertised. I just tested it again:

C:\projects\_All\MyNewService\bin\Debug>installutil MyNewService.exe
Microsoft (R) .NET Framework Installation utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.


Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the
C:\projects\_All\MyNewService\bin\Debug
\MyNewService.exe assembly's progress.
The file is located at
C:\projects\_All\MyNewService\bin\Debug\MyNewService.Inst
allLog.
Installing assembly
'C:\projects\_All\MyNewService\bin\Debug\MyNewService.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\projects\_All\MyNewService\bin\Debug\MyNewService.exe
logfile = C:\projects\_All\MyNewService\bin\Debug\MyNewService.InstallLog
Installing service MyNewService...
Service MyNewService has been successfully installed.
Creating EventLog source MyNewService in log Application...

The Install phase completed successfully, and the Commit phase is beginning.
See the contents of the log file for the
C:\projects\_All\MyNewService\bin\Debug
\MyNewService.exe assembly's progress.
The file is located at
C:\projects\_All\MyNewService\bin\Debug\MyNewService.Inst
allLog.
Committing assembly
'C:\projects\_All\MyNewService\bin\Debug\MyNewService.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\projects\_All\MyNewService\bin\Debug\MyNewService.exe
logfile = C:\projects\_All\MyNewService\bin\Debug\MyNewService.InstallLog

The Commit phase completed successfully.

The transacted install has completed.

C:\projects\_All\MyNewService\bin\Debug>net start mynewservice
The MyNewService service is starting.
The MyNewService service was started successfully.


C:\projects\_All\MyNewService\bin\Debug>installutil /u MyNewService.exe
Microsoft (R) .NET Framework Installation utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.



The uninstall is beginning.
See the contents of the log file for the
C:\projects\_All\MyNewService\bin\Debug
\MyNewService.exe assembly's progress.
The file is located at
C:\projects\_All\MyNewService\bin\Debug\MyNewService.Inst
allLog.
Uninstalling assembly
'C:\projects\_All\MyNewService\bin\Debug\MyNewService.exe'
 
M

michael sorens

Whether trying to install or uninstall, I get the same error from
installutil:
installutil MyNewService.exe
Error dispatching command/test named 'MyNewService.exe'

And I figured out why: my search path was picking up a *different* program
called 'installutil' (this one from Rational). Once I adjusted it, now I
get the .Net utility. Mystery solved.
 
W

Walter Wang [MSFT]

Hi Michael,

Thank you for letting us know the cause.

Have a nice day!

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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