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.

Difference between Abstract Class and Interface

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. it allows other classes to inherit from it. In simple words, if you inherit this class need to implement methods in this class.
Ex: Like in your project you have all the EDIT,DELETE,UPDATE and SELECT methods, if you want maintain to same hierarchies for all the classes then place these methods in abstract class and inherit this class in all your modules.
Real life example of Abstract : Mobile,TV,Watch and human body...... etc.


What is an Interface?

An interface is not a class. An interface has no implementation; Method without body, As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses.


Difference between them is
that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Abstract class  : static methods, main method and constructor.

Interface: no static methods, main method or constructor.