15

I need to read a list of properties from appsettings.json file (section: placeto) in a business class, but I haven't been able to access them. I need these properties to be public.

I add the file in the Program class:

Class program

This is my appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "placeto": {
    "login": "fsdfsdfsfddfdfdfdf",
    "trankey": "sdfsdfsdfsdfsdf"
  }
}
aaron
  • 39,695
  • 6
  • 46
  • 102
Jhon Diaz
  • 187
  • 1
  • 1
  • 13
  • 1
    Possible duplicate of https://stackoverflow.com/questions/46632090/mvc-core-2-0-how-to-use-an-appsettings-json-file-via-configuration/46632521#46632521 – anserk Nov 21 '17 at 19:48
  • This documentation from MS explain it how to => https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration – CodeNotFound Nov 21 '17 at 19:56
  • but i need read in custom class, the example is in controllers and i haven tried several options. in .Net standard used ` string userName = System.Configuration.ConfigurationManager.AppSettings["PFUserName"];` for read web.config, in .Net core which is equivalent @CodeNotFound – Jhon Diaz Nov 21 '17 at 20:34
  • 2
    The answer I linked you shows how to use `appsettings.json` in a class. In that case is a `LocalMailService` but it could be anything you want it to be. – anserk Nov 21 '17 at 22:10

1 Answers1

17

First : Use the default in program.cs for it already adds Configuration:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

Second : Create an Interface for your class and pass configuration with dependency injection by creating Iconfiguration field:

private readonly IConfiguration Configuration;

then pass it by contructor:

public Test(IConfiguration configuration)
{
    Configuration = configuration;
}

Then create an interface for your class in order to use Dependency Injection properly. Then one can create instances of it without needing to pass IConfiguration to it.

Here is the class and Interface:

using Microsoft.Extensions.Configuration;

namespace GetUserIdAsyncNullExample
{
    public interface ITest { void Method(); }

    public class Test : ITest
    {
        public Test(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        private readonly IConfiguration Configuration;
        public void Method()
        {
            string here = Configuration["placeto:login"];
        }
    }
}

Third: Then in your startup.cs implement Dependency Injection for your class by calling:

services.AddSingleton< ITest, Test>();

in your ConfigureServices method

Now you can pass your class instances to any class used by Dependency Injection too.


For example, if you have ExampleController that you wanna use your class within do the following:

 private readonly ITest _test;

 public ExampleController(ITest test) 
 {
     _test = test;          
 } 

And now you have _test instance to access it anywhere in your controller.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Mohamad Elnaqeeb
  • 541
  • 4
  • 13