By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

getting started with windows services

P: n/a
(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/lib...t39148a.aspx):

if (!System.Diagnostics.EventLog.SourceExists("MySour ce")) { ...

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?
Oct 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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/..._generator.asp

"michael sorens" <m_********@community.nospamwrote in message
news:op***************@spo-cont-2-dt.itron.com...
(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/lib...t39148a.aspx):

if (!System.Diagnostics.EventLog.SourceExists("MySour ce")) { ...

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?

Oct 2 '06 #2

P: n/a
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("MySour ce"))
{
System.Diagnostics.EventLog.CreateEventSource("MyS ource",
"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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 3 '06 #3

P: n/a

"michael sorens" <m_********@community.nospamwrote in message
news:op***************@spo-cont-2-dt.itron.com...
| (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/lib...t39148a.aspx):
|
| if (!System.Diagnostics.EventLog.SourceExists("MySour ce")) { ...
|
| 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("MySour ce")) { ...
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.

Oct 3 '06 #4

P: n/a
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.
Oct 3 '06 #5

P: n/a
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>installuti l 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\MyNewServi ce.Inst
allLog.
Installing assembly
'C:\projects\_All\MyNewService\bin\Debug\MyNewServ ice.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\projects\_All\MyNewService\bin\Debug\MyNewServi ce.exe
logfile = C:\projects\_All\MyNewService\bin\Debug\MyNewServi ce.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\MyNewServi ce.Inst
allLog.
Committing assembly
'C:\projects\_All\MyNewService\bin\Debug\MyNewServ ice.exe'.
Affected parameters are:
logtoconsole =
assemblypath = C:\projects\_All\MyNewService\bin\Debug\MyNewServi ce.exe
logfile = C:\projects\_All\MyNewService\bin\Debug\MyNewServi ce.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>installuti l /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\MyNewServi ce.Inst
allLog.
Uninstalling assembly
'C:\projects\_All\MyNewService\bin\Debug\MyNewServ ice.exe'
Oct 5 '06 #6

P: n/a
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.
Oct 9 '06 #7

P: n/a
Hi Michael,

Thank you for letting us know the cause.

Have a nice day!

Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Oct 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.