【问题标题】:Console application with Topshelf throws an exception when running as a service带有 Topshelf 的控制台应用程序在作为服务运行时引发异常
【发布时间】:2019-08-27 16:26:27
【问题描述】:

我开发了一个控制台应用程序,它连接到 RabbitMQ 然后处理一些消息。我正在使用 Topshelf 允许作为服务执行而没有(在纸上)问题,但是当我将它作为服务运行时,出现以下异常

应用程序:XXX.exe 框架 版本:v4.0.30319 描述:进程因 未处理的异常。异常信息:System.IO.FileNotFoundException 在 Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(布尔值) 在 Microsoft.Extensions.Configuration.FileConfigurationProvider.Load() 在 Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(System.Collections.Generic.IList`1) 在 Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() 在 XXX.Program.Main(System.String[])

现在我认为它以某种方式丢失了配置文件或一些 dll,但我将所有文件都放在了 c:\services\myservice 上的文件夹中。如果我从命令提示符运行 exe,它可以完美运行。

我还尝试将身份设置为网络服务/管理员用户和类似用户......但没有运气。我正在使用 TopShelf 4.0.4

有什么建议吗? 谢谢

解决方案

这是我的错……

我不得不使用

var builder = new ConfigurationBuilder()
            .SetBasePath(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
            .AddJsonFile("appsettings.json")
            .AddJsonFile("appsettings.{environmentName}.json",true,true);

而不是

 var builder = new ConfigurationBuilder() 
 .SetBasePath(Directory.GetCurrentDirectory()) //(which is the default code present on MS site)
 .AddJsonFile("appsettings.json")
 .AddJsonFile("appsettings.{environmentName}.json",true,true);

【问题讨论】:

  • 您的update 解决了我的问题。也许你应该回答你自己的问题,我可以投票赞成。

标签: topshelf


【解决方案1】:

正如@bozoJoe 所建议的,这是我的问题的答案,请支持

解决方案 这是我的错...

我不得不使用

var builder = new ConfigurationBuilder()
        .SetBasePath(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
        .AddJsonFile("appsettings.json")
        .AddJsonFile("appsettings.{environmentName}.json",true,true);

而不是

var builder = new ConfigurationBuilder() 
.SetBasePath(Directory.GetCurrentDirectory()) //(which is the default code present on MS site)
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.{environmentName}.json",true,true);

【讨论】:

  • 非常感谢。启动服务时,我立即收到错误“服务没有及时响应启动或控制请求”,但这是对我有用的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 2020-10-09
相关资源
最近更新 更多