您是否需要在“启动”中为连接字符串的所有DI添加服务?

问题描述

我在应用程序中使用数据库,因此需要访问存储在appsettings.json文件中的连接字符串。在Startup.cs文件中多次添加服务似乎很麻烦,而我每次都在做同样的事情。由于我有一个连接字符串,需要在随后在Blazor页面上使用的类中使用该连接字符串,因此我尝试制作一个提供服务的连接字符串:

public class ConnectionStringService
    {
        public string DataDB { get; set; }      
    }
//In Startup.cs
   services.Configure<ConnectionStringService>(Configuration.GetSection("ConnectionStrings"));
   services.AddScoped<ConnectionStringService>();
//I don't want a billion services.AddScoped<>(),one for every time I want to use a connection string,because it would get messy.

并尝试如下使用它:

 public class SearchesTimeline
    {
        [Inject]
        private ConnectionStringService connectionStrings { get; }
        //private readonly ConnectionStringService connectionStrings;
        //public SearchesTimeline(IOptions<ConnectionStringService> options)
        //{
        //    connectionStrings = options.Value;
        //}

        public async Task<List<List<Timelineinfo>>> GetTimeline(string? current,int? dateChange,string? date)
        { 
            if (current != null && dateChange != null)
            {
                date = Convert.ToDateTime(current).AddDays((double)dateChange).ToString("yyyy-MM-dd");
            }
            //This line is being executed
            else if (date == null | string.IsNullOrWhiteSpace(Extensions.GetQueryParm("d")))
            {
                date = DateTime.Today.ToString("yyyy-MM-dd");
            }
            else
            {
                date = Extensions.GetQueryParm("d");
            }

            for (int i = 0; i < 7; i++)
            {
                //get data
                DataAccess data = new DataAccess();
                var query = "SELECT t.TimelineinfoId From timelineinfo t where t.date = " + dates[i].ToString("yyyy-MM-dd") + ";";
                dateData[i] = await data.LoadData<Timelineinfo,dynamic>(query,new { },connectionStrings.DataDB);
            //Here,where I access connectionStrings.DataDB,I get the error
            }
            //more code stuff
        }
    }
//On the blazor page:
SearchesTimeline searches = new Searches.SearchesTimeline();

    [Parameter]
    public string? current { get; set; }
    [Parameter]
    public string? date { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await searches.GetTimeline(current,date);
    }
    public async Task NextWeek()
    {
        await searches.GetTimeline(current,+7,date);
    }
    public async Task PrevWeek()
    {
        await searches.GetTimeline(current,date);
    }

但是当我尝试运行它时,我得到了NullReferenceException:“对象引用未设置为对象的实例。”因此此代码无法正常工作。

是否可以为我所有的连接字符串需求配置一项服务,并以与上述类似的方式使用它?

解决方法

   services.Configure<ConnectionStringService>(Configuration.GetSection("ConnectionStrings"));

此行之后,您无需再将类型添加到DI中。您可以使用IOptions 接口获取值。像这样:

[Inject] private IOptions<ConnectionStringService> connectionStrings { get; }

,然后可以通过访问对象的Value属性来使用它:

connectionStrings.Value
,

我最终要做的是制作一个静态函数,该函数将在需要的地方返回连接字符串。我将Startup.cs文件更改为包含:

 public static string ConnectionString { get; private set; }

public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
    //Add alongside whatever else you have in Configure()
    ConnectionString = Configuration["ConnectionStrings:DataDB"];
}

函数真的很简单,只是:

public static string GetConnectionString()
{
    return Startup.ConnectionString; //returns DataDB connection string
}