10

I have a .Net core worker service, which I am running as a windows service. The service is using appsettings.json file for the config information. After installing the service using SC CREATE command, the service was failing.

In the event viewer I found the error it cannot find the file C:\Windows\System32\appsettings.json. My service files are placed in a different folder c:\Services\, instead of looking at that location, the service is looking for a the file in System32 folder.

The configuration registration is as below.

 var configBuilder = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json");
var configuration = configBuilder.Build();
            services.AddSingleton(configuration);

How can I make the service to look at the local folder?

2
  • Did you manage to solve this issue @ajith?
    – Max Medina
    Mar 27, 2022 at 17:27
  • @MaxMedina I managed to solve it bit the one marked as Answer. I used Assembly.GetExecutingAssembly().Location
    – Ajith
    Mar 28, 2022 at 11:20

3 Answers 3

11

That's because the current directory is changed to C:\Windows\System32 at runtime. You could get a relative path by Assembly.GetExecutingAssembly(). For example:

var configBuilder = new ConfigurationBuilder()
    .SetBasePath( Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location))
    .AddJsonFile("appsettings.json");
var configuration = configBuilder.Build();
5
  • I tried this. Got error Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.DirectoryNotFoundException: C:\Services\MyAssemblyName.dll\
    – Ajith
    Dec 4, 2019 at 10:34
  • 1
    @Ajith Did you use Path.GetDirectoryName( ) as I did above? or use exactly the same code as mine? I test my above code. It works fine for me.
    – itminus
    Dec 4, 2019 at 10:38
  • @itminus I have used the same as your code. But still getting the same error on EventViewer.Do you know other possible reason for this issue? Sep 7, 2020 at 9:09
  • @ShaileeShah used the solution suggested by itminus above. It is using Assembly.GetExecutingAssembly().Location
    – Ajith
    Sep 15, 2020 at 8:20
  • It didn't work for me either, but pointed me in the Event Log that the assembly is running from 'C:\WINDOWS\TEMP\.net\<AssemblyName>\<weirdalphanum>. Which goes against what's in the docs from MS. Jul 29, 2021 at 18:15
5

I´ve successfully used the docs in Host ASP.NET Core in a Windows Service

In a nutshell, you should just add .UseWindowsService() in your configuration builder step, like in the following:

Host.CreateDefaultBuilder(args)
    .UseWindowsService()
    .ConfigureAppConfiguration((hostContext, configuration) => 
        {
            //...set your configurations here
        })
        .ConfigureServices((hostContext, services) =>
        {
            //...configure you services here
        }
2

I solved it differently by setting the ContentRoot. But I'm installing the service to windows using Wix.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Diagnostics;
using System.IO;

namespace ConverterService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
            string pathToContentRoot = Path.GetDirectoryName(pathToExe);

            CreateHostBuilder(args, pathToContentRoot).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args, string contentRoot) =>
            Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .UseContentRoot(contentRoot)
                .ConfigureServices((hostContext, services) =>
                {
                    ConverterConfig config = hostContext.Configuration
                    .GetSection("ConverterConfig")
                    .Get<ConverterConfig>();

                    config.RootPath = contentRoot;

                    services.AddSingleton(config);
                    services.AddHostedService<WindowsBackgroundService>();
                });
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.