111

I'm developing an ASP.NET 5 WebAPI project using VS Ultimate 2015 Preview. I'm trying to configure the app in this way (line numbers are just guides):

1 using Microsoft.Framework.ConfigurationModel;
2
3 public IConfiguration Configuration { get; private set; }
4 
5 public Startup()
6 {
7     Configuration = new Configuration()
8         .AddJsonFile("config.json")
9         .AddEnvironmentVariables();
10 }

Line 8 gives me an error: 'Configuration' does not contain a definition for 'AddJsonFile'...

What is wrong?

Thorkil Værge
  • 2,727
  • 5
  • 32
  • 48
NetCito
  • 1,253
  • 2
  • 9
  • 6

4 Answers4

267

You need to include the Microsoft.Extensions.Configuration.Json NuGet package if you want to call the .AddJsonFile() method.

See: https://github.com/aspnet/Configuration/tree/dev/src/Microsoft.Framework.ConfigurationModel.Json

For further reading, here's a nice tutorial: ASP.NET vNext Moving Parts: IConfiguration.

Nick Alexeev
  • 1,688
  • 2
  • 22
  • 36
Lonig
  • 2,694
  • 1
  • 11
  • 2
  • 12
    Since RC2 it's now `Microsoft.Extensions.Configuration` and `Microsoft.Extensions.Configuration.Json ` – Chris S Jun 07 '16 at 18:22
  • 6
    To further specify the comment added by Chris: As of 22.07.2016 you should add `Microsoft.Extensions.Configuration.Json` as a *dependency* in your *project.json* file, and then add `using Microsoft.Extensions.Configuration` in your *Startup.cs* class. – Tormod Haugene Jul 22 '16 at 11:54
  • 2
    Links are broken – WPFUser Sep 13 '18 at 12:10
  • This answer is still useful four years later! – MEMark Oct 17 '18 at 20:19
33

I know this is a bit old but I just ran into this issue when attempting to build my first Asp.net core 1.0 blank project. In order to use the AddJsonFile method you must add a reference to Microsoft.Extensions.Configuration.Json to your project via Nuget.

To install the reference run the below command in the package manager console:

PM> Install-Package Microsoft.Extensions.Configuration.Json
WBuck
  • 5,162
  • 2
  • 25
  • 36
4

In case anyone else has been having trouble with this, Microsoft have made breaking changes to this part of the framework on 16 August 2015. You must import the right versions of the dependencies and switch across to the new way of building up configuration.

My config includes:

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.Framework.Runtime": "1.0.0-*",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta7",
    "Microsoft.AspNet.Mvc": "6.0.0-beta7",
    "Microsoft.Framework.Configuration": "1.0.0-beta7",
    "Microsoft.Framework.Configuration.Json": "1.0.0-*"
  },
...
}

This code, inspired by this question might go some way to helping you:

using System;
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Messenger.Services;
using Microsoft.Framework.Configuration;
using Microsoft.Dnx.Runtime;
using Microsoft.AspNet.Hosting;

namespace Messenger
{
    public class Startup
    {
        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
            Configuration = configurationBuilder.Build();
        }

        public IConfiguration Configuration { get; set; }
    }
...

}

Hope it helps.

Community
  • 1
  • 1
John Thow
  • 409
  • 1
  • 4
  • 11
1

Under project.json you will need to add it within dependencies

dependencies {
"Microsoft.Extensions.Configuration.Json": "1.0.0"
}
D.AhmedRafik
  • 81
  • 2
  • 15