【问题标题】:Base Class for Windows Service for databses数据库的 Windows 服务的基类
【发布时间】: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


【解决方案1】:

你的问题在这里:

public class Version_10 : ServiceBase
{
    Version_10 set = new Version_10(); // <-- Recursive call on object construct

它与服务或任何东西无关。您的代码有一个触发 StackOverflow 异常的递归调用。

更新:

要解决您的问题,请将您的 Version_10 类更改为:

public class Version_10 : ServiceBase
{
    public void Start(string[] args)
    {
        this.OnStart(args);
    }
}

【讨论】:

  • 好的,谢谢,我在其他教程中看到过,但可能不适合我的情况。
  • 这可能是一个愚蠢的问题,但是这个服务连接到一个数据库,所以当我运行调试时,什么都没有发生,控制台行永远不会像我想象的那样出现在 Program.cs 中跨度>
【解决方案2】:

这部分是递归的,这就是你得到 System.StackOverflowException 的原因。

public class Version_10 : ServiceBase
{
    **Version_10 set = new Version_10();**


    public void Start(string[] args)
    {
        set.OnStart(args);
    }
 }

也许你应该查看一些文章:

A basic Windows service in C#

【讨论】:

    【解决方案3】:

    因此,您会收到 Stack Overflow 异常:

    public class Version_10 : ServiceBase
    {
        Version_10 set = new Version_10();
    }
    

    当您创建Version_10 的实例时,它会创建Version_10 的实例,该实例创建Version_10 的实例,该实例创建Version_10 的实例,该实例创建Version_10 的实例,该实例创建@ 的实例987654327@等……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      相关资源
      最近更新 更多