Hi all, here an easy example to make a debug on C# Windows Service ,
Implementation of Code founded on:
https://www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C
With this useful example you create a Single *.exe file , but if you start the debugger
you can't debug the code with Visual Studio, Here is how to do:
First, make all step writed on CodeProject(in future reference I call this project "CodeProjectWS"), after you need to do this:
Right Clik on Current Solution -> Add -> New Project
On Project Type select Windows Consolle Application
select a name for new project, I choose :"LwebCodeWinDbg" -> Ok
Now in New Project "LwebCodeWinDbg" right-click the project name and go to:
[Add]->[Class]. Name the class "LwebCodeWindowsService.cs" and then hit OK.
Open class "LwebCodeWindowsService" and put this code:
namespace LwebCodeWinDbg
{
public class LwebCodeWindowsService
{
public static void WindowsServiceStart()
{
System.IO.File.WriteAllText("c:\\LwebCodeTetsService.txt", DateTime.Now.ToString());
}
}
}
Now in New Project "LwebCodeWinDbg" open Program.cs and paste this code over Main Function:
namespace LwebCodeWinDbg
{
class Program
{
static void Main(string[] args)
{
//put debug breakpoint on this istruction:
LwebCodeWindowsService.WindowsServiceStart();
}
}
}
Now Open "CodeProjectWS" , right Clik on "Reference"-> Add Reference
in left pane select: "Solution" -> Browse... and browse to "LwebCodeWinDbg" Project and then hit OK.
Open WindowsService.cs
and paste this code on OnStart Event:
protected override void OnStart(string[] args)
{
base.OnStart(args);
LwebCodeWinDbg.LwebCodeWindowsService.WindowsServiceStart();
}
with this istruction you can call the same function from Windows Service and Windows Consolle Application.
To debug the Windows service right click on "LwebCodeWinDbg" -> Set as Startup Project
F5 to start debug.
Hope it helps
No comments:
Post a Comment