I have a Blazor server app that is built on .NET Core 3.1. The app is working without problem.
After I updated from VS 2019 to VS 2022 and also .NetCore3.1 to Net6.0 I noticed that my scoped services are not running any more. I have read that the registration of the services are a little bit different in .NET6.0, but I couldn't make it working.
My actual registration is in Startup.cs (that is merged with Program.cs in .NET6.0 as I understood)
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<ConnectService>();
services.AddScoped<TagService>();
}
I tried to register in Pogram.cs like below but didn't work
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<ConnectService>();
builder.Services.AddScoped<TagService>();
}
I am injecting the service in my razor page as follows (that I haven't changed)
@page "/connect2"
@inject TagService TagService
You can get the basic structure with the templated solution.
dotnet new blazorserver -o <OutputDirectory>
It should look something like this
//declare web
var builder = WebApplication.CreateDefault(args)
//add services
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<ConnectService>();
builder.Services.AddScoped<TagService>();
//then build app
var app = builder.Build();
//add any neccessary routing middleware
//then run
app.Run()