作为服务运行时,带有 Topshelf 的控制台应用程序抛出异常

adv*_*api 5 topshelf

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

应用程序:XXX.exe 框架版本:v4.0.30319 描述:由于未处理的异常,进程被终止。异常信息: Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean) 处的 System.IO.FileNotFoundException 在 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);
Run Code Online (Sandbox Code Playgroud)

代替

 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);
Run Code Online (Sandbox Code Playgroud)

adv*_*api 2

正如 @bozoJoe 所建议的,这是我的问题的答案,用于投票

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

我不得不使用

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

代替

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);
Run Code Online (Sandbox Code Playgroud)