[英]Blazor creating a service, to pull data from SQL Server stored procedure
I'm a noob when it comes to Blazor, and C# for that matter.对于 Blazor 和 C#,我是个菜鸟。
I'm trying to create a simple service.我正在尝试创建一个简单的服务。 Simple process that includes running a stored procedure, and displaying its results in a table.
包括运行存储过程并在表中显示其结果的简单过程。 I'm starting simple, by just making sure that the process runs.
我从简单开始,只需确保进程运行即可。
Here's what my service looks like:这是我的服务的样子:
public class ACheckerService : IACheckerService
{
// Database connection
private readonly SqlConnectionConfig _config;
public ACheckerService(SqlConnectionConfig config)
{
_config = config;
}
public async Task<IEnumerable<AChecker>> GetAllExceptions()
{
IEnumerable<AChecker> exceptions;
using (var conn = new SqlConnection(_config.Value))
{
exceptions = await conn.QueryAsync<AChecker>("Auto.GetAll", commandType: System.Data.CommandType.StoredProcedure);
}
return exceptions.ToList(); ;
}
}
I created AChecker
model class which comprises of the fields that are to be pulled in by the stored procedure.我创建了
AChecker
model class,其中包含要由存储过程引入的字段。 Just 4 fields.只有 4 个字段。
Once I had this done, I added a Blazor component ( .Razor
)完成此操作后,我添加了一个 Blazor 组件 (
.Razor
)
This is what my code looks like:这是我的代码的样子:
@page "/Data/Auto"
@using AChecker.Data
@inject IACheckerService CheckerService
<h3>Auto</h3>
<p>This component demonstrates Auto Exceptions</p>
@if (aChecker is null)
{
<p><em>Loading....</em></p>
}
else
{
<p><em>Data....</em></p>
}
@code {
private IEnumerable<AChecker> aChecker;
protected override async Task OnInitializedAsync()
{
aChecker = await CheckerService.GetAllAutoExceptions();
}
}
I added this service in Startup.Cs like:我在 Startup.Cs 中添加了此服务,例如:
services.AddSingleton<ACheckerService>();
Is there any issue with the code above or why it would not work?上面的代码是否有任何问题或为什么它不起作用?
I have this service added in my app, basically when I click on the menu, I expect to see either Loading or Data (ideally down the road, I want to create a table and populate the fields from the stored procedure in it, there are 4 fields), however I get a bunch of errors:我在我的应用程序中添加了这项服务,基本上当我点击菜单时,我希望看到加载或数据(理想情况下,我想创建一个表并从其中的存储过程填充字段,有4 个字段),但是我得到了一堆错误:
InvalidOperationException: Cannot provide a value for property CheckerService on type AChecker.Pages.MyPages.Auto.
InvalidOperationException:无法为 AChecker.Pages.MyPages.Auto 类型的属性 CheckerService 提供值。 There is no registered service of type AChecker.Data.IACheckerService
没有 AChecker.Data.IACheckerService 类型的注册服务
Try registering the service using this overload instead which specifies the interface and its concrete implementation:尝试使用此重载注册服务,而不是指定接口及其具体实现:
services.AddSingleton<IACheckerService, ACheckerService>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.