Loading...

Debugging Windows Services under Visual Studio .NET

View: 930    Dowload: 0   Comment: 0   Post by: hanhga   Category: Visual studio & .NET   Fields: Computers - Technology

// The main entry point for the process
static void Main()
{
    System.ServiceProcess.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 
    //      System.ServiceProcess.ServiceBase[] {new Service1(), 
    //      new MySecondUserService()};
    //

    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

Obviously, it's the Main() above that ends up executing the service, and it's the Main() that this approach manipulates so that the Windows Service can be debugged directly within Visual Studio .NET.

Using the example above (and removing some of the comments), here's how:

// The main entry point for the process
static void Main()
{
#if (!DEBUG)
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#else
    // Debug code: this allows the process to run as a non-service.
    // It will kick off the service start point, but never kill it.
    // Shut down the debugger to exit
    Service1 service = new Service1();
    service.<Your Service's Primary Method Here>();
    // Put a breakpoint on the following line to always catch
    // your service when it has finished its work
    System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif 
}

It's crude, but effective (CBE - also known as Commander of the British Empire Wink | ;) ). Run the service in debug mode to debug it, compile and install it as a release build, and it's a full and proper Windows service.

You may still wish to pull the guts out of your service into a separate library for unit testing. But this approach allows you to work with almost all of your service code as an actual service.

Debugging Windows Services under Visual Studio .NET

Normally, debugging a Windows service under Visual Studio .NET is painful. Windows services won't actually run directly within Visual Studio .NET, so the usual technique is to install and start the Windows service and then attach a debugger to it. An alternative approach is to pull the guts out of the service, stick it in a separate library, and then build some other app (e.g., a console app) to sit in front of it. This approach uses neither of those techniques. When building a C# Windows Service project in Visual Studio, it will leave you with a class containing quite a few methods including a Main(), such as this:

Posted on 01-04-2014 

Comment:

To comment you must be logged in members.

Files with category

  • Pharmacy/Chemist POS and Invoicing Software

    Pharmacy/Chemist POS and Invoicing Software

    View: 38    Download: 5   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Pharmacy POS /Chemist Management System allows User to sell medicine , Over the counter or at any Hospital, Easily Integrated with Email to SEND invoices and Print Receipt. This is one of my very best Project i Decided to Share with you Guys ...

  • Computer-Based Inventory Management System in VB.net

    Computer-Based Inventory Management System in VB.net

    View: 30    Download: 6   Comment: 0

    Category: Visual studio & .NET     Fields: none

    This is a project entitled Computer-Based Inventory Management System written in Vb.net and Microsoft access database for Surigao Doctors College. The system has the following features: - Cataloging - Inventory - Circulation - Borrow - Return - View...

  • Salary Management System

    Salary Management System

    View: 97    Download: 11   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Introduction: ----------------- *This project is based on accounts concept. *Salary Management System is a DataBase system which can be used for managing employee Salary Details. *It is a multi-user system and can be used by hundreds of user at the...

  • New Print-maker Cyber Cafe

    New Print-maker Cyber Cafe

    View: 41    Download: 0   Comment: 0

    Category: Visual studio & .NET     Fields: none

    New Printmaker Cyber Cafe ------------------------- Before you run this project, please set the following date format “dd/MM/yyyy” to your computer. Introduction: -------------- The purpose of developing this software project is to fully automate...

  • Folder Locker 7.0

    Folder Locker 7.0

    View: 128    Download: 2   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Folder Lock is great security app that lets you password-protect files, folders and drives; encrypt your important files on-the-fly, backup files in real-time, protect portable drives, shred files & drives and clean history. Folder Lock can...

  • Bakeshop Inventory System in VB.Net and MS Access Database

    Bakeshop Inventory System in VB.Net and MS Access Database

    View: 127    Download: 7   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Bakeshop Inventory System is a software based system especially designed for a Bakeshop business and for the inventory process. This system will be able to track the daily, weekly, monthly and yearly inventory of products. In this way, the staff can...

  • Bezier Curve Machine Learning Demonstration

    Bezier Curve Machine Learning Demonstration

    View: 35    Download: 0   Comment: 0

    Category: Visual studio & .NET     Fields: Other

    I enjoy working with longitudinal data. This is the second article in a series about using Bezier curves to smooth large data point fluctuations and improve the visibility of the patterns unfolding. This current article focuses on using machine...

  • Basic Library Management System

    Basic Library Management System

    View: 92    Download: 1   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Library System is designed to store students (borrowers) info and their individual file in an institution regarding on the library transactions. This system is a user-friendly program able to print reports, view students’ file or transactions in the...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd