locked
TopShell Service deployed just showing starting RRS feed

  • Question

  • Hallo

    Hope somebody can assist, think I'm doing something wrong, followed the guidance on how to deploy a console application as a service to using TopShelf, but sure I'm doing something wrong, when running the console application with the exe it runs fine and without error, but when installing as a service it just shows Starting, but it's actually running and I got no control to Start,Stop,Pause or Reset. The actual program is another class, so the service is only controller a sequencer. (Which in returns call all the other part of the program.

    Prgram section

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Topshelf;
    
    namespace Adidas_Service
    {
        class Program
        {
            static void Main(string[] args)
            {
                var exitCode = HostFactory.Run(x =>
                {
                    x.Service<Sequencer>(s =>
                    {
                        s.ConstructUsing(sequencer => new Sequencer());
                        s.WhenStarted(sequencer => sequencer.Start());
                        s.WhenStopped(sequencer => sequencer.Stop());
                    });
    
                    x.RunAsLocalSystem();
                    x.SetServiceName("AdidasWCS");
                    x.SetDisplayName("Adidas WCS Service");
                    x.SetDescription("This is managing the PTL Wall between PLC and Databases");
                });
    
                int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
                Environment.ExitCode = exitCodeValue;
            }
        }
    }

    Sequencer section

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using WCS_Shared.MainHandlerClasses.Logic;
    using WCS_Shared.MainHandlerClasses.Symbols;
    using WCS_Shared.SubHandlerClasses.Logic;
    using WCS_Shared;
    using System.Threading.Tasks;
    using System.ServiceProcess;
    
    namespace Adidas_Service
    {
        public class Sequencer
        {
            public Sequencer()
            {
    
            }
    
            public void Start()
            {
                var newHuNrTriggerCycle = new NewHuNrTriggeredHandler();
                var newHuTriggerHandlerSymbols = new NewHuNrTriggeredHandlersSymbols();
                var monitorSequencerParameters = new MainSequencerParameterHandlerSymbols();
                var getParameterSettings = new MainSequencerParametersHandler();
                var sequencerEnabled = 1;
                int sequenceCycleTimer = 0;
                int counter = 1;
                
                while (true)
                {
                    monitorSequencerParameters = getParameterSettings.GetSequencerSettingParameters();
                    sequencerEnabled = monitorSequencerParameters.sequencerEnabled;
    
                    Thread.Sleep(2000);
                    sequencerEnabled = monitorSequencerParameters.sequencerEnabled;
    
                    while (sequencerEnabled > 0)
                    {
                        monitorSequencerParameters = getParameterSettings.GetSequencerSettingParameters();
                        sequenceCycleTimer = monitorSequencerParameters.applicationCycleSpeed;
                         SequencerRunner();
                         Thread.Sleep(sequenceCycleTimer);
                        sequencerEnabled = monitorSequencerParameters.sequencerEnabled;
                    }
                    Console.WriteLine("Sequencer Disabled");
                }   
    
            }
            public void Stop()
            {
    
            }
    
            private void SequencerRunner()
            {
                var sequencer = new MainSequencerHandler();
                var sequencerSymbols = new MainSequencerHandlerSymbols();
                int cyclecounter = 1;
    
                while (cyclecounter != 0)
                {
                    sequencerSymbols = sequencer.MainSequencerHandlerSequencer();
                    cyclecounter = sequencerSymbols.sequencer;
                }
               
            }
        }
    }
    


    labjac

    • Moved by CoolDadTx Thursday, July 16, 2020 3:41 PM Third Party Product
    Wednesday, July 15, 2020 9:42 PM

All replies

  • Hi labjac,

    Thank you for posting here.

    Topshelf is a third-party product, and you can ask questions about Topshelf in their Github repository.

    For creating Windows Service, you can look at this document provided by Microsoft.

    Tutorial: Create a Windows service app

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 16, 2020 2:54 AM
  • Hello,

    If the problem can be repeated on your computer than use Visual Studio remote debugger. You would need to place the following in the start event

    Thread.Sleep(10000);
    #if DEBUG
      Debugger.Launch();
    #endif

    See also

    https://social.technet.microsoft.com/wiki/contents/articles/53573.visual-studio-debugging-windows-services.aspx


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 16, 2020 10:01 AM