IConfiguration does not contain a definition for GetValue

C#asp.netConfigurationasp.net Core-2.2

C# Problem Overview


After moving a class through projects, one of the IConfiguration methods, GetValue<T>, stopped working. The usage is like this:

using Newtonsoft.Json;
using System;
using System.Net;
using System.Text;
using Microsoft.Extensions.Configuration;

namespace Company.Project.Services
{
    public class MyService
    {
        private readonly IConfiguration _configuration;

        public string BaseUri => _configuration.GetValue<string>("ApiSettings:ApiName:Uri") + "/";

        public MyService(
            IConfiguration configuration
        )
        {
            _configuration = configuration;
        }
    }
}

How can I fix it?

C# Solutions


Solution 1 - C#

Just install Microsoft.Extensions.Configuration.Binder and the method will be available.

The reason is that GetValue<T> is an extension method and does not exist directly in the IConfiguration interface.

Solution 2 - C#

The top answer is the most appropriate here. However another option is to get the value as a string by passing in the key.

public string BaseUri => _configuration["ApiSettings:ApiName:Uri"] + "/";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMachadoView Question on Stackoverflow
Solution 1 - C#MachadoView Answer on Stackoverflow
Solution 2 - C#Jordan RyderView Answer on Stackoverflow