Reputation: 1489
I'm trying to set the application base path for a .NET web application I'm building. I keep getting errors on Configuration builder. This is the error I get.
DNX,Version=v4.5.1 error CS1061: 'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)
I'm assuming I'll get the same error for my .AddJsonFile()
and .AddEnvironmentVariables()
. Did I do something wrong? Did I not add the right dependency to my project.json? I've enclosed my startup.cs and my project.json.
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"tooling": {
"defaultNamespace": "TripPlanner"
},
"dependencies": {
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.Framework.Configuration": "1.0.0-beta8",
"Microsoft.Framework.DependencyInjection": "1.0.0-beta8"
//"Microsoft.Extensions.PlatformAbstractions": "1.0.0-beta8"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using TripPlanner.Services;
namespace TripPlanner
{
public class Startup
{
public static IConfigurationRoot Configuration;
public Startup(IApplicationEnvironment appEnv){
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services)
{
services.AddMvc();
#if DEBUG
services.AddScoped<IMailService, DebugMailService> ();
#else
services.AddScoped<IMailService, RealMailService> ();
#endif
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
//app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(config =>
{
config.MapRoute(
name: "Default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "App", action = "Index"}
);
});
}
// Entry point for the application.
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
}
The error is in the public startup
function right near the top of startup.cs.
Upvotes: 134
Views: 99020
Reputation: 1126
I am not sure if anyone still runs into this issue, but I was able to address this in a .NET Core console project (specifically targeting netcoreapp2.0
) via:
dotnet add package Microsoft.Extensions.Configuration.Json
Upvotes: 85
Reputation: 363
can go ahead with below dependencies:
npm-install Microsoft.Extensions.Configuration.FileExtensions
npm-install Microsoft.Extensions.Configuration.Json
Upvotes: 1
Reputation: 559
This worked for me by adding to the project file in .NET Core 3.1:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
</ItemGroup>
This worked in .NET 6:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>
Upvotes: 3
Reputation: 1340
One more possible issue...
I had to add this:
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.0.0" />
in order for this to compile:
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
...
I'm pretty sure it was for a console app.
Upvotes: 6
Reputation: 10705
If you’re running a .NET Core 1.x or .NET Standard 1.x, you should run this command:
dotnet add package Microsoft.Extensions.Configuration.Json -v 1.1.1
If your project is inside another folder:
dotnet add .\src\MyProject package Microsoft.Extensions.Configuration.Json -v 1.1.1
...where MyProject is the name of the .csproj
file.
Upvotes: 5
Reputation: 1498
Additionally, do not forget to set the "Copy to Output Directory" property to "Copy always" of the JSON file from Properties window.
Upvotes: 0
Reputation: 21
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsetting.json",
Optional: true, reloadOnChange: true);
}
For the above line of code, you will need the following NuGet packages:
Microsoft.extensions.configurations
Microsoft.extensions.configurations.json
Microsoft.extensions.Options.configurations
Upvotes: 0
Reputation: 952
Something else to consider:
using Microsoft.Extensions.Configuration;
Without that "using" statement, it doesn't find the extension method in Microsoft.Extensions.Configuration.FileExtensions.
The trouble cropped up for me because we were also:
using System.Configuration;
And there was a name clash with "ConfigurationBuilder". Add the
using Microsoft.Extensions.Configuration;
line... remove the
using System.Configuration;
line, then fully qualify anything under System.Configuration.
Upvotes: 5
Reputation: 692
Try adding the following in your .csproj
file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>
Upvotes: 14
Reputation: 39
Try adding the following to your project.json dependencies:
"Microsoft.Extensions.Configuration.CommandLine": "1.1.1",
Or in project.csproj:
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />
This worked for me.
Upvotes: 2
Reputation: 161
Try adding the following to your project.json dependencies:
"Microsoft.Extensions.Configuration": "1.0.0-*",
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-*",
Upvotes: 3
Reputation: 2416
I was able to solve the issue. If you have not yet solved it, try the following in the project.json. Add the following:
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"
and it should work
Upvotes: 240
Reputation: 47
Add the following to your project.json:
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*"
Upvotes: 2