How to debug a service ?

C

cyshao

How to debug a service ?

Hi, friends:

I created a new C# service project and wrote some log in OnStart()
function.
But when I type F5 to debug it, system prompt me
"Can't debug a service program by debugger. Please use installutil.exe
to install it."

So, I tried to use " installutil MyPorgram.exe". But there is an error
written in logfile.
I don't know what does "public installers " mean ? What shall I do to
solve that?

Thanks

Charles Shao :)

------------------------------Log File -----------------------------------

No public installers with the RunInstallerAttribute.Yes attribute could be
found in the
d:\project\directoryservices\directoryservices\agent\bin\deubgunittests\asti
..bos.directoryservices.agent.exe assembly.
Remove InstallState file because there are no installers.
 
A

Adrian

Use the code below in a new .cs file and then use installutil.

using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;
[RunInstallerAttribute(true)]
public class ProjectInstaller: Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public ProjectInstaller()
{
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.Automatic;
serviceInstaller.ServiceName = "svchost";
Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}

Best regards.
 
H

Hans Kesting

cyshao said:
How to debug a service ?

Hi, friends:

I created a new C# service project and wrote some log in OnStart()
function.
But when I type F5 to debug it, system prompt me
"Can't debug a service program by debugger. Please use installutil.exe
to install it."

So, I tried to use " installutil MyPorgram.exe". But there is an error
written in logfile.
I don't know what does "public installers " mean ? What shall I do to
solve that?

Thanks

Charles Shao :)

------------------------------Log File -----------------------------------

No public installers with the RunInstallerAttribute.Yes attribute could be
found in the
d:\project\directoryservices\directoryservices\agent\bin\deubgunittests\asti
.bos.directoryservices.agent.exe assembly.
Remove InstallState file because there are no installers.

1) when you run InstallUtil it will search for a class that is marked
with the [RunInstaller(true)] attribute. This will do the actual
installing. It needs to derive from
System.Configuration.Install.Installer. I believe MSDN has
examples on how you can make this class.

2) for debugging: I don't think you can debug a service. What you
*can* do however: put all the logic into a different project
and have a third project with a winform project, where you can call
the various "worker methods" from the logic-project.
The "service" project has only the basic service-hooks.
 
A

Adrian

To debag a service, you could try this:
comment the line with

System.ServiceProcess.ServiceBase.Run(new Service1());

Then add

Service1 s = new Service1();
s.Method1();
....
....

The project will be transformed in a console application.

Hope that helps.
 
D

Dale Preston

To debug a service, after all the things already discussed have been done to
get your service installed, open the service project and open a client or
service consumer project. Attach the debugger to the consumer project and
set a break point in your service.

HTH

Dale Preston
MCAD, MCDBA, MCSE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top