admin管理员组

文章数量:1296857

I have a Blazor app with WebAssembly render mode. In my Client project, I have this page:

@page "/demo"
@page "/{lang}/demo"
@inject IDemoService DemoService
@inject ICsvImportService CsvImportService
@inject ILanguageService LanguageService
...
@code {
    [Parameter]
    public required string Lang { get; set; }

    protected override void OnParametersSet()
    {
        text =  LanguageService.GetLocalizedStrings("AppName.Client.Localization.Strings.Demo.csv", Lang ?? Constants.SupportedLanguages[0]);
    }
...
}

The services are registered in Client's Program.cs like this:

string apiUrl = "https://localhost:7254"; // TODO move to appsettings.json
string apiVersion = "/api/v1";

builder.Services.AddRefitClient<IDemoService>()
    .ConfigureHttpClient(c => c.BaseAddress = new Uri($"{apiUrl}{apiVersion}"));

builder.Services.AddScoped<IEmbeddedCsvService, EmbeddedCsvService>();
builder.Services.AddScoped<ILanguageService, LanguageService>();
builder.Services.AddScoped<ICsvImportService, CsvImportService>();

Usually, everything works fine. But sometimes, for example when I manually change the language code in the URL, the browser shows me this error:

InvalidOperationException: Cannot provide a value for property 'DemoService' on type 'AppName.Client.Features.Calculation.Demo.Page.Demo'. There is no registered service of type 'AppName.Client.Features.Calculation.Demo.Service.IDemoService'.

Suddenly, the service is not registered, but when the page is loaded and I use it to call the API, everything works. Should I change the way I deal with language versions?

EDIT: It happens even with other services unrelated to Refit and even if I keep the language version unchanged.

I have a Blazor app with WebAssembly render mode. In my Client project, I have this page:

@page "/demo"
@page "/{lang}/demo"
@inject IDemoService DemoService
@inject ICsvImportService CsvImportService
@inject ILanguageService LanguageService
...
@code {
    [Parameter]
    public required string Lang { get; set; }

    protected override void OnParametersSet()
    {
        text =  LanguageService.GetLocalizedStrings("AppName.Client.Localization.Strings.Demo.csv", Lang ?? Constants.SupportedLanguages[0]);
    }
...
}

The services are registered in Client's Program.cs like this:

string apiUrl = "https://localhost:7254"; // TODO move to appsettings.json
string apiVersion = "/api/v1";

builder.Services.AddRefitClient<IDemoService>()
    .ConfigureHttpClient(c => c.BaseAddress = new Uri($"{apiUrl}{apiVersion}"));

builder.Services.AddScoped<IEmbeddedCsvService, EmbeddedCsvService>();
builder.Services.AddScoped<ILanguageService, LanguageService>();
builder.Services.AddScoped<ICsvImportService, CsvImportService>();

Usually, everything works fine. But sometimes, for example when I manually change the language code in the URL, the browser shows me this error:

InvalidOperationException: Cannot provide a value for property 'DemoService' on type 'AppName.Client.Features.Calculation.Demo.Page.Demo'. There is no registered service of type 'AppName.Client.Features.Calculation.Demo.Service.IDemoService'.

Suddenly, the service is not registered, but when the page is loaded and I use it to call the API, everything works. Should I change the way I deal with language versions?

EDIT: It happens even with other services unrelated to Refit and even if I keep the language version unchanged.

Share edited Feb 11 at 21:54 ZdenekF asked Feb 11 at 17:05 ZdenekFZdenekF 52 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

During Static Server rendering,it requires services from the service container in server side

As stated in the document,the two following approaches would both resolve this problem:

Register the service in the main project to make it available during component prerendering.

If prerendering isn't required for the component, disable prerendering by following the guidance in ASP.NET Core Blazor render modes. If you adopt this approach, you don't need to register the service in the main project.

Solved by registering these services in both Client's and Server's Program.cs.

本文标签: cBlazor WebAssembly returns quotno registered servicequot even though service is registeredStack Overflow