【问题标题】:Topshelf: Issues using NancyFX with Topshelf 4.2.1Topshelf:在 Topshelf 4.2.1 中使用 NancyFX 的问题
【发布时间】:2020-07-25 02:04:26
【问题描述】:

我正在尝试使用 Visual Studio 2019、.Net Core 3.0 开发数据访问服务。我正在使用 NancyFX 来处理 http 请求。这作为控制台应用程序工作得很好。当我构建并运行时,然后在浏览器中转到 HTTP://localhost/,它会返回正确的数据。我有一个工作 Nancy 模块来处理请求。这是原始的主要代码:

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

现在我想让它成为 Windows 服务。第一次尝试是使用 Topshelf。以下 Main 代码基本取自 Topshelf 文档和其他关于 Topshelf 的文章。

static void Main(string[] args)
{
    Logger.LogInfo("NancyDataService starting...");
    var rc = HostFactory.Run(x =>
    {
        x.Service<DataService>(s =>
        {
            s.ConstructUsing(name => new DataService());
            s.WhenStarted(tc => tc.Start());
            s.WhenStopped(tc => tc.Stop());
        });

        x.RunAsLocalSystem();
        x.StartAutomatically();
        x.EnableServiceRecovery(r => r.RestartService(TimeSpan.FromSeconds(10)));
        x.SetServiceName("NancyDataService");
    });
    var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());  //11
    Environment.ExitCode = exitCode;
}

这是我的 DataService 类,基本上是根据 Topshelf 文档和我找到的几篇文章构建的:

class DataService
{

    public DataService()
    {
    }

    private SemaphoreSlim _semaphoreToRequestStop;
    private Thread _thread;

    public void Start()
    {
        
        // 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}...");
            Logger.LogInfo($"Nancy now listening on {uri}...");

            // spin thread here
            _semaphoreToRequestStop = new SemaphoreSlim(0);
            _thread = new Thread(DoWork);
            _thread.Start();

        }
        catch (Exception ex)
        {
            Logger.LogError(ex.Message);
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    private void DoWork(object obj)
    {
        while (true)
        {
            Console.WriteLine("doing work..");
            if (_semaphoreToRequestStop.Wait(500))
            {
                Console.WriteLine("Stopped");
                break;
            }
        }
    }

    public void Stop()
    {
        Logger.LogInfo("NancyDataService stopping...");
        _semaphoreToRequestStop.Release();
        _thread.Join();
        //return true;
    }

}

所以现在当我在设计模式下运行项目时(Topshelf 说你应该能够这样做),它似乎开始正常并且 Nancy 似乎正在侦听正确的端口。但是,当我转到浏览器并输入 HTTP//localhost:8080/ 时,我得到“无法访问此站点。localhost 拒绝连接”

我有最新版本的 Topshelf (4.2.1) 和 Topshelf.Log4Net 包。

任何人都可以对此有所了解吗?谢谢...

【问题讨论】:

    标签: nancy topshelf


    【解决方案1】:

    解决了这个问题。结果是我的 NancyHost 对象的范围不正确。现在工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多