IConfiguration does not contain a definition for GetValue

Machado picture Machado · Feb 19, 2019 · Viewed 19.5k times · Source

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?

Answer

Machado picture Machado · Feb 19, 2019

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.