locked
Problems Converting Console App to Service using TopShelf RRS feed

  • Question

  • I am developing a data access layer using NancyFX in console app.  So far, it's working fine.

    I am using VS 2019 and this is a .Net Core 3.0 application.

    Trying to convert to a Windows Service using TopShelf.  Original code in Main:

    static void Main(string[] args)
    {
        Logger.LogInfo("NancyDataService starting...");
        var uri = new Uri(ConfigurationManager.AppSettings["uri"]);
        var hostConfig = new HostConfiguration();
                
        hostConfig.UrlReservations.CreateAutomatically = true;
        hostConfig.RewriteLocalhost = false;
    
        using (var nancyHost = new NancyHost(uri, new AppBootstrapper(), hostConfig))
        {
            Try
            {
                nancyHost.Start();
                Console.WriteLine($"Nancy now listening on {uri}.\n\nPress any key to exit");
                Logger.LogInfo($"Nancy now listening on {uri}...");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                Console.WriteLine("Error " + ex.Message + "\n\nPress any key to exit");
            }
            Console.ReadKey();
            Logger.LogInfo("NancyDataService stopped...");
        }
    }

    When I open browser and go to http://localhost:8080/whatever, the Nancy endpoint properly services the request, depending on path.

    New code in Main:

    static void Main(string[] args)
    {
        Logger.LogInfo("NancyDataService starting...");
        var rc = HostFactory.Run(x =>
        {
            x.Service<DataService>();
            x.EnableServiceRecovery(r => r.RestartService(TimeSpan.FromSeconds(10)));
            x.SetServiceName("TestService");
            x.StartAutomatically();
        });
        var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
        Environment.ExitCode = exitCode;
    }

    DataService code:

    class DataService : ServiceControl
    {
        public bool Start(HostControl hostControl)
        {
            // start Nancy here
            var uri = new Uri(ConfigurationManager.AppSettings["uri"]);
            var hostConfig = new HostConfiguration();
            hostConfig.UrlReservations.CreateAutomatically = true;
            hostConfig.RewriteLocalhost = false;
            using var nancyHost = new NancyHost(uri, new AppBootstrapper(), hostConfig);
            try
            {
                nancyHost.Start();
                Console.WriteLine($"Nancy now listening on {uri}.\n\nPress any key to exit");
                Logger.LogInfo($"Nancy now listening on {uri}...");
                return true;
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                Console.WriteLine("Error " + ex.Message + "\n\nPress any key to exit");
                return false;
            }
        }
    
        public bool Stop(HostControl hostControl)
        {
            Logger.LogInfo("NancyDataService stopped.");
            return true;
        }
    }

    So now when I run the application, it seems to run ok and start Nancy listening on that port.  Also, logging and console show program has started and is listening on that port.  But when I type the url in browser and hit Enter, I get "The site can't be reached. localhost refused to connect."

    I'm sure I'm missing something, but can't figure out what.

    Any ideas?  Thanks...


    Ron Mittelman


    • Edited by RMittelman Tuesday, July 21, 2020 8:57 PM
    • Moved by CoolDadTx Wednesday, July 22, 2020 2:09 PM Third Party Product
    Tuesday, July 21, 2020 8:56 PM

All replies

  • We don't support third party libraries. Please use the following discussion forum.

    https://groups.google.com/forum/#!forum/topshelf-discuss


    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

    Tuesday, July 21, 2020 10:43 PM