4

I am building a simple console app to process a large CSV file. The SDK is version 5.0.202 and I am on Manjaro Linux.

CODE

using System;
using System.Text;
using TinyCsvParser;
using System.Linq;
using Microsoft.Extensions.Hosting;

namespace DrawsParser
{
    class Program
    {
        static void Main(string[] args)
        {
          var host = new HostBuilder()
            .ConfigureAppConfiguration((hostContext, builder) =>
            {
                if (hostContext.HostingEnvironment.IsDevelopment())
                {
                    builder.AddUserSecrets<Program>();
                }
            })
            .Build();

            host.Run();

            CsvParserOptions options = new CsvParserOptions(true, ',');
            CsvDrawResultMapping mapping = new CsvDrawResultMapping();
            CsvParser<DrawResult> parser = new CsvParser<DrawResult>(options, mapping);

            var results = parser
              .ReadFromFile(@"subset_game_data.csv", Encoding.UTF8)
              .ToList();

            foreach (var result in results)
            {
              var item = result.Result;
              string text = $"{item.Date.ToString()} | {item.State} | {item.Game} | {item.OriginalResult}";
              Console.WriteLine(text);
            }
        }
    }
}

PACKAGES

DrawsParser on  feature/reading-csv-file [!?] •NET v5.0.202  net5.0 ❯ dotnet list package
Project 'DrawsParser' has the following package references
   [net5.0]:
   Top-level Package                                     Requested   Resolved
   > Microsoft.Extensions.Configuration                  5.0.0       5.0.0
   > Microsoft.Extensions.Configuration.UserSecrets      5.0.0       5.0.0
   > Microsoft.Extensions.Hosting                        5.0.0       5.0.0
   > TinyCsvParser                                       2.6.0       2.6.0

When I run dotnet build . I get the following error:

/home/ryan/work/will/parser/DrawsParser/Program.cs(18,29): error CS1061: 'IConfigurationBuilder' does not contain a definition for 'AddUserSecrets' and no accessible extension method 'AddUserSecrets' accepting a first argument of type 'IConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?) [/home/ryan/work/will/parser/DrawsParser/DrawsParser.csproj]

I'm not sure what I'm missing, I am actually following the official docs here.

J86
  • 14,345
  • 47
  • 130
  • 228
  • 4
    I think you're missing the `using Microsoft.Extensions.Configuration` line – MindSwipe Jun 07 '21 at 10:18
  • Thank you @MindSwipe, you were right. As someone starting out, how can I find out that I needed that `using` statement? The error message doesn't even hint at that. – J86 Jun 07 '21 at 10:22
  • 1
    I searched the web by copying your error message and found that problem immediately. l found this - https://stackoverflow.com/questions/43255160/asp-net-core-1-1-web-api-using-secret-manager-tool – Ben.S Jun 07 '21 at 10:24
  • 1
    How will the compiler know? You can have your own custom code in some other library to do the same implementation – Akshay G Jun 07 '21 at 10:24
  • 1
    Well, the error message says `are you missing a using directive or an assembly reference?`, so I wouldn't say `The error message doesn't even hint at that` – MindSwipe Jun 07 '21 at 10:24
  • Does this answer your question? [ASP .NET Core 1.1 web api using Secret Manager tool](https://stackoverflow.com/questions/43255160/asp-net-core-1-1-web-api-using-secret-manager-tool) – Akshay G Jun 07 '21 at 10:28

1 Answers1

7

Googling C# IConfigurationBuilder.AddUserSecrets leads to this docs page, which shows that the AddUserSecret extension method is defined in the Microsoft.Extensions.Configuration namespace, and checking your code you're missing the using Microsoft.Extensions.Configuration; directive

MindSwipe
  • 7,193
  • 24
  • 47