我知道这个问题已经被问过了,但是我在这里找到的所有答案都对我不起作用,所以也许我在代码中的其他地方有一个愚蠢的错误。
我的问题是,正如您在错误中看到的那样,尝试激活我的控制器时无法解析服务文件。当我打开页首横幅网页时,就会发生这种情况。
我的代码:
控制器:
public class LeaderboardController : Controller
{
private readonly IIdentityService _identityService;
public LeaderboardController(IIdentityService identityService)
{
_identityService = identityService;
}
[HttpGet]
public async Task<IActionResult> Leaderboards()
{
return View(await _identityService.GetLeaderboardAsync());
}
}
服务接口(相关件):
public interface IIdentityService
{
Task<UsersDto> GetLeaderboardAsync();
}
服务(相关件):
public class IdentityService : IIdentityService
{
public async Task<UsersDto> GetLeaderboardAsync()
{
var userList = await _identityRepository.GetLeaderboardAsync();
var usersDto = userList.ToModel();
return usersDto;
}
}
存储库界面(相关部分):
public interface IIdentityRepository
{
Task<List<UserIdentity>> GetLeaderboardAsync();
}
存储库(相关件):
public class IdentityRepository : IIdentityRepository
{
public async Task<List<UserIdentity>> GetLeaderboardAsync()
{
return await _userManager.Users.OrderBy(x => x.Score).ToListAsync();
}
}
Startup.cs ConfigureServices方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContexts<StrikeNetDbContext>(Configuration);
services.AddIdentity<UserIdentity, RoleIdentity>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
}).AddEntityFrameworkStores<StrikeNetDbContext>();
services.AddStrikeNetServices<StrikeNetDbContext>();
services.AddControllersWithViews();
}
在[[Startup.cs]]中的services.AddScoped<IIdentityRepository, IdentityRepository>();
方法中添加ConfigureServices
似乎无效;我遇到了同样的错误,但是甚至没有进入网站。该网站崩溃了,并返回到我的VS代码,并给出了相同的错误。