skip to Main Content

I’m migrating an ASP.NET Web API solution that uses the .NET Framework 4.6.1 to ASP.NET Core 5 / .NET Framework 5. Besides the Web API project there is an MSTest based testing project that shares the connection string information with the Web API project. This looks as follows:

The Web API project has a Web.config file containing the following code:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings configSource="binConnectionInfo.config" />
  ...
</configuration>

The MSTest based project has an App.config file containing the following code:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
  <connectionStrings configSource="ConnectionInfo.config"/>
</configuration>

Hence, both of the above files reference ConnectionInfo.config which contains the actual connection string and looks as follows:

<connectionStrings>
  <add name="MyConnectionString" connectionString="the connection string" />
</connectionStrings>

That setup ensures that I have a single point where I need to change the connection string to point the application to another database.

Eventually, I retrieve the connection string in the test database and pass it to my DatabaseManager which provides functionality to create and reset the database:

string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
_databaseManager = new DatabaseManager(connectionString);

Now, after migrating the Web API project to ASP.NET Core 5 the Web.config is replaced with an appsettings.json file. There’s one such file for every environment and each contains the connection string for that environment.

Is there a way to share the corresponding connection string with the test project somehow? I don’t want to load the appsettings.json manually in that project but hope for a built-in mechanism. Is there anything that might help?

2

Answers


  1. Chosen as BEST ANSWER

    In order to read the connection string stored in the main project's appsettings.json from the test project I added the NuGet package Microsoft.Extensions.Configuration.Json to it.

    Then, I ensured that the appsettings.json file is copied to the output directory by the main project which can be done as follows in the .csproj file:

    <ItemGroup>
      <None Include="appsettings.Development.json">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
      <None Include="appsettings.json">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
    </ItemGroup>
    

    Finally, in the test project I obtain the connection string by reading the appsettings.json file as follows:

    protected static readonly string _connectionString = new ConfigurationBuilder()
      .SetBasePath(AppContext.BaseDirectory)
      .AddJsonFile("appsettings.json", false, true)
      .Build()
      .GetConnectionString("ConnectionStringKeyName");
    

  2. You can use the existing app.config to hold connection string like those in .NET Framework in .NET 5.0 by adding nuget package of System.Configuration.ConfigurationManager to the unit test project, and use the ConfigurationManager.ConnectionStrings as before in .NET Framework.

    The latest stable version is v5.0.0.

    See also the nuget page https://www.nuget.org/packages/System.Configuration.ConfigurationManager/5.0.0

    and the docs:
    https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.connectionstrings?view=windowsdesktop-5.0

    One thing is to be sure, the System.Configuration.ConfigurationManager nuget package is mostly Windows specific, and in the docs it is included as part of "Windows Desktop" of .NET 5.0, not the full cross platform of .NET 5.0.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search