如何访问 Blazor 服务器中的 appsettings.json 数据
我正在尝试在 blazor 服务器应用程序的后台服务中访问存储在 appsettings.json 文件中的数据,但大多数有关如何在线执行此操作的插图都是基于 Blazor 的 Webassemble 配置。我想我应该使用 Microsoft.Extensions.Configuration 包,但我不太清楚如何实现它。
下面是带有基本代码的后台服务
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using log4net;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
namespace BlazorServerApp
{
public class JobExecutedEventArgs : EventArgs { }
public class PeriodicExecutor : IDisposable
{
Timer _Timer;
bool _Running;
public void StartExecuting()
{
if (!_Running)
{
// Initiate a Timer
_Timer = new Timer();
_Timer.Interval = 5000;
_Timer.Elapsed += HandleTimer;
_Timer.AutoReset = true;
_Timer.Enabled = true;
_Running = true;
}
}
void HandleTimer(object source, ElapsedEventArgs e)
{
// Execute required job
//connect to the appropriate SQL database
var connectionString = "connection";
//create connection variables for the two main SQL operations we will be doing
using var connectionUpdate = new SqlConnection(connectionString);
using var connectionCreate = new SqlConnection(connectionString);
while (true)
{
Parallel.Invoke(
() => createSurveyQueueEntries(connectionCreate),
() => updateSurveyQueue(connectionUpdate));
}
// Notify any subscribers to the event
OnJobExecuted();
}
public void Dispose()
{
if (_Running)
{
_Timer?.Dispose();
}
}
}
}
我想获取appsetting.json中定义的connectionString并使用它来设置HandleTimer中的
方法。connectionString
变量( )
下面是 appsettings.json 文件
{
"connectionString": "connection",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
任何详细的指导将不胜感激。
I am trying to access the data stored in the appsettings.json file in a background service in my blazor server app but most illustrations of how to do this online are based on the Webassembly configuration of Blazor. I think I'm suppossed to use the Microsoft.Extensions.Configuration package but I can't quite figure out how to implement it.
Below is the background service with the essential code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Timers;
using log4net;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
namespace BlazorServerApp
{
public class JobExecutedEventArgs : EventArgs { }
public class PeriodicExecutor : IDisposable
{
Timer _Timer;
bool _Running;
public void StartExecuting()
{
if (!_Running)
{
// Initiate a Timer
_Timer = new Timer();
_Timer.Interval = 5000;
_Timer.Elapsed += HandleTimer;
_Timer.AutoReset = true;
_Timer.Enabled = true;
_Running = true;
}
}
void HandleTimer(object source, ElapsedEventArgs e)
{
// Execute required job
//connect to the appropriate SQL database
var connectionString = "connection";
//create connection variables for the two main SQL operations we will be doing
using var connectionUpdate = new SqlConnection(connectionString);
using var connectionCreate = new SqlConnection(connectionString);
while (true)
{
Parallel.Invoke(
() => createSurveyQueueEntries(connectionCreate),
() => updateSurveyQueue(connectionUpdate));
}
// Notify any subscribers to the event
OnJobExecuted();
}
public void Dispose()
{
if (_Running)
{
_Timer?.Dispose();
}
}
}
}
I would like to take the connectionString defined in appsetting.json and use it to set the connectionString
variable in the HandleTimer()
method.
Below is the appsettings.json file
{
"connectionString": "connection",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Any detailed guidance would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在处理某些类,例如创建服务,则需要在
construction
中添加IConfiguration
:如果您有
razor
组件那么你需要像这样注入IConfiguration
:你可以阅读更多关于这个
If you are working in some class for example like creating a service, you need to add
IConfiguration
in theconstructure
:If you have a
razor
component then you need toInject IConfiguration
like this:You can read more about this
除了上面的解决方案之外,以下解决方案也有效:
首先安装此包:
using Microsoft.Extensions.Configuration;
然后在类的顶部添加以下代码行:
然后您可以使用密钥访问存储的数据,如下所示:
检查以下内容有关详细信息的链接:https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration
The following solution, aside from the one above, also works:
First install this package:
using Microsoft.Extensions.Configuration;
Then add the following line of code at the top of the class:
And then you can access the stored data using the key like so:
Check the following link for more information: https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration