asp.net Core 2 Web API appsetting.json 部署为 windows 服务时未读取值

asp.net Core 2 Web API appsetting.json values aren't being read when deployed as windows service

我正在 运行将 asp.net 核心 2 网络 api 作为 windows 服务。当我通过 IIS 调试时,我能够毫无问题地读取我的配置值。

但是,一旦我 运行 它作为一项服务,我就没有得到任何值。

appsettings.json

{
  "Database": {
    "DatabaseName": "testdb",
    "DatabaseServer": "localhost",
    "DatabaseUserName": "admin",
    "DatabasePassword": "admin"
  }
}

 public string GetConnectionString()
    {
        var databaseName = Configuration["Database:DatabaseName"];
        var databaseServer = Configuration["Database:DatabaseServer"];
        var username = Configuration["Database:DatabaseUserName"];
        var password = Configuration["Database:DatabasePassword"];


        return $"Data Source={databaseServer};Initial Catalog={databaseName};Persist Security Info=True;User ID={username};Password={password};MultipleActiveResultSets=True";
    }

不确定为什么我在发布应用程序后无法读取这些值。在已发布的 appsettings.json 文件中,值在那里。

这是我的 startup.cs。我的印象是我不必在新的 asp.net 核心 2 中引用 appSettings.json 文件。感谢您的帮助。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        AutoMapper.Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<InventoryTransaction, Models.InventoryTransactionModel>();
            cfg.CreateMap<ReasonCode, Models.ReasonCodeModel>();
            cfg.CreateMap<InventoryTransaction, Models.InventoryTransactionForCreationModel>();
            cfg.CreateMap<InventoryTransactionForCreationModel, InventoryTransaction>();
        });
        services.AddScoped<IInventoryTransactionRepository, InventoryTransactionRepository>();
        services.AddSingleton<IConfiguration>(Configuration);
        services.AddDbContext<VPSInventoryContext>(options => options.UseSqlServer(GetConnectionString()));

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();
        loggerFactory.AddDebug();
        loggerFactory.AddNLog();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();

    }

    public string GetConnectionString()
    {
        var databaseName = Configuration["Database:DatabaseName"];
        var databaseServer = Configuration["Database:DatabaseServer"];
        var username = Configuration["Database:DatabaseUserName"];
        var password = Configuration["Database:DatabasePassword"];


        return $"Data Source={databaseServer};Initial Catalog={databaseName};Persist Security Info=True;User ID={username};Password={password};MultipleActiveResultSets=True";
    }
}

这是我的 Program.cs,我 运行 将其作为 windows 服务。

public class Program
{
    public static void Main(string[] args)
    {
        if (Debugger.IsAttached || args.Contains("--debug"))
        {
            BuildWebHost(args).Run();
        }
        else
        {
            BuildWebHost(args).RunAsService();
        }
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

当您将应用程序作为服务启动时,工作目录设置为系统目录,如 C:\Windows\System32\。配置文件在同一目录中查找。至于那里缺少它,您会加载空配置。

要修复它,只需将工作目录设置为您的应用程序所在的目录:

public static void Main(string[] args)
{
    if (Debugger.IsAttached || args.Contains("--debug"))
    {
        BuildWebHost(args).Run();
    }
    else
    {
        var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        Directory.SetCurrentDirectory(path);
        BuildWebHost(args).RunAsService();
    }
}