473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 "ServiceName" 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.cs":

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer;

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

this.timer = new System.Timers.Timer();
this.timer.Enabled = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed);
CanPauseAndContinue = true;
this.ServiceName = "Hello-World Service";

}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("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.WriteEntry("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.WriteEntry("Hello-World Service Paused");
timer.Enabled = false;
}

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService2
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;

public ProjectInstaller()
{
InitializeComponent();

processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();

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

// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Hello-World Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

My "Program.cs" has no changes:

using System.Collections.Generic;
using System.ServiceProcess;
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
MySecondUserService()};
//
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 "installutil
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 "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the "ProjectInstaller.cs"
file. Why do I get this?

The format of the credentials appears to only accept "machinename\username"
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 "InstallUtil -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 "SimpleService" installation, since it appears to not
work anyway.

Any help would be appreciated.


Jun 27 '08 #1
5 3257
All --

I think I made a bit of progress since this post. It would appear the the
"ProjectInstallerDesigner.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 "ProjectInstallerDesigner.cs"

namespace WindowsService2
{
partial class ProjectInstaller
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer 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.Dispose();
}
base.Dispose(disposing);
}

#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 InitializeComponent()
{
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
//this.serviceProcessInstaller1.Password = null;
//this.serviceProcessInstaller1.Username = null;
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "Hello-World2";
this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Manual;
//
// ProjectInstaller
//
this.Installers.AddRange(new
System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});

}

#endregion

private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
}
}

"dm3281" <dm****@nospam.netwrote in message
news:43**********************************@microsof t.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 "ServiceName" 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.cs":

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer;

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

this.timer = new System.Timers.Timer();
this.timer.Enabled = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed);
CanPauseAndContinue = true;
this.ServiceName = "Hello-World Service";

}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("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.WriteEntry("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.WriteEntry("Hello-World Service Paused");
timer.Enabled = false;
}

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService2
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;

public ProjectInstaller()
{
InitializeComponent();

processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();

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

// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Hello-World Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

My "Program.cs" has no changes:

using System.Collections.Generic;
using System.ServiceProcess;
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
MySecondUserService()};
//
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 "installutil
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 "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the
"ProjectInstaller.cs" file. Why do I get this?

The format of the credentials appears to only accept
"machinename\username" 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 "InstallUtil -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 "SimpleService" 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 AdvancedInstaller from
http://www.caphyon.com/, what would I need to do? Would I need any of these
propteries set within the "ProjectInstallerDesigner.cs" file?

Does AdvancedInstaller 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 "ServiceName" 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 "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the
"ProjectInstaller.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 ProjectInstaller.cs file (double click it to bring up the
designer) and select your processInstaller 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 AdvancedInstaller from
http://www.caphyon.com/, what would I need to do? Would I need any of
these propteries set within the "ProjectInstallerDesigner.cs" file?
Does AdvancedInstaller 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.Configuration.Install.Installer class (which is the base class for
the ProjectInstaller) 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 "InstallUtil -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 "SimpleService" 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**********************************@microsof t.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 "ServiceName" 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.cs":

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer;

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

this.timer = new System.Timers.Timer();
this.timer.Enabled = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed);
CanPauseAndContinue = true;
this.ServiceName = "Hello-World Service";

}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("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.WriteEntry("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.WriteEntry("Hello-World Service Paused");
timer.Enabled = false;
}

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService2
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;

public ProjectInstaller()
{
InitializeComponent();

processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();

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

// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Hello-World Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

My "Program.cs" has no changes:

using System.Collections.Generic;
using System.ServiceProcess;
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
MySecondUserService()};
//
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 "installutil
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 "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the
"ProjectInstaller.cs" file. Why do I get this?

The format of the credentials appears to only accept
"machinename\username" 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 "InstallUtil -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 "SimpleService" 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 AdvancedInstaller 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.comwrote in message
news:25**********************************@microsof t.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 "ServiceName" 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 "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the
"ProjectInstaller.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 ProjectInstaller.cs file (double click it to bring up
the designer) and select your processInstaller 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 AdvancedInstaller from
http://www.caphyon.com/, what would I need to do? Would I need any of
these propteries set within the "ProjectInstallerDesigner.cs" file?
Does AdvancedInstaller 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.Configuration.Install.Installer class (which is the
base class for the ProjectInstaller) 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 "InstallUtil -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 "SimpleService" 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**********************************@microsof t.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 "ServiceName" 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.cs":

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace WindowsService2
{
public partial class SimpleService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer;

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

this.timer = new System.Timers.Timer();
this.timer.Enabled = true;
timer.Interval = 5000;
timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed );
CanPauseAndContinue = true;
this.ServiceName = "Hello-World Service";

}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
EventLog.WriteEntry("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.WriteEntry("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.WriteEntry("Hello-World Service Paused");
timer.Enabled = false;
}

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService2
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;

public ProjectInstaller()
{
InitializeComponent();

processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();

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

// Service will have Start Type of Manual
serviceInstaller.StartType = ServiceStartMode.Manual;
serviceInstaller.ServiceName = "Hello-World Service";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

My "Program.cs" has no changes:

using System.Collections.Generic;
using System.ServiceProcess;
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
MySecondUserService()};
//
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 "installutil
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 "ProjectInstaller.cs". But I was
getting this prior to setting any properties under the
"ProjectInstaller.cs" file. Why do I get this?

The format of the credentials appears to only accept
"machinename\username" 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 "InstallUtil -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 "SimpleService" 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
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...
7
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...
5
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...
1
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...
0
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 -...
10
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...
5
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...
0
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". ...
2
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.