1

I am running an asp.net core 2 web api as a windows service. When I debug through IIS I am able to read my configuration values no problem.

However, once I run it as a service I am not getting any values back.

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

Not sure why I can't read those values once I publish the application. In the published appsettings.json file the values are there.

Here is my startup.cs. I was under the impression that I didn't have to reference teh appSettings.json file in the new asp.net core 2. Thanks for the help.

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

Here is my Program.cs where I run it as a windows service.

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();
}
6
  • Please share the code of loading configuration. Do you use AddJsonFile extension call? Commented Dec 14, 2017 at 11:36
  • I do not, I use the default method. Commented Dec 14, 2017 at 15:01
  • Are you using Microsoft.AspNetCore.Hosting.WindowsServices NuGet for RunAsService() call? How did you manage to use it with .Net Core application? It's currently supported for .Net Framework only (github.com/aspnet/Hosting/issues/1173). I've tried to use it with asp.net core app and getting Unhandled Exception: System.TypeLoadException: Could not load type 'System.ServiceProcess.ServiceBase' from assembly 'System.ServiceProcess' Commented Dec 14, 2017 at 15:49
  • I am using that nuget package. My underlying solution framework is .Net 4.6.1 which allows you to use that package and run it as a service. Commented Dec 14, 2017 at 15:52
  • There has to be something in how to access the configuration properties that I am missing. Like I said running it through visual studio in IIS express reads the values just fine, but when it runs as a service I don't think it's looking at the same file. Commented Dec 14, 2017 at 15:53

1 Answer 1

9

When you launch your application as a service, the working directory is set to a system directory, like C:\Windows\System32\. Configuration file is looked in the same directory. As far as it's missing there, you get empty configuration loaded.

To fix it, just set working directory to the one where your application is located:

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thank you for working through this with me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.