One of the simplest ways to debug Windows Services

Source: Internet
Author: User

Let's take a look at the startup code:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. serviceprocess;
Using system. text;

Namespace windowsservice1
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
Static void main ()
{
Service1 S = new service1 ();

If (environment. userinteractive)
{
S. debugstart ();

Console. readkey ();

S. debugstop ();
}
Else
{
Servicebase [] servicestorun;
Servicestorun = new servicebase [] {s };
Servicebase. Run (servicestorun );
}
}
}
}

The key lies in the judgment of environment. userinteractive,

Please refer to the above explanation on msdn:

Gets a value that indicates whether the current process is running in user interaction mode.

If the userinteractive attribute is a Windows process or a service (such as IIS) that does not have a user interface at runtime, false is reported. If this attribute is false, do not display the mode dialog box or message box, because there is no graphic user interface used to interact with users.

Http://msdn.microsoft.com/zh-cn/library/system.environment.userinteractive (V = vs.100). aspx

Then let's take a look at the code in service1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
this.ServiceStart();
}

protected override void OnStop()
{
this.ServiceStop();
}

internal void DebugStart()
{
this.ServiceStart();
}

internal void DebugStop()
{
this.ServiceStop();
}

private void ServiceStart()
{
// TODO:

}

private void ServiceStop()
{
// TODO:
}



}
}

Finally, change the output type of the project.

Right-click the project and click Properties. In the application tag, set output type to console application.

OK. Press F5 to try.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.