user3825831
user3825831

Reputation: 109

Base Class for Windows Service for databses

I'm trying to create a base class for a windows service so I can change as little as possible when deploying to different databases. I have this but there is an unhandled exception:

"An unhandled exception of type 'System.StackOverflowException' occurred in >Microsoft.VisualStudio.HostingProcess.Utilities.dll",

I'm new to services so I could totally be off on this, so far this is what I have:

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);
    }
 }

This is my 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);

   }
}

As usually I have other methods that I call but I figured it would just be a waste of space. Am I totally off on base classes?

Upvotes: 0

Views: 270

Answers (3)

sergiogarciadev
sergiogarciadev

Reputation: 2202

You problem lies here:

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

It has nothing to do with services or anything. Your code has a recursive call with trigger the StackOverflow exception.

UPDATE:

To solve your problem, change your Version_10 class to:

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

Upvotes: 1

DavidG
DavidG

Reputation: 119017

You are getting a Stack Overflow exception because of this:

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

When you create an instance of Version_10 it creates an instance of Version_10 which creates an instance of Version_10 which creates an instance of Version_10 which creates an instance of Version_10 which creates an instance of Version_10 etc....

Upvotes: 0

Bura Chuhadar
Bura Chuhadar

Reputation: 3751

This part is recursive that's why you are getting a System.StackOverflowException.

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


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

Maybe you should check some articles:

A basic Windows service in C#

Upvotes: 0

Related Questions