在 razor 页面中调用接口方法抛出未处理的异常

Calling interface method in razor page throws unhandled exception

未处理的异常渲染组件:

Cannot provide a value for property _accountService on type Inventory_Management.Client.Pages.Authentication.Login. There are no registered service of type Inventory_Management.Shared.Services.AccountService.

AccountService.cs

  public interface IAccountService
    {
        Task<IResult> Login(Identity model);
    }
    public class AccountService : IAccountService
    {
        public async Task<IResult> Login(Identity model){}
    }

login.razor

@inject IAccountService _accountService;
<div></div>
@code{
private async Task Submit()
    {
        var result = await _accountService.Login(userlogin); // Unhandled exception
    }
}

遵循 Github 示例,它在启动 class 中没有任何作用域。 https://github.com/iammukeshm/CleanArchitecture.WebApi.

参考资料

Client-> Pages-> Authentication-> Login.razor.cs
Infrastructure-> Identity-> Authentication->IAuthenticationManager
Server-> Extensions -> ServiceCollectionExtension.cs
startup.cs -> services.AddServerLocalization();

您需要在 Startup class 的 ConfigureServices 方法中注册您的具体 class。请尝试以下操作。

public void ConfigureServices(IServiceCollection services){
services.AddScoped<IAccountService, AccountService>();
}

您需要 IAccountService(在您尝试遵循的示例中 - 请参阅 ServiceExtensions.AddIdentityInfrastructure and call to it in the Startup class)注册,例如使用;

services.AddScoped<IAccountService, AccountService>(); // also you should register all AccountService's dependencies

然后解析接口,而不是实现:

@inject IAccountService _accountService;
<div></div>
@code{
private async Task Submit()
    {
        var result = await _accountService.Login(userlogin); // Unhandled exception
    }
}