cs0815
cs0815

Reputation: 17408

getting security .Net Core code working in .Net Framework Console application

I inherited a bit of code:

 var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");

I think is stems from a .net Core application. First I struggled with this bit (in the sense that I could not compile it in .Net Framework):

.SetBasePath(Directory.GetCurrentDirectory())

But I came across this. The accepted answer solved it. Now I am struggling with this bit (again in the sense that I cannot compile it in .Net Framework)::

.AddJsonFile("appsettings.json")

Is there a way to fix this please (usually I would get such data from App.Config ...)? Thanks.

PS:

More code plus error message:

using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration.FileExtensions;
using Microsoft.Extensions.Configuration;

namespace SandboxSecurityToken
{
    class Program
    {
        static void Main(string[] args)
        {

        ...


        static async Task RunAsync()
        {
            var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");       


Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'IConfigurationBuilder' does not contain a definition for 'AddJsonFile' and no accessible extension method 'AddJsonFile' accepting a first argument of type 'IConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)    

Upvotes: 0

Views: 250

Answers (1)

Euphoric
Euphoric

Reputation: 12849

This is .NET Standard code that should work just fine on .NET Framework 4.6.1 or higher.

You don't provide enough information, but I would guess you are missing reference to this NuGet package : https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/2.2.0

Or you might be missing proper using:

using Microsoft.Extensions.Configuration;

So that AddJsonFile extension method is found.

Upvotes: 1

Related Questions