473,715 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows Service Help

I'm really starting to hate writing services -- or trying to, anyway.

Why do I need to rename my project to the service name?

Why do I need to set the "ServiceNam e" property to my service name?

Why do I need to set a property within my code to the service name?

Are all these required or am I just doing this for consistency purposes?

Now for my real question/problem:

I have written this service and have this in my "SimpeService.c s":

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceP rocess.ServiceB ase
{
private System.Timers.T imer timer;

/// <summary>
/// Required deisgner variable
/// </summary>
public SimpleService()
{
// This call is required by the Windows.Forms Component Designer
InitializeCompo nent();

this.timer = new System.Timers.T imer();
this.timer.Enab led = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.E lapsedEventHand ler(timer_Elaps ed);
CanPauseAndCont inue = true;
this.ServiceNam e = "Hello-World Service";

}

void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{
EventLog.WriteE ntry("Hello World!");
//throw new Exception("The method or operation is not
implemented.");
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteE ntry("Hello-World Service Started");
timer.Enabled = true;
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
EventLog.WriteE ntry("Hello-World Service Paused");
timer.Enabled = false;
}

protected override void OnPause()
{
EventLog.WriteE ntry("Hello-World Service paused");
timer.Enabled = false;
}

protected override void OnContinue()
{
EventLog.WriteE ntry("Hello-World Service continued");
timer.Enabled = true;
}

}
}
This is in my "ProjectInstall er.cs" and I have set the service properties,
etc:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Configur ation.Install;
using System.ServiceP rocess;

namespace WindowsService2
{
[RunInstaller(tr ue)]
public partial class ProjectInstalle r : Installer
{
private ServiceInstalle r serviceInstalle r;
private ServiceProcessI nstaller processInstalle r;

public ProjectInstalle r()
{
InitializeCompo nent();

processInstalle r = new ServiceProcessI nstaller();
serviceInstalle r = new ServiceInstalle r();

// Service will run under system account
processInstalle r.Account = ServiceAccount. LocalSystem;

// Service will have Start Type of Manual
serviceInstalle r.StartType = ServiceStartMod e.Manual;
serviceInstalle r.ServiceName = "Hello-World Service";
Installers.Add( serviceInstalle r);
Installers.Add( processInstalle r);
}
}
}

My "Program.cs " has no changes:

using System.Collecti ons.Generic;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process.
To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new
MySecondUserSer vice()};
//
ServicesToRun = new ServiceBase[] { new SimpleService() };

ServiceBase.Run (ServicesToRun) ;
}
}
}

Now, when I compiel the above, it all compiles fine.

I then jump to Admin Command Prompt and do a "installuti l
WindowsService2 .exe" and it does the following:

It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstall er.cs". But I was
getting this prior to setting any properties under the "ProjectInstall er.cs"
file. Why do I get this?

The format of the credentials appears to only accept "machinename\us ername"
and nothing else. It took me a while to figure this out! Arrghhh

After the service is installed, I appear to have two services:

SimpleService2
Hello-World

SimpleService2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.

Executing "InstallUti l -u WindowsService2 .exe" appears to get rid of both
services.

I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService. cs" file??

I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.

How do I get rid of the dialoge all together?

How do I get rid of "SimpleServ ice" installation, since it appears to not
work anyway.

Any help would be appreciated.


Jun 27 '08 #1
5 3304
All --

I think I made a bit of progress since this post. It would appear the the
"ProjectInstall erDesigner.cs" file has some of the same options that I was
setting; which had default options of null, etc. Remarking this out and
putting the other values in appeared to elimiante two services from being
installed. I'm not sure where I should put my changes. I was under the
impression that I wasn't suppose to modifiy the "Designer" classes.

Here is what is in "ProjectInstall erDesigner.cs"

namespace WindowsService2
{
partial class ProjectInstalle r
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.IContain er components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing ">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Disp ose();
}
base.Dispose(di sposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.servicePro cessInstaller1 = new
System.ServiceP rocess.ServiceP rocessInstaller ();
this.serviceIns taller1 = new
System.ServiceP rocess.ServiceI nstaller();
//
// serviceProcessI nstaller1
//
//this.servicePro cessInstaller1. Password = null;
//this.servicePro cessInstaller1. Username = null;
this.servicePro cessInstaller1. Account =
System.ServiceP rocess.ServiceA ccount.LocalSys tem;
//
// serviceInstalle r1
//
this.serviceIns taller1.Service Name = "Hello-World2";
this.serviceIns taller1.StartTy pe =
System.ServiceP rocess.ServiceS tartMode.Manual ;
//
// ProjectInstalle r
//
this.Installers .AddRange(new
System.Configur ation.Install.I nstaller[] {
this.servicePro cessInstaller1,
this.serviceIns taller1});

}

#endregion

private System.ServiceP rocess.ServiceP rocessInstaller
serviceProcessI nstaller1;
private System.ServiceP rocess.ServiceI nstaller serviceInstalle r1;
}
}

"dm3281" <dm****@nospam. netwrote in message
news:43******** *************** ***********@mic rosoft.com...
I'm really starting to hate writing services -- or trying to, anyway.

Why do I need to rename my project to the service name?

Why do I need to set the "ServiceNam e" property to my service name?

Why do I need to set a property within my code to the service name?

Are all these required or am I just doing this for consistency purposes?

Now for my real question/problem:

I have written this service and have this in my "SimpeService.c s":

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceP rocess.ServiceB ase
{
private System.Timers.T imer timer;

/// <summary>
/// Required deisgner variable
/// </summary>
public SimpleService()
{
// This call is required by the Windows.Forms Component
Designer
InitializeCompo nent();

this.timer = new System.Timers.T imer();
this.timer.Enab led = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.E lapsedEventHand ler(timer_Elaps ed);
CanPauseAndCont inue = true;
this.ServiceNam e = "Hello-World Service";

}

void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{
EventLog.WriteE ntry("Hello World!");
//throw new Exception("The method or operation is not
implemented.");
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteE ntry("Hello-World Service Started");
timer.Enabled = true;
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
EventLog.WriteE ntry("Hello-World Service Paused");
timer.Enabled = false;
}

protected override void OnPause()
{
EventLog.WriteE ntry("Hello-World Service paused");
timer.Enabled = false;
}

protected override void OnContinue()
{
EventLog.WriteE ntry("Hello-World Service continued");
timer.Enabled = true;
}

}
}
This is in my "ProjectInstall er.cs" and I have set the service properties,
etc:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Configur ation.Install;
using System.ServiceP rocess;

namespace WindowsService2
{
[RunInstaller(tr ue)]
public partial class ProjectInstalle r : Installer
{
private ServiceInstalle r serviceInstalle r;
private ServiceProcessI nstaller processInstalle r;

public ProjectInstalle r()
{
InitializeCompo nent();

processInstalle r = new ServiceProcessI nstaller();
serviceInstalle r = new ServiceInstalle r();

// Service will run under system account
processInstalle r.Account = ServiceAccount. LocalSystem;

// Service will have Start Type of Manual
serviceInstalle r.StartType = ServiceStartMod e.Manual;
serviceInstalle r.ServiceName = "Hello-World Service";
Installers.Add( serviceInstalle r);
Installers.Add( processInstalle r);
}
}
}

My "Program.cs " has no changes:

using System.Collecti ons.Generic;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process.
To add
// another service to this process, change the following line
to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new
MySecondUserSer vice()};
//
ServicesToRun = new ServiceBase[] { new SimpleService() };

ServiceBase.Run (ServicesToRun) ;
}
}
}

Now, when I compiel the above, it all compiles fine.

I then jump to Admin Command Prompt and do a "installuti l
WindowsService2 .exe" and it does the following:

It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstall er.cs". But I was
getting this prior to setting any properties under the
"ProjectInstall er.cs" file. Why do I get this?

The format of the credentials appears to only accept
"machinename\us ername" and nothing else. It took me a while to figure
this out! Arrghhh

After the service is installed, I appear to have two services:

SimpleService2
Hello-World

SimpleService2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.

Executing "InstallUti l -u WindowsService2 .exe" appears to get rid of both
services.

I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService. cs" file??

I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.

How do I get rid of the dialoge all together?

How do I get rid of "SimpleServ ice" installation, since it appears to not
work anyway.

Any help would be appreciated.

Jun 27 '08 #2
If I wanted to use a tool such as AdvancedInstall er from
http://www.caphyon.com/, what would I need to do? Would I need any of these
propteries set within the "ProjectInstall erDesigner.cs" file?

Does AdvancedInstall er allow me to browse to the .EXE, add it to my package,
and then allow me to hardcode or prompt the user for what they want?

What's the best way to do this?
Jun 27 '08 #3
Going to do my best to answer the questions - though you seem to have quite
a few. I've put a block of asterisks at the end of my response since this is
going to be quite large after answering everything. If you have any other
questions please feel free to ask. I work with Windows services a lot. :o)
Why do I need to rename my project to the service name?
You don't, I do it to keep everything consistent.

Why do I need to set the "ServiceNam e" property to my service name?
Because Windows uses this when registering the service, and they must be
unique. Also, if we're on the same page, this is the value used as your
service name when writing events to the event log. Though I could be wrong,
I'm not 100% sure which property you're talking about.

Why do I need to set a property within my code to the service name?
See above.

Are all these required or am I just doing this for consistency purposes?
They're required. If you provide incorrect information the service will have
problems when registering. Which will usually throw an exception that you'll
be able to see if you're registering the service with InstallUtil at the
command prompt.

It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstall er.cs". But I was
getting this prior to setting any properties under the
"ProjectInstall er.cs" file. Why do I get this?
Windows services run under a non-interactive user account on the machines.
Typically when a service is first created in Visual Studio the default
settings require a user account the service will run as. You can change this
by opening your ProjectInstalle r.cs file (double click it to bring up the
designer) and select your processInstalle r component that should be visible.
If you want the service to run as a system account just change the account
to the account type you want. Be cautious about doing this, the system
accounts have unrestricted rights to the machine the service is running on.
The ServiceAccount. User enum requires if the Username and Password
properties on the component aren't set, when the service is being installed
the account information must be prompted. The reason you have to give the
full account (including domain/machine name) is because the service needs
the entire credential when it's starting.

If I wanted to use a tool such as AdvancedInstall er from
http://www.caphyon.com/, what would I need to do? Would I need any of
these propteries set within the "ProjectInstall erDesigner.cs" file?
Does AdvancedInstall er allow me to browse to the .EXE, add it to my
package, and then allow me to hardcode or prompt the user for what they
want?
What's the best way to do this?
This would be a question better suited to be asked by the software vendor. I
typically just use the Setup project available within Visual Studio for
deploying services.

NOTE: InstallUtil will not be available on your target machines as it is not
included with the .NET Framework. You will need to use an installer package
to get the service registered on the deployment machine. Also, about that -
you will need to use a custom installer action pointed to your service
executable to get the installer package to register your service. The
System.Configur ation.Install.I nstaller class (which is the base class for
the ProjectInstalle r) you made, handles registering the service with the
machine. You can use .NET Reflector if you want to actually take a look at
the code in the class how it uses the Win32 API and pinvoke if you're
interested in looking under the hood. :o)

After the service is installed, I appear to have two services:

SimpleService2
Hello-World

SimpleService2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.

Executing "InstallUti l -u WindowsService2 .exe" appears to get rid of both
services.

I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService. cs" file??
This would be a result of not using a consistent service name throughout the
service. My suggestion would be to do a Find All within the IDE and search
for "SimpleService2 " throughout your application and make sure it's updated
to the proper service name.

I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.

How do I get rid of the dialoge all together?
See the above answer on the project installer.

How do I get rid of "SimpleServ ice" installation, since it appears to not
work anyway.
Change the service name in your application back to the original value,
register the service again and finally remove it again. This will get the
old service removed from the machine (hopefully) so it won't be listed.

HINT: When working with Windows services I have found it best to create a
batch file to install and uninstall the services into the program files
folder on my machine when testing. When InstallUtil is executed against the
service assembly the configuration in that executable is what is used during
the install/uninstall process. If you build the software before uninstalling
the old version this is what can result.

*************** *************** *************** *************** **************
*************** *************** *************** *************** **************

"dm3281" <dm****@nospam. netwrote in message
news:43******** *************** ***********@mic rosoft.com...
I'm really starting to hate writing services -- or trying to, anyway.

Why do I need to rename my project to the service name?

Why do I need to set the "ServiceNam e" property to my service name?

Why do I need to set a property within my code to the service name?

Are all these required or am I just doing this for consistency purposes?

Now for my real question/problem:

I have written this service and have this in my "SimpeService.c s":

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceP rocess.ServiceB ase
{
private System.Timers.T imer timer;

/// <summary>
/// Required deisgner variable
/// </summary>
public SimpleService()
{
// This call is required by the Windows.Forms Component
Designer
InitializeCompo nent();

this.timer = new System.Timers.T imer();
this.timer.Enab led = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.E lapsedEventHand ler(timer_Elaps ed);
CanPauseAndCont inue = true;
this.ServiceNam e = "Hello-World Service";

}

void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{
EventLog.WriteE ntry("Hello World!");
//throw new Exception("The method or operation is not
implemented.");
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteE ntry("Hello-World Service Started");
timer.Enabled = true;
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
EventLog.WriteE ntry("Hello-World Service Paused");
timer.Enabled = false;
}

protected override void OnPause()
{
EventLog.WriteE ntry("Hello-World Service paused");
timer.Enabled = false;
}

protected override void OnContinue()
{
EventLog.WriteE ntry("Hello-World Service continued");
timer.Enabled = true;
}

}
}
This is in my "ProjectInstall er.cs" and I have set the service properties,
etc:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Configur ation.Install;
using System.ServiceP rocess;

namespace WindowsService2
{
[RunInstaller(tr ue)]
public partial class ProjectInstalle r : Installer
{
private ServiceInstalle r serviceInstalle r;
private ServiceProcessI nstaller processInstalle r;

public ProjectInstalle r()
{
InitializeCompo nent();

processInstalle r = new ServiceProcessI nstaller();
serviceInstalle r = new ServiceInstalle r();

// Service will run under system account
processInstalle r.Account = ServiceAccount. LocalSystem;

// Service will have Start Type of Manual
serviceInstalle r.StartType = ServiceStartMod e.Manual;
serviceInstalle r.ServiceName = "Hello-World Service";
Installers.Add( serviceInstalle r);
Installers.Add( processInstalle r);
}
}
}

My "Program.cs " has no changes:

using System.Collecti ons.Generic;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process.
To add
// another service to this process, change the following line
to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new
MySecondUserSer vice()};
//
ServicesToRun = new ServiceBase[] { new SimpleService() };

ServiceBase.Run (ServicesToRun) ;
}
}
}

Now, when I compiel the above, it all compiles fine.

I then jump to Admin Command Prompt and do a "installuti l
WindowsService2 .exe" and it does the following:

It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstall er.cs". But I was
getting this prior to setting any properties under the
"ProjectInstall er.cs" file. Why do I get this?

The format of the credentials appears to only accept
"machinename\us ername" and nothing else. It took me a while to figure
this out! Arrghhh

After the service is installed, I appear to have two services:

SimpleService2
Hello-World

SimpleService2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.

Executing "InstallUti l -u WindowsService2 .exe" appears to get rid of both
services.

I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService. cs" file??

I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.

How do I get rid of the dialoge all together?

How do I get rid of "SimpleServ ice" installation, since it appears to not
work anyway.

Any help would be appreciated.

Jun 27 '08 #4
Thanks for the quick response, Jeff.

I tried using AdvancedInstall er and it's relatively easy. Of course, only
the professional version supports installing services, but all I have to do
is browse for the .EXE and give it a service name/description and it will
create an MSI. Very cool. I assume if I use this approach, then I can
remove the "Installer" project that was added within VS as that isn't needed
since I won't be using InstallUtil or anything, but maybe I should leave it
for the cases where I want to manually test???

Now, off to another related topic.

What is the best way to debug a Windows service?

I plan on writing a program that scan foldres for XML files, procsess the
XML files, then insert into SQL the information. This will run within a
timer loop every 5 minutes or so.

I was thinking that I should write a console application first and create a
class for my program. This way I can get everything tested and working.

I was then thinking I could create another project for my service program
and add my class from my console project???

I'm not sure how I can actually debug this one I have it all within a
service. I was under the impression that I could install the service, open
VS and attach to the running process after setting my breakpoint and life
would be good. But I do not appear to be able to step thru the code or
anything. I assume I'm doing something wrong here....


"Jeff Winn" <jw***@nospam.c omwrote in message
news:25******** *************** ***********@mic rosoft.com...
Going to do my best to answer the questions - though you seem to have
quite a few. I've put a block of asterisks at the end of my response since
this is going to be quite large after answering everything. If you have
any other questions please feel free to ask. I work with Windows services
a lot. :o)
>Why do I need to rename my project to the service name?
You don't, I do it to keep everything consistent.

>Why do I need to set the "ServiceNam e" property to my service name?
Because Windows uses this when registering the service, and they must be
unique. Also, if we're on the same page, this is the value used as your
service name when writing events to the event log. Though I could be
wrong, I'm not 100% sure which property you're talking about.

>Why do I need to set a property within my code to the service name?
See above.

>Are all these required or am I just doing this for consistency purposes?
They're required. If you provide incorrect information the service will
have problems when registering. Which will usually throw an exception that
you'll be able to see if you're registering the service with InstallUtil
at the command prompt.

>It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstall er.cs". But I was
getting this prior to setting any properties under the
"ProjectInstal ler.cs" file. Why do I get this?
Windows services run under a non-interactive user account on the machines.
Typically when a service is first created in Visual Studio the default
settings require a user account the service will run as. You can change
this by opening your ProjectInstalle r.cs file (double click it to bring up
the designer) and select your processInstalle r component that should be
visible. If you want the service to run as a system account just change
the account to the account type you want. Be cautious about doing this,
the system accounts have unrestricted rights to the machine the service is
running on. The ServiceAccount. User enum requires if the Username and
Password properties on the component aren't set, when the service is being
installed the account information must be prompted. The reason you have to
give the full account (including domain/machine name) is because the
service needs the entire credential when it's starting.

>If I wanted to use a tool such as AdvancedInstall er from
http://www.caphyon.com/, what would I need to do? Would I need any of
these propteries set within the "ProjectInstall erDesigner.cs" file?
Does AdvancedInstall er allow me to browse to the .EXE, add it to my
package, and then allow me to hardcode or prompt the user for what they
want?
What's the best way to do this?
This would be a question better suited to be asked by the software vendor.
I typically just use the Setup project available within Visual Studio for
deploying services.

NOTE: InstallUtil will not be available on your target machines as it is
not included with the .NET Framework. You will need to use an installer
package to get the service registered on the deployment machine. Also,
about that - you will need to use a custom installer action pointed to
your service executable to get the installer package to register your
service. The System.Configur ation.Install.I nstaller class (which is the
base class for the ProjectInstalle r) you made, handles registering the
service with the machine. You can use .NET Reflector if you want to
actually take a look at the code in the class how it uses the Win32 API
and pinvoke if you're interested in looking under the hood. :o)

>After the service is installed, I appear to have two services:

SimpleServic e2
Hello-World

SimpleServic e2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.

Executing "InstallUti l -u WindowsService2 .exe" appears to get rid of both
services.

I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService. cs" file??
This would be a result of not using a consistent service name throughout
the service. My suggestion would be to do a Find All within the IDE and
search for "SimpleService2 " throughout your application and make sure it's
updated to the proper service name.

>I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.

How do I get rid of the dialoge all together?
See the above answer on the project installer.

>How do I get rid of "SimpleServ ice" installation, since it appears to not
work anyway.
Change the service name in your application back to the original value,
register the service again and finally remove it again. This will get the
old service removed from the machine (hopefully) so it won't be listed.

HINT: When working with Windows services I have found it best to create a
batch file to install and uninstall the services into the program files
folder on my machine when testing. When InstallUtil is executed against
the service assembly the configuration in that executable is what is used
during the install/uninstall process. If you build the software before
uninstalling the old version this is what can result.

*************** *************** *************** *************** **************
*************** *************** *************** *************** **************

"dm3281" <dm****@nospam. netwrote in message
news:43******** *************** ***********@mic rosoft.com...
>I'm really starting to hate writing services -- or trying to, anyway.

Why do I need to rename my project to the service name?

Why do I need to set the "ServiceNam e" property to my service name?

Why do I need to set a property within my code to the service name?

Are all these required or am I just doing this for consistency purposes?

Now for my real question/problem:

I have written this service and have this in my "SimpeService.c s":

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceP rocess.ServiceB ase
{
private System.Timers.T imer timer;

/// <summary>
/// Required deisgner variable
/// </summary>
public SimpleService()
{
// This call is required by the Windows.Forms Component
Designer
InitializeCompo nent();

this.timer = new System.Timers.T imer();
this.timer.Enab led = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers. ElapsedEventHan dler(timer_Elap sed);
CanPauseAndCont inue = true;
this.ServiceNam e = "Hello-World Service";

}

void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs
e)
{
EventLog.WriteE ntry("Hello World!");
//throw new Exception("The method or operation is not
implemented.") ;
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
EventLog.WriteE ntry("Hello-World Service Started");
timer.Enabled = true;
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to
stop your service.
EventLog.WriteE ntry("Hello-World Service Paused");
timer.Enabled = false;
}

protected override void OnPause()
{
EventLog.WriteE ntry("Hello-World Service paused");
timer.Enabled = false;
}

protected override void OnContinue()
{
EventLog.WriteE ntry("Hello-World Service continued");
timer.Enabled = true;
}

}
}
This is in my "ProjectInstall er.cs" and I have set the service
properties, etc:

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Configur ation.Install;
using System.ServiceP rocess;

namespace WindowsService2
{
[RunInstaller(tr ue)]
public partial class ProjectInstalle r : Installer
{
private ServiceInstalle r serviceInstalle r;
private ServiceProcessI nstaller processInstalle r;

public ProjectInstalle r()
{
InitializeCompo nent();

processInstalle r = new ServiceProcessI nstaller();
serviceInstalle r = new ServiceInstalle r();

// Service will run under system account
processInstalle r.Account = ServiceAccount. LocalSystem;

// Service will have Start Type of Manual
serviceInstalle r.StartType = ServiceStartMod e.Manual;
serviceInstalle r.ServiceName = "Hello-World Service";
Installers.Add( serviceInstalle r);
Installers.Add( processInstalle r);
}
}
}

My "Program.cs " has no changes:

using System.Collecti ons.Generic;
using System.ServiceP rocess;
using System.Text;

namespace WindowsService2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process.
To add
// another service to this process, change the following line
to
// create a second service object. For example,
//
// ServicesToRun = new ServiceBase[] {new Service1(), new
MySecondUserSe rvice()};
//
ServicesToRun = new ServiceBase[] { new SimpleService() };

ServiceBase.Run (ServicesToRun) ;
}
}
}

Now, when I compiel the above, it all compiles fine.

I then jump to Admin Command Prompt and do a "installuti l
WindowsService 2.exe" and it does the following:

It pops up a dialoge asking me for service credentials. I'm not sure why
because I've defined them within my "ProjectInstall er.cs". But I was
getting this prior to setting any properties under the
"ProjectInstal ler.cs" file. Why do I get this?

The format of the credentials appears to only accept
"machinename\u sername" and nothing else. It took me a while to figure
this out! Arrghhh

After the service is installed, I appear to have two services:

SimpleServic e2
Hello-World

SimpleServic e2 won't start.
Hello-World starts and appears to work as expected. I can pause, etc.
without issue and items are appearing in event log.

Executing "InstallUti l -u WindowsService2 .exe" appears to get rid of both
services.

I assume SimpleService2 is appearing because that is the name that I use
within the "SimpleService. cs" file??

I assume the crendtial dialogue is coming from this service and not
Hello-World because it appears to have the correct credentials.

How do I get rid of the dialoge all together?

How do I get rid of "SimpleServ ice" installation, since it appears to not
work anyway.

Any help would be appreciated.

Jun 27 '08 #5
dm3281 wrote:
I plan on writing a program that scan foldres for XML files, procsess
the XML files, then insert into SQL the information. This will run
within a timer loop every 5 minutes or so.
A far simpler idea may be to use the task scheduler to run your program
every 5 minutes. No service needed.

--
Joel Lucsy
"The dinosaurs became extinct because they didn't have a space program."
-- Larry Niven
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5137
by: belgiozen | last post by:
Hi, I have a working windows service,it is looking for files on the disk and when some of the files are cupdated it calls an executable. But it takes a lot of time(about 10 minutes) to run the executable.So while that executable is running,if I try to stop the windows service while the executable(MYPROGRAM.EXE) is running,it will wait for a time(about 20 seconds) for the main windows thread to abort.If it does not finish its work, the...
7
3925
by: Simon Harvey | last post by:
Hi everyone, I need to make a service that monitors a directory for changes in the files contained within it. I have two questions: 1. I'm going to be using a FileSystemWatcher object to do the monitoring - but do I need to somehow involve another thread to allow the service to do other stuff as well, or is another thread created automatically when the FileSystemMonitor object is created?
5
14122
by: Richard Steele | last post by:
I have created a WinForm application that needs to be run as a windows service (the PC is inaccesible by any user) i have successfully installed the application as a windows service. When i start the service, i get the following error Error 1053: The service did not respond to the start or control request in a timely fashion 1. Is there anyway to extend the timeout period.? I use timed threads to load a serial device driver and this...
1
3454
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm going wrong. The program has been written to do the following tasks: - Select remote server - Select from two specific services
0
3937
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server - Select from two specific services - Check the status of the server
10
3480
by: Ger | last post by:
I am having problems using VB.Net's Management base object on a machine hosting Windows Server 2003. I am trying to set file permissions from a Windows Service. These files may be loacted on a local machine or somewhere on the network. I use the Management Base Object to set these permissions which works perfectly when the windows service is running on an XP machine. However, when the service is running on a Windows Server 2003 machine I...
5
2870
by: Andrew | last post by:
Hey all, Requesting help from the VB.Net gurus in here. I was given a task to write a Windows Service (VB.Net) that would run an external program, and if that program closed for any reason (other than the service being stopped) it would restart it. I have written the service to do just this, and for testing purposes I am running the program Notepad. And I even have it making entries in the System Event Log. However, and this is my...
0
2225
by: Charles Leonard | last post by:
I am having yet another issue with Windows Server 2003. This time, the web service (a file import web service) appears to run except for one odd message: "ActiveX component can't create object". There are no other statements to indicate what object cannot be created. Otherwise, everything on the test Windows Server 2003 works fine—all import data updates correctly. Unfortunately, my normal development environment is not Windows...
2
4492
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as seperate process. If my windows application starts running,only if it completes fully, then my windows services should continue its execution. My main process is Windows service.
0
8817
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9322
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9039
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7966
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.