我正在嘗試爲Windows服務創建基類,以便在部署到不同數據庫時儘可能少地進行更改。我有這個,但有一個未處理的異常:用於數據庫的Windows服務的基類
「類型的未處理的異常‘System.StackOverflowException’發生在> Microsoft.VisualStudio.HostingProcess.Utilities.dll」,
我新的服務,所以我可以完全離開這個,到目前爲止,這是我所:
public partial class Service1 : ServiceBase
{
namespace SecureVoiceBase
{
public Service1()
{
try
{
InitializeComponent();
}
catch (Exception e)
{
EventLog.WriteEntry(e.Message);
}
}
protected override void OnStart(string[] args)
{
//code here
}
//OnStop, Timers as well...
}
}
public class Version_10 : ServiceBase// Derived class, This is where I will call
{
//certain methods depending on which database I will use
Version_10 set = new Version_10();
public void Start(string[] args)
{
set.OnStart(args);
}
}
這是我的Program.cs:
namespace testservice
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(params string[] args)
{
var service = new Version_10();
if (!Environment.UserInteractive)
{
var servicesToRun = new ServiceBase[] { service };
ServiceBase.Run(servicesToRun);
return;
}
Console.WriteLine("Running as a Console Application");
Console.WriteLine(" 1. Run Service");
Console.WriteLine(" 2. Other Option");
Console.WriteLine(" 3. Exit");
Console.Write("Enter Option: ");
var input = Console.ReadLine();
switch (input)
{
case "1":
service.Start(args);
Console.WriteLine("Running Service - Press Enter To Exit");
Console.ReadLine();
break;
case "2":
// TODO!
break;
}
Console.WriteLine("Closing");
}
// ServiceBase[] ServicesToRun;
// ServicesToRun = new ServiceBase[]
// {
// new Version_10()
// };
// ServiceBase.Run(ServicesToRun);
}
}
通常我有其他方法,我打電話,但我認爲這隻會浪費空間。我完全脫離基礎班嗎?
爲什麼Version_10你實例化服務的實例? Windows服務框架爲你實例化... –