Appsettings.json in a .NET Core Console Application

This is applicable to .NET Core 2.0, and is the equivalent of the old app.config and ConfigurationManager pattern. Below are the steps to set it up.

This is a modified version of Ray Vega’s answer on stackoverflow.

  1. Create a file named appsettings.json at the project root. (The filename can actually be anything, and is referenced below, but appsettings.json is a good convention.)
  2. Add your settings to that file in JSON format. Note that the file is much more flexible than the old app.config, and this is just an option. I currently prefer to have an AppSettings section here:

  3. Set appsettings.json to be copied to the output directory if there have been any changes, since we’ll load it from the default path below¬†(in Visual Studio, right click the file, and choose Properties. Then under the advanced section in the window, set Copy to output directory to Copy if newer):
    Copy if newer settings in Visual Studio
  4. Install these two NuGet packages:

    • Microsoft.Extensions.Configuration.Json
    • Microsoft.Extensions.Options.ConfigurationExtensions
  5. Add an AppSettings.cs file to your project with a class and properties that match the names you added in the JSON file above. For example:

  6. And add the following code to Main in Program.cs, or factored out into a separate function (note the filename “appsettings.json” below — it should match whatever you created above):
  7. You can now access the configuration values using properties of the appSettings property, for example:

‘Microsoft.SqlServer.Types’ version 10 or higher could not be found

If you have a connection to SQL Server where you’re using spatial types with Entity Framework, and deploying to an Azure service/server, you’re likely to come across the exception:

Spatial types and functions are not available for this provider because the assembly ‘Microsoft.SqlServer.Types’ version 10 or higher could not be found.

While you can install SQL Server, a far easier way around it is to install the NuGet package Microsoft.SqlServer.Types.

After you install that NuGet package, you’ll want to make a call to SqlServerTypes.Utilities.LoadNativeAssemblies prior to any actual calls in Entity Framework.

ASP.NET (e.g. in Global.asax.cs -> Application_Start)

Standalone Application

Additionally, in the current versions of Entity Framework, there is a hardcoded reference to only look for versions 10 and 11 of the Microsoft.SqlServer.Types assembly. You can fix this by doing the following in code (the version number should match the version you’re using via the NuGet package above):

The final version:

ASP.NET:

Standalone Application: