I am developing a blazor application. I want to have connection string and some other key in a class as service.
For that I have created an interface
interface IDbConnector
{
string ConnectionString { get; set; }
bool SomeKey { get; set; }
}
and in my class I want to have something like that
using Microsoft.Extensions.Configuration;
public class DbConnector : IDbConnector
{
private IConfiguration Configuration { get; set; }
public DbConnector(IConfiguration configuration)
{
Configuration = configuration;
}
public string ConnectionString = Configuration.GetConnectionString();
public bool SomeKey = Configuration.GetSection("xyz");
}
I can register it as a service with
services.AddScoped<IDbConnector, DbConnector>();
But inside DbConnector class it says
A fiels initializer can not refrence the non static field ,method or property DbConnector.Configuration
Pardon for my coding pattern as I am new to DI concept. Please suggest if there is another and better way to do this.