0
votes

I am building a Blazor server app using a repository pattern and am trying to execute a stored procedure from a Razor component page.

However I am getting an error when I run it:

blazor.server.js:21 [2021-03-29T02:03:38.207Z] Error: System.InvalidOperationException: Cannot provide a value for property 'ICalculateImportanceService' on type 'ThePositionerBlazorServerDapperSyncfusion.Pages.DBProcessing.CalculateItemImportance'. There is no registered service of type 'ThePositionerBlazorServerDapperSyncfusion.Data.CalculateImportanceService'.

Not sure why I am getting the error because I believe I registered the service.

Here is the relevant code:

CalculateItemImportance.razor

@using ThePositionerBlazorServerDapperSyncfusion.Data
@page "/DBProcessing/calcultateitemImportance"
@inject CalculateImportanceService ICalculateImportanceService
@inject NavigationManager NavigationManager

<h1 style="text-align:center">@pagetitle</h1>

@code {

    public string pagetitle = "Calculate Importance";

    protected override async Task OnInitializedAsync()
    {
        // Kick off stored procedure to calculate importance
        await ICalculateImportanceService.CalculateImportance();
    }
}

ICalculateImportanceService.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ThePositionerBlazorServerDapperSyncfusion.Data;

namespace ThePositionerBlazorServerDapperSyncfusion.Data
{
    public interface ICalculateImportanceService
    {
        Task<bool> CalculateImportance();
    }
}

CalculateImportanceService.cs

using Dapper;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace ThePositionerBlazorServerDapperSyncfusion.Data
{
    public class CalculateImportanceService : ICalculateImportanceService
    {
        private readonly SqlConnectionConfiguration _configuration;

        public CalculateImportanceService(SqlConnectionConfiguration configuration)
        {
            _configuration = configuration;
        }

        public async Task<bool> CalculateImportance()
        {
            using (var conn = new SqlConnection(_configuration.Value))
            {
                await conn.ExecuteAsync("CalculateTheItemImportance", commandType: CommandType.StoredProcedure);
            }
            return true;
        }
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThePositionerBlazorServerDapperSyncfusion.Data;
using Syncfusion.Blazor;

namespace ThePositionerBlazorServerDapperSyncfusion
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            //services.AddSingleton<WeatherForecastService>();
            //Syncfusion support
            services.AddSyncfusionBlazor();
            services.AddControllers().AddNewtonsoftJson();
            var sqlConnectionConfiguration = new SqlConnectionConfiguration(Configuration.GetConnectionString("SqlDBContext"));
            services.AddSingleton(sqlConnectionConfiguration);
            services.AddScoped<IApplicConfsService, ApplicConfsService>();
            services.AddScoped<IPOSSUMMARYService, POSSUMMARYService>();
            services.AddScoped<IPOSDETAILService, POSDETAILService>();
            services.AddScoped<IDESCRIPTIONTYPEService, DESCRIPTIONTYPEService>();
            services.AddScoped<IIMPService, IMPService>();
            services.AddScoped<IITEMCATEGORYService, ITEMCATEGORYService>();
            services.AddScoped<IKNOWDEPService, KNOWDEPService>();
            services.AddScoped<IMembersService, MembersService>();
            services.AddScoped<IPRDSRVService, PRDSRVService>();
            services.AddScoped<IPROCESSESService, PROCESSESService>();
            services.AddScoped<ITASKKNOService, TASKKNOService>();
            services.AddScoped<ITMPOVERLAPService, TMPOVERLAPService>();
            services.AddScoped<ITEXTUALService, TEXTUALService>();
            services.AddScoped<ITIMESCALEService, TIMESCALEService>();
            services.AddScoped<IWORKHIERService, WORKHIERService>();
            services.AddScoped<ICalculateImportanceService, CalculateImportanceService>();
            services.AddScoped<ICalculateFTEService, CalculateFTEService>();
        }
    }
}

The stored procedure has no parameters and simply executes processing on the database.

Any help would be appreciated. Thanks

1

1 Answers

1
votes

Change

@inject CalculateImportanceService ICalculateImportanceService

to

@inject ICalculateImportanceService CalculateImportanceService

and further down

// await ICalculateImportanceService.CalculateImportance();
   await CalculateImportanceService.CalculateImportance();