我想知道有没有办法创建可重用的HttpContextaccessor当前用户ID。
至于现在我只是将它直接注入我的服务类,但从这项服务的可重用性方面来看很糟糕。
此时的代码:
private readonly IServiceScopeFactory _scopeFactory;
public CurrentUserIdService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public string CurrentUserId()
{
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetService<HttpContextAccessor>();
return context.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
}
}
我尝试使用Scope Factory来调用HttpContextAccessor,但它返回null
./config shared CFLAGS=-fPIC
有关如何使其可重复使用的任何提示吗?