Issue with Services when attempting to add Context to Startup.cs

asp.net-core-mvc blazor blazor-server-side c# entity-framework-core

Question

I get this error when I attempt to migrate my project:

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: AIR.Data.AmaxContext Lifetime: Scoped ImplementationType: AIR.Data.AmaxContext': A suitable constructor for type 'AIR.Data.AmaxContext' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.) (Error while validating the service descriptor 'ServiceType: AIR.Data.Services.SkuService Lifetime: Scoped ImplementationType: AIR.Data.Services.SkuService': A suitable constructor for type 'AIR.Data.AmaxContext' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.)
Unable to create an object of type 'AmaxContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

This is my configure services:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddServerSideBlazor();
    var builder = new MySqlConnectionStringBuilder(
        Configuration.GetConnectionString("AmaxContext")) {Password = Configuration["DbPassword"]};
    var connection = builder.ConnectionString;
    services.AddDbContext<AmaxContext>(options =>
        options.UseMySql(connection, builder =>
        {
            builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
        }));
    services.AddScoped<SkuService>();
}

This is my skuservice:

using AIR.Data.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AIR.Data.Services
{
    public class SkuService
    {
        private AmaxContext _context;

        public SkuService(AmaxContext context)
        {
            _context = context;
        }

        public async Task<List<Core.SKU>> GetSkus()
        {
            return await _context.SKUs.ToListAsync();
        }
    }
}

I'm still quite new to blazor and I am attempting to migrate from mvc .net core. I attempted to find help in the C# discord and Blazor discord but they can't seem to know why either.

1
0
1/13/2020 7:32:00 PM

Accepted Answer

It turns out, my Context's constructor was set to Protected instead of Public. Problem Solved. Thanks for reading.

1
1/13/2020 8:00:01 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow