Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

Cross-Library JSON Configuration in .NET Core

$
0
0

Let's say I have some projects, a library Foo and two projects Bar and Baz, which depend on Foo. Foo contains some configuration that will be shared between Bar and Baz, but Bar and Baz will also do some configuration that is different between them.

In Foo, I have a configuration file:

/* /dev/Foo/fooConfig.json */{"lorem": "ipsum","dolor": "set"}

and a method that does the initial configuration:

/* /dev/Foo/configuration.cs */public static IConfigurationBuilder BuildBaseConfiguration(){   return new ConfigurationBuilder()      .AddJsonFile("fooConfig.json")}

Then in Bar, I have something similar:

/* /dev/Bar/barConfig.json */{"semper": "suspendisse"}
/* /dev/Bar/Program.cs */public static void main(){   BuildBaseConfiguration()      .AddJsonFile("barConfig.json")      .Build();}

Normally, Foo is distributed as a NuGet package, but during development, I reference it locally by including the following Bar.csproj:

<Reference Include="Foo"><HintPath>../Foo/bin/Debug/net6.0/Foo.dll</HintPath></Reference>

I've made sure that fooConfig.json is being copied to the output directory, and that it is indeed appearing after successfully running a build.

However, after running Bar, I get the following error:

System.IO.FileNotFoundException: The configuration file 'fooConfig.json' was not found and is not optional. The expected physical path was '/dev/Bar/bin/Debug/net6.0/fooConfig.json'.

It would seem that .NET Core is looking for the config file using a relative file path based on the working directory at runtime (/dev/Bar/bin/Debug/net6.0), rather than where the file is actually kept (../Foo/bin/Debug/net6.0/fooConfig.json).

How do I correct this behavior, so that .NET Core references the real location of fooConfig.json?


Viewing all articles
Browse latest Browse all 5049

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>