Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Windows Service stopping immediately
Message
De
01/09/2013 19:03:01
 
 
À
01/09/2013 15:12:32
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01581778
Message ID:
01581804
Vues:
63
This message has been marked as the solution to the initial question of the thread.
We don't need to recompile in order to run the service as a Console app for debugging purposes. Our project is a Console app project and here's what my code looks like:
static class Program
{
	static MyService oService;

	static void Main()
	{
		if (Environment.UserInteractive == false)
		{
			ServiceBase[] ServicesToRun;
			ServicesToRun = new ServiceBase[] 
			{ 
				new MyService(),
			};
			ServiceBase.Run(ServicesToRun);
		}
		else
		{
			if (ConfigurationManager.AppSettings.Count > 0)
			{
				oService = new MyService();
				oService.Start();
				Console.ReadLine();
				oService.Stop();
			}
			else
			{
				Console.WriteLine("Config file is missing ...");
				Console.ReadLine();
			}
		}
	}
}
And then MyService looks like this:
public partial class MyService : ServiceBase
{
	public MyService()
	{
		this.CanStop = true;
		this.CanShutdown = true;
		this.CanPauseAndContinue = false;
	}
	protected override void OnStart(string[] args)
	{
		// startup code here
		//Console.WriteLine("Done With OnStart");
	}
	protected override void OnStop()
	{
		// stop code here
	}
	protected override void OnShutdown()
	{
		// TODO: There may be a bug in the .NET Framework in that this method
		// is not being called on system shutdown for SYSTEM account services.
		// Not sure when/if it will be fixed, but I'm putting code here to stop
		// stuff on the off-chance that it will be called eventually.

		// stop code  here
	}

	// These 2 methods are only used when not runnning as a Service, for testing from a Console window.
	public void Start()
	{
		this.OnStart(null);
	}
	public void Stop()
	{
		this.OnStop();
	}
}
Then all you have to do is run the EXE normally and it will be like you're running the service, only it will be a Console app.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform