在 SignalR Core 中的 Hub 外部访问或注入 HubCallerContext
Access or Inject HubCallerContext outside of Hub in SignalR Core
我有一个带有 signalR 实现的 asp-net-core 项目。当在我的集线器中调用方法时,我需要从 Context.User
中提取用户信息。问题是,当正在构建的集线器 Context.User
不包含用户信息时。但是在方法范围内, Context.User
正是我所期望的。
public class Basehub : Hub
{
public Basehub(IUserProfileProvide userProfileProvider)
{
this.CurrentUser = userProfileProvider.InitUserProfile(Context); // Context.User is empty when breakpoint hits this line
}
public IUserProfile CurrentUser {get;}
}
public class NotificationHub: BaseHub
{
private IUserProfileProvide userProfileProvider;
public NotificationHub(IUserProfileProvide userProfileProvider)
{
}
public async Task InvokeMe(string message)
{
var contextUser = Context.User;
var profile = CurrentUser;//this is empty because Context is empty in the construction phase
await Clients.All.SendAsync("invoked",message); // Context.User is OK when breakpoint hits this line
}
}
我的主要目标是将 HubCallerCOntext
注入 IUserProfileProvide
并且 BaseHub
将尽可能干净。
*我的问题: 如何在 hub 外部注入 HubCallerContext
?
调用构造函数时上下文尚不可用。
它将在调用预期函数时填充。
public class Basehub : Hub {
protected IUserProfileProvide userProfileProvider;
public Basehub(IUserProfileProvide userProfileProvider) {
this.userProfileProvider = userProfileProvider;
}
}
在流程的后期推迟对其的访问,例如在框架有时间正确填充上下文的方法中。
public class NotificationHub: BaseHub {
public NotificationHub(IUserProfileProvide userProfileProvider)
: base(userProfileProvider) { }
public async Task InvokeMe(string message) {
IUserProfile profile = userProfileProvider.InitUserProfile(Context); //context populated
//...
await Clients.All.SendAsync("invoked",message);
}
}
我有一个带有 signalR 实现的 asp-net-core 项目。当在我的集线器中调用方法时,我需要从 Context.User
中提取用户信息。问题是,当正在构建的集线器 Context.User
不包含用户信息时。但是在方法范围内, Context.User
正是我所期望的。
public class Basehub : Hub
{
public Basehub(IUserProfileProvide userProfileProvider)
{
this.CurrentUser = userProfileProvider.InitUserProfile(Context); // Context.User is empty when breakpoint hits this line
}
public IUserProfile CurrentUser {get;}
}
public class NotificationHub: BaseHub
{
private IUserProfileProvide userProfileProvider;
public NotificationHub(IUserProfileProvide userProfileProvider)
{
}
public async Task InvokeMe(string message)
{
var contextUser = Context.User;
var profile = CurrentUser;//this is empty because Context is empty in the construction phase
await Clients.All.SendAsync("invoked",message); // Context.User is OK when breakpoint hits this line
}
}
我的主要目标是将 HubCallerCOntext
注入 IUserProfileProvide
并且 BaseHub
将尽可能干净。
*我的问题: 如何在 hub 外部注入 HubCallerContext
?
调用构造函数时上下文尚不可用。
它将在调用预期函数时填充。
public class Basehub : Hub {
protected IUserProfileProvide userProfileProvider;
public Basehub(IUserProfileProvide userProfileProvider) {
this.userProfileProvider = userProfileProvider;
}
}
在流程的后期推迟对其的访问,例如在框架有时间正确填充上下文的方法中。
public class NotificationHub: BaseHub {
public NotificationHub(IUserProfileProvide userProfileProvider)
: base(userProfileProvider) { }
public async Task InvokeMe(string message) {
IUserProfile profile = userProfileProvider.InitUserProfile(Context); //context populated
//...
await Clients.All.SendAsync("invoked",message);
}
}