19

According to the docs for .NET Core 5, there's a method SetBasePath and it's widely used in a bunch of blogs (example 1, example 2, example 3 etc.). There's no notion of it being a weird gotcha or such. However, when I try the syntax below, it's marked red and claimed not to be there.

using System;
using Microsoft.Extensions.Configuration;
static void Main(string[] args)
{
  string path = AppDomain.CurrentDomain.BaseDirectory;
  IConfigurationBuilder builder = new ConfigurationBuilder();
  builder.SetBasePath(path);
}

I'm not sure why this happens not what to do about it.

Steps to reproduce:

  1. Create a vanilla console application in .NET Core 5 in C#.
  2. Paste in the code in Program.cs replacing the file's contents.
  3. Try to compile or execute.

The error received is like this.

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

As far i can understand, I have all the prerequisites in place.

2 Answers 2

38

Remember that multiple types can contribute methods to a single type through Extension Methods. And so SetBasePath was never a method on the IConfigurationBuilder interface.

And multiple Assemblies can contribute types to the same namespace, and the docs say the type that defines the SetBasePath extension method is in:

FileConfigurationExtensions Class Definition Namespace: Microsoft.Extensions.Configuration Assembly: Microsoft.Extensions.Configuration.FileExtensions.dll

FileConfigurationExtensions Class

Which you can easilly verify is not present in your dependencies. So you're missing a NuGet package. Turns out this one is easy to find: Microsoft.Extensions.Configuration.FileExtensions

2
  • 2
    Seems to be the precise issue. What puzzles me is that I haven't found this asked previously. It seems that there ought to be some other poor schmuck before me doing precisely that and not thinking about installing additional packages. Well, I guess I'm the original schmuck for this issue. Commented Jan 24, 2021 at 17:53
  • wow, appreciated what they've done breaking things down into smaller nuget packages, however, discoverable has been penalised here. i've had to look for a few more packages to get the some ported code working with .NET 6. easy way to find them all: nuget.org/packages?q=Microsoft.Extensions.Configuration
    – Ruslan
    Commented Jul 25, 2022 at 14:35
0

You could also use Microsoft.Extensions.Configuration.Json package for an *.json file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.