Error Execution whith Blazor [RESOLVED]

Hi,how to risolve this error on my project in Run ?

InvalidOperationException: Cannot provide a value for property 'ProvinceService' on type 'BlazorAppUI.Pages.FetchProvince'. There is no registered service of type 'DAL.Services.ProvinceService'.


This my Project :

startup.cs:


namespace BlazorAppUI

{

public class Startup

{

public Startup(IConfiguration configuration)

{

Configuration = configuration;

}


public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)

{


services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddRazorPages();

services.AddServerSideBlazor();

services.AddTransient();

services.AddSingleton();

}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

}

}

}


In Program.cs


var builder = WebApplication.CreateBuilder(args);


// Add services to the container.

builder.Services.AddRazorPages();

builder.Services.AddServerSideBlazor();

builder.Services.AddSingleton();


var app = builder.Build();



// Configure the HTTP request pipeline.

if (!app.Environment.IsDevelopment())

{

app.UseExceptionHandler("/Error");

// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

app.UseHsts();

}




app.UseHttpsRedirection();


app.UseStaticFiles();


app.UseRouting();


app.MapBlazorHub();

app.MapFallbackToPage("/_Host");


app.Run();


In ProvinceServices.CS


namespace DAL.Services

{

    public class ProvinceService

    {

        protected readonly AziendaXDBContext _context;


        public ProvinceService(AziendaXDBContext context)

        {

            _context = context;

        }


         public List<Provincia> GetItems()

        {

            return _context.Province.ToList();

        }

        public Provincia GetItem(string key)

        {

            return _context.Province.Find(key);

        }


    }

}



Excuse me but I'dont very expert in this.

thanks



3 Replies 1 reply marked as answer

SP Sureshkumar P Syncfusion Team September 26, 2022 10:53 AM UTC

Hi Biagio,

We suspect that you have rendered the Syncfusion blazor application without registering the Syncfusion blazor service in the blazor App. So, we suggest you refer the AddSyncfusionBlazor service to the Startup.cs file.

Find the code example here:

services.AddSyncfusionBlazor();

Note: If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.

Regards,

Sureshkumar P


Marked as answer

BI Biagio September 28, 2022 06:48 AM UTC

--------

I'have resolved With:

IN PROGRAM.CS



    public class Program

    {

       public static void Main(string[] args)

        {

            CreateHostBuilder(args).Build().Run();

        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>

            Host.CreateDefaultBuilder(args)

                .ConfigureWebHostDefaults(webBuilder =>

                {

                    webBuilder.UseStartup<Startup>();

                });

    }

IN STARTUP WITH:

      public void ConfigureServices(IServiceCollection services)

        {

            services.AddRazorPages();

            services.AddServerSideBlazor();

            var connection = Configuration.GetConnectionString("DefaultConnection");

            services.AddDbContext<AziendaXDBContext>(options => options.UseSqlServer(connection));

            services.AddScoped<ProvinceService>();

            services.AddSingleton<WeatherForecastService>();

        }


THANKS



SS Shereen Shajahan Syncfusion Team September 28, 2022 07:17 AM UTC

Hi Biagio,


Glad to know that your issue is resolved. Please get back to us if you need further assistance.


Regards,

Shereen


Loader.
Up arrow icon