【发布时间】:2022-08-20 14:59:30
【问题描述】:
//this is UserService file
using System.Security.Claims;
namespace _HALKA.Services
{
public class UserService : IUserService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public UserService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string GetMyName()
{
var result = string.Empty;
if (_httpContextAccessor.HttpContext != null)
{
result = _httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.Name);
}
return result;
}
}
}
//this is IUserService file
namespace _HALKA.Services
{
public interface IUserService
{
string GetMyName();
}
}
//And this is program.cs file
builder.Services.AddScoped<IUserService, UserService>();
我从 \"var app = builder.Build();\" 收到此错误:
System.AggregateException:\'无法构造某些服务(验证服务描述符时出错\'ServiceType:_HALKA.Services.IUserService Lifetime:Scoped ImplementationType:_HALKA.Services.UserService\':无法解析服务类型\ 'Microsoft.AspNetCore.Http.IHttpContextAccessor\' 尝试激活 \'_HALKA.Services.UserService\'。)\'
-
什么以及如何解决
IUserService?