Wednesday, August 31, 2016

How to Debug the Window service in Dotnet both C# and VB.Net

There Two ways you can debug the service

 First :

 Go to Service.Designer.vb page find below mentioned code and comment mentioned places, later create new instance of the class and call the main method check below code.
 --- VB.NET
 ' The main entry point for the process
    <MTAThread()> _
    <System.Diagnostics.DebuggerNonUserCode()> _
    Shared Sub Main()
        Comment/Uncomment --'Dim ServicesToRun() As System.ServiceProcess.ServiceBase

        ' More than one NT 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 System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
        '
        Comment/Uncomment --'ServicesToRun = New System.ServiceProcess.ServiceBase() {New UniDocBulkImportService}

        Comment/Uncomment --'System.ServiceProcess.ServiceBase.Run(ServicesToRun)

Dim objClsMain as New ClsMain() -- Create new instance for your main class in your service
objClsMain.MainMethod()         --- Call the Main method in above class where service start processing

    End Sub

 --- C#.NET
Go to Program.cs page find below mentioned code and comment mentioned places, later create new instance of the class and call the main method check below code.
static void Main()
        {
           Comment/Uncomment // ServiceBase[] ServicesToRun;
           Comment/Uncomment // ServicesToRun = new ServiceBase[]
            Comment/Uncomment //{
            Comment/Uncomment //    new Unidoc_CHAD_IN_Service()
           Comment/Uncomment // };
           Comment/Uncomment // ServiceBase.Run(ServicesToRun);
         clsMain ObjClsMain =new clsMain(); -- Create new instance for your main class in your service
ObjClsMain.MainMethod();  --- Call the Main method in above class where service start processing
         
        }
Second :

 Add new windows form to your service project right click on project and go to properties
 under ApplicationType ---> select WindowsFormApplication
 and UnderStartup project select Form1 so directlry debugger starts from Form1.
 same C# also.

No comments:

Post a Comment