0

I have to make a project with a N-tier architecture using a repository pattern without entity framework (School assignment). I want to be able to get configuration settings from a configuration file independent from the UI so I can get the right connectionstring to use as it has to support multiple database types.

I want to use a WPF UI and an ASP.NET website on my n-tier architecture and read a configuration file in the exact same way

In a WPF project it uses App.config, but in ASP.NET Core 2.0 it uses Appsettings.json.

This is my App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <appSettings>
    <!-- Context has to be set to use the right database-->
    <add key="Context" value="MSSQL"/>
  </appSettings>
  <connectionStrings>
    <add name="MSSQL" connectionString="my connectionstring here"/>
    <add name="ORACLE" connectionString="my connectionstring here"/>
  </connectionStrings>
</configuration>

Here is appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "Context":  {
    "ContextType": "MSSQL"
  },
  "ConnectionStrings":  {
    "MSSQL": "connectionstring",
    "ORACLE": "connectionstring",
    "MYSQL": "connectionstring"
  }
}

I'm getting the context and the connectionstrings from app.config like this:

Context = ConfigurationManager.AppSettings["Context"];
ConnString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["Context"]].ConnectionString;

I'm aware that ASP.NET Core 2.0 doesn't have the ConfigurationManager class. But isn't there a way to get the configuration settings from both UI's (WPF and ASP.NET) in the exact same way? Ideally I'd prefer a similar approach as ConfigurationManager

Would appreciate it if anyone could point me in the right direction on how to approach this.

Sezer Türkdal
  • 193
  • 2
  • 10
Dvg22
  • 5
  • 3
  • I didnt understand. You want to get those values from appsettings.json in .net core ? – Praneet Nadkar Mar 29 '18 at 11:30
  • [This](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration#json-configuration) seems pretty close to Configurationmanager to me. – Fildor Mar 29 '18 at 11:32
  • I want to get configuration settings from .net core the same way as in WPF. I know this isn't possible because one is XML and one is JSON. One thing i can think of is a custom config file in both WPF and .net core, but is that the right way? – Dvg22 Mar 29 '18 at 11:33
  • I would _read_ the config in a technology-specific way, but have a "centralized" configuration-data-provider, that I can inject anywhere needed. – Fildor Mar 29 '18 at 11:35

2 Answers2

2

There is actually an easier way to do it :) - which is best practice:

How to read AppSettings values from Config.json in ASP.NET Core

For Wpf, you should use ConfigurationManager, it's the simple way out as it's how Microsoft intended it to be used/ consumed. However, you can probably implement something like the above for Wpf, but you have to keep in mind that there is a price to pay when you're deviating from the 'built-in' approach - like deploying - using app.config allows you to modify the configs without redeploying, strait from azure portal, etc.

  • And you can use a `IConfiguration` in your WPF app to get the settings too. Just add the NuGet package [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration/) to your WPF project to get access to it. – Scott Chamberlain Mar 29 '18 at 20:40
  • @ScottChamberlain How would I implement the IConfiguration in WPF? In .net Core 2.0 I do it through startup.cs. Couldn't really find any info on how to use IConfiguration with WPF – Dvg22 Mar 31 '18 at 07:21
0

I solved this by adding a class called "ConfigSettings" Looking like this:

public class ConfigSettings : IConfig
{
    public string Connstring { get; }
    public string Context { get; set; }

    public ConfigSettings()
    {
        this.Context = ConfigurationManager.AppSettings["Context"];
        this.Connstring = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["Context"]]
            .ConnectionString;
    }


}

Then I pass it through my Factory:

private readonly Factory factory = new Factory(new ConfigSettings());

Factory:

    private string ConnString;
    private string Context;

    public Factory(IConfig config)
    {
        this.ConnString = config.Connstring;
        this.Context = config.Context;

    }

Now all I have to do for .NET core is making the ConfigSettings class and get the configurations from the appsettings.json and pass it to my factory.

If this is bad practice i'd love to hear it.

Dvg22
  • 5
  • 3