1

Building a .net Framework (4.7.2) Windows service and trying to reference a few class libraries that are targeting .net standard 2.0

Im trying to use the newer configuration methods with ConfigurationBuilder. eg.

    config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional:false)
            .Build();

When I attempt to startup the service to debug, it fails to start and I get this in the Windows app log

Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.FileLoadException

The appsettings file(S) are there in the target folder, but the Config builder cant seem to load them?

Im referencing all the packages:

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Abstractions
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.FileProviders.Physical

[ update ]
While, this is related to some of the linked issues that were suggested as duplicates, its not the same. Im not publishing to Azure and im not doing this from a .net core web app. This is from a windows service project running .net framework 4.7

At first, I thought it was having trouble FINDING the json config files, but the exception is actually a FileLoadException https://learn.microsoft.com/en-us/dotnet/api/system.io.fileloadexception?view=netframework-4.8

Which seems to indicate the code found the file, but just couldnt open it? Strange, expecially since Im running as a local admin.

[ update ]
If I can't figure this out, Im going to next try creating my service, not with .net framework 4.7, but with .net core and use the Windows Compatibility Pack like in this example
https://www.pmichaels.net/2019/01/08/creating-a-windows-service-using-net-core-2-2/

bitshift
  • 6,026
  • 11
  • 44
  • 108
  • Possible duplicate of [The configuration file 'appsettings.json' was not found and is not optional](https://stackoverflow.com/questions/39157781/the-configuration-file-appsettings-json-was-not-found-and-is-not-optional) – TrevorBrooks Sep 23 '19 at 20:20

1 Answers1

0

My solution was to replace the service with a new project, starting with a .net core 2.2 console app, then using this class along with the "plumbing" for managing it via windows services, as mentioned in this thread How to make a Windows Service from .Net Core 2.1/2.2 Works like a champ,

bitshift
  • 6,026
  • 11
  • 44
  • 108