How to get relative file path of app.settings file shared between projects

1

I have an appsettings.config file in the root of my solution, displayed in the 'Solution Items' folder that I want to reference in 3 different projects

enter image description here

These 3 projects consist of the following

  • Web performance and load test project
  • Unit test project (UI automation tests)
  • Standard Class Library

I have a static wrapper for the config file in the class library project that I intend to reference from the other projects.

The problem I have is getting the correct relative path of the solution root directory, regardless of which project is running at the time.

This is my code for the wrapper class....

public static class TestConfiguration
{
    private static Configuration Config { get; }

    static TestConfiguration()
    {
        var map = new ExeConfigurationFileMap();

        map.ExeConfigFilename = Path.Combine(@"../../../appsettings.config");

        Config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    }

    public static string SepaTestDb {
        get { return Config.ConnectionStrings.ConnectionStrings["SEPATest"].ConnectionString; }
    }

    public static string ApiUrl => Config.AppSettings.Settings["apiUrl"].Value;
}

This works for the load test project but for the others I get...

System.NullReferenceException occurred
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=SA.SEPA.Web.UI.Selenium.Integration

...when I try to get either the SepaTestDb or the ApiUrl property, presumably due to the object not being initialized from an invalid file path

How do I set a relative path so that the appsettings.config file is accessible from the different projects, and will be picked up if any of the projects are being run in a build/in the cloud etc.

EDIT

..and this is my appsettings.config file contents

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="SEPATest" connectionString="Server=[server];Database=SEPA-c7455a3a-86bd-4862-bb25-2b5d17d97f95Test;User Id=[username];Password=[password];" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="apiUrl" value="https://sasepaapi-sasepaapitest.azurewebsites.net/api" />
  </appSettings>
</configuration>

Many thanks,

c#
asp.net
visual-studio
filepath
appsettings
asked on Stack Overflow Oct 27, 2017 by Konzy262 • edited Oct 27, 2017 by Konzy262

1 Answer

3

Solution 1: Mostly for development, it won't allow sharing config file on running environment

You can link appsettings.config to each project (Add Item As Link)

Then change "Copy To Output Directory" Property of these links to "Copy If Newer"

enter image description here

In the config file of each project use below:

...    
<appSettings configSource="appsettings.config" />
...

UPDATE :

Solution 2: Support sharing config file on running environment

For hosting the appsettings.config at a common place You can also use this in projects's config files:

...
<appSettings file=".\..\..\..\appsettings.config" />
...

Or

An absolute path

...
<appSettings file="C:\tmp\appsettings.config" />
...

You can access these settings via ConfigurationManager.AppSettings[]. No extra work required for loading that file.

answered on Stack Overflow Oct 27, 2017 by Khoa Nguyen • edited Oct 27, 2017 by Khoa Nguyen

User contributions licensed under CC BY-SA 3.0