Problems Converting Console App to Service using TopShelf

  • Thread starter Thread starter RMittelman
  • Start date Start date
R

RMittelman

Guest
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

Continue reading...
 
Back
Top