locked
accesso appsettings.json RRS feed

  • Question

  • User-1053625857 posted

    Hello everybody,

    how can i access to appsetting.json in my blazor server project and use it in componentbase.cs and class.cs ?

    Monday, November 9, 2020 1:17 PM

Answers

  • User475983607 posted

    Configuration is available through DI in every constructor.  

        public interface IConfigService
        {
            string GetGretting();
        }
    
        public class ConfigService : IConfigService
        {
            IConfiguration _configuration;
            public ConfigService(IConfiguration configuration)
            {
                _configuration = configuration;
            }
    
            public string GetGretting()
            {
                return _configuration["Greeting"];
            }
        }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();
        services.AddScoped<IConfigService, ConfigService>();
    @page "/"
    @inject Services.IConfigService ConfigService
    
    <button class="btn btn-primary" @onclick="GetGreeting">
        Get Greeting
    </button>
    
    <div>
        @Message
    </div>
    
    @code {
        private string Message { get; set; }
        private void GetGreeting()
        {
            Message = ConfigService.GetGretting();
        }
    }

    https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-3.1

    https://docs.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2020 4:34 PM
  • User475983607 posted

    Suppose to use in class (ex repository class) and use it in static method, it's always null

     [Inject]
            static protected IConfigService configService { get; set; }

    public static List<Employee> GetEmployee(string connectionStr)
            {

                // string connectionStr = configService.GetConnectionString();    // null error

    By definition (see the C# programming guide), a static method cannot access instance members.  ASP.NET DI uses constructor injection and constructors are methods that populate instance class members.  

    If all you want is the connection string then you can simple create a static variable in the startup.cs file.

        public class Startup
        {
            public static string connectionsString { get; set; }
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
                connectionsString = configuration["Greeting"];
            }
    <div>
        @Startup.connectionsString
    </div>

    you can also get the configuration from the DI with code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2020 9:36 PM

All replies

  • User475983607 posted

    Configuration is available through DI in every constructor.  

        public interface IConfigService
        {
            string GetGretting();
        }
    
        public class ConfigService : IConfigService
        {
            IConfiguration _configuration;
            public ConfigService(IConfiguration configuration)
            {
                _configuration = configuration;
            }
    
            public string GetGretting()
            {
                return _configuration["Greeting"];
            }
        }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();
        services.AddScoped<IConfigService, ConfigService>();
    @page "/"
    @inject Services.IConfigService ConfigService
    
    <button class="btn btn-primary" @onclick="GetGreeting">
        Get Greeting
    </button>
    
    <div>
        @Message
    </div>
    
    @code {
        private string Message { get; set; }
        private void GetGreeting()
        {
            Message = ConfigService.GetGretting();
        }
    }

    https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-3.1

    https://docs.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2020 4:34 PM
  • User-1053625857 posted

    Thank's, it works perfectly inside ComponentBase with  

    [Inject]
            protected IConfigService configService { get; set; }

    Suppose to use in class (ex repository class) and use it in static method, it's always null

     [Inject]
            static protected IConfigService configService { get; set; }

    public static List<Employee> GetEmployee(string connectionStr)
            {

                // string connectionStr = configService.GetConnectionString();    // null error

    Monday, November 9, 2020 8:04 PM
  • User475983607 posted

    Suppose to use in class (ex repository class) and use it in static method, it's always null

     [Inject]
            static protected IConfigService configService { get; set; }

    public static List<Employee> GetEmployee(string connectionStr)
            {

                // string connectionStr = configService.GetConnectionString();    // null error

    By definition (see the C# programming guide), a static method cannot access instance members.  ASP.NET DI uses constructor injection and constructors are methods that populate instance class members.  

    If all you want is the connection string then you can simple create a static variable in the startup.cs file.

        public class Startup
        {
            public static string connectionsString { get; set; }
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
                connectionsString = configuration["Greeting"];
            }
    <div>
        @Startup.connectionsString
    </div>

    you can also get the configuration from the DI with code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 9, 2020 9:36 PM
  • User-1053625857 posted

    OK thank's, it works perfectly !

    Tuesday, November 10, 2020 7:10 AM