【发布时间】:2014-07-17 16:40:20
【问题描述】:
我正在尝试为 Windows 服务创建一个基类,以便在部署到不同的数据库时尽可能少地进行更改。我有这个但是有一个未处理的异常:
“在 >Microsoft.VisualStudio.HostingProcess.Utilities.dll 中发生了“System.StackOverflowException”类型的未处理异常”,
我是服务新手,所以我完全可以不做这个,到目前为止,这就是我所拥有的:
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 服务框架为您实例化它....
标签: c# base-class