all groups > dotnet myservices > february 2005 >
You're in the

dotnet myservices

group:

Passing startup parameters to a .NET service


Passing startup parameters to a .NET service Dave Hall
2/25/2005 11:03:43 AM
dotnet myservices: How do I pass arguments at the startup of my service derived from
ServiceBase?

I have added code in the overidden OnStart(string[] args) function to use
the args parameter, but when I try to edit the Startup Parameters in the
Control Panel Sercices Applet, the applet won't save the value I provide. I
type my parameters in the appropriate text box ("Port=999") and then click
OK. When I re-open the properties dialog for that service, the value I typed
in is no longer present. The start parameters textbox is empty. I'm pretty
sure that if I could get XP to save the value I type in, that I would see it
on my overridden OnStart(string[] args) method. Is there somethig else I
need to do?

Dave

Re: Passing startup parameters to a .NET service Lee Gillie
2/25/2005 4:15:49 PM
[quoted text, click to view]

Dave -

..NET ServiceBase is incomplete. There are other important things you
can't do with it as well. But you can call the Win32 API for things such
as this. If they would ONLY give us access to the internal handle!!!!
But you can still deal with it.

Re: Passing startup parameters to a .NET service Dave Hall
3/1/2005 10:45:49 PM
Although the ServiceBase class may be incomplete as you say, I still don't
understand why the control panel applet won't even let me type in any
parameters. If I could do that, but still couldn't access the parameters
from my code, I could see your point, but this doesn't seem to be a .NET
specific issue, but rather a generic OS service issue. Any other insight?

Thanks,
Dave

[quoted text, click to view]

Re: Passing startup parameters to a .NET service Lee Gillie
3/3/2005 1:05:48 PM
Dave -

Your issue is that the SERVICES applet does not persist the value? Yes
that is how it was designed. It has a purpose, but as you see is mostly
useless. There is a way to have persisting parameters though. Why not
use that feature instead of wishing the non-persisting parameters
approach would persist?

What I typically do for persisting parameters is to store those values
in the registry myself. Often many things, including database connection
strings, folders were files are used. Options settings. And so on. I
write a control-panel applet, or a program that auto-starts on logon,
and sits in the tray. And this provides for control of the service,
editing my persisted settings, and often some actual run status
information which I obtain through .NET remoting.

Start parameters are not provided for in the SCM database, such as the
path to exe, service type, service name, and so on. Start parameters
are parameters to the START method, or the invocation of the service. It
is a transitory value specified at invocation of START. For example,
consider that you have a service that is normally stopped, and when
started you want it to monitor a folder. You could pass that folder that
is to be used for this run of the service.

Although, .NET has highly outdated this book, I highly recommend
"Professional NT Services" by Kevin Miller, WROX Press to ANYONE writing
services (.NET or not!!!). He explains how services should REALLY be
designed, and the infrastructure for them. Then once you see what is
there, and how .NET took some shortcuts and left a bunch of pieces out,
you will see my complaint, that it falls short of the full SCM API
interface for services, and leaves some of the more inventive features
out of the picture.

All of that being said, in a very brief time you COULD provide a
START/STOP/MONITOR application that WOULD persist some start up
parameters, and pass them, and remember them from run to run, but allow
them to be changed. But the typical way to persist configuration is via
the registry. I guess it is just a matter if you are pitching overhand
or underhand? i.e. using the PERSISTING PARAMETERS approach, or wishing
the NON PERSISTING PARAMETERS approach would PERSIST also.

HTH - Lee

[quoted text, click to view]
Re: Passing startup parameters to a .NET service Dave Hall
3/5/2005 7:57:26 PM
Thanks Lee. You hit the nail on the head. I didn't realize that the startup
parameters in the control panel applet were not intended to be persistent. I
somehow assumed they were saved in the SCM database or somewhere in the
registry, and then passed to the service whenever it started, even if it was
set to start automatically. I don't really have any problem passing the
config data to the service through another mechanism such as a registry
setting or a config file. It just seemed like the startup args might be
easier to use. Now that I understand that they were never intended to be
persistent, I'll use one of the other standard methods. After you
enlightened me about how it works, I was able to pass parameters into my
service through the OnStart event with no trouble. Like most things, it
works fine once you know how it's intended to behave.

I don't know about the particular book you mentioned, but in general, for
years, WROX books have been some of the best I've seen on any given subject.
They make up a good part of my library. I'll see if I can track down the
particular book you recommended and take a look at it as well.

Thanks,
Dave

[quoted text, click to view]

AddThis Social Bookmark Button