aspnet / Configuration

[Archived] Interfaces and providers for accessing configuration files. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IConfiguration.Bind extension is missing

ddizh opened this issue · comments

As per this doc I should be able to bind a JSON configuration to its POCO representation via IConfiguration.Bind extension method which is in Microsoft.Extensions.Configuration package, but I'm not able to.

Environment

.NET Command Line Tools (2.1.300-preview1-008174)

Product Information:
 Version:            2.1.300-preview1-008174
 Commit SHA-1 hash:  b8df89a54f

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.12-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.300-preview1-008174/

Microsoft .NET Core Shared Framework Host

  Version  : 2.1.0-preview1-26216-03
  Build    : f2c3216183d20416568a4bbf5bb7d153e826f153

Code

Program.cs

using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace DocFX.SpellCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);

            var appSettings = new AppSettings(); //POCO

            IConfiguration config = builder.Build();   
            config.Bind(config, appSettings); //error CS0103
        }
    }
}

csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
  </ItemGroup>

</Project>

Actual result:

error CS1061: 'IConfiguration' does not contain a definition for 'Bind' and no extension method 'Bind' accepting a first argument of type 'IConfiguration' could be found (are you missing a using directive or an assembly reference?)

Also, is there a reason why this api has not been shipped even with 2.0?

dotnet add package Microsoft.Extensions.Configuration.Binder is the missing piece which solved the issue. I got confused by namespace\package names difference.
Closing this