我正在开发一个 blazor 应用程序。我想在一个类中拥有连接字符串和其他一些键作为服务。
为此,我创建了一个界面
interface IDbConnector
{
string ConnectionString { get; set; }
bool SomeKey { get; set; }
}
在我的课堂上,我想要这样的东西
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");
}
我可以将其注册为服务
services.AddScoped<IDbConnector, DbConnector>();
但是在 DbConnector 类里面它说
字段初始化程序不能引用非静态字段、方法或属性 DbConnector.Configuration
请原谅我的编码模式,因为我是 DI 概念的新手。请建议是否有另一种更好的方法来做到这一点。