从 SignalR Hub 中的 AbpSession 获取 UserId
Get the UserId from AbpSession in SignalR Hub
我无法在实现 ITransientDependency
.
的 class 中从 AbpSession
获取 UserId
public class Chat : Hub, ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public ILogger Logger { get; set; }
public Chat()
{
AbpSession = NullAbpSession.Instance;
Logger = NullLogger.Instance;
}
private static List<MemberDto> _members = new List<MemberDto>();
public Task Send(string message)
{
return Clients.All.InvokeAsync("Send", string.Format("User {0}: {1}", UserId(), message));
}
public long? UserId()
{
return AbpSession.UserId;
}
}
属性 UserId
和 TenantId
仍然是 null
。
Chat
class 在 "Application" 层内,其中 AbpSession
运行良好。
我认为问题可能与您正在使用的 属性 注入有关:
public IAbpSession AbpSession { get; set; }
虽然 SignalR 使用 DI,但它不会从 DI 容器创建聊天 class。这意味着 属性 注入不起作用。但是构造函数注入会起作用,因为它从 DI 容器传递 ctor 参数。
因此,使用构造函数注入而不是 属性 注入。
这适用于 SignalR AspNetCore Integration. I tried here:
public async Task SendMessage(string message)
{
await Clients.All.InvokeAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
您现在可以download v3.4.1
of the template with the Abp.AspNetCore.SignalR预览。
更新
您可能没有像 SignalRAspNetCoreHelper.ts 那样传入 encryptedAuthToken
:
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
var qs = AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken);
然后将其作为查询字符串传递到 SignalR 集线器的 URL 中。
我无法在实现 ITransientDependency
.
AbpSession
获取 UserId
public class Chat : Hub, ITransientDependency
{
public IAbpSession AbpSession { get; set; }
public ILogger Logger { get; set; }
public Chat()
{
AbpSession = NullAbpSession.Instance;
Logger = NullLogger.Instance;
}
private static List<MemberDto> _members = new List<MemberDto>();
public Task Send(string message)
{
return Clients.All.InvokeAsync("Send", string.Format("User {0}: {1}", UserId(), message));
}
public long? UserId()
{
return AbpSession.UserId;
}
}
属性 UserId
和 TenantId
仍然是 null
。
Chat
class 在 "Application" 层内,其中 AbpSession
运行良好。
我认为问题可能与您正在使用的 属性 注入有关:
public IAbpSession AbpSession { get; set; }
虽然 SignalR 使用 DI,但它不会从 DI 容器创建聊天 class。这意味着 属性 注入不起作用。但是构造函数注入会起作用,因为它从 DI 容器传递 ctor 参数。
因此,使用构造函数注入而不是 属性 注入。
这适用于 SignalR AspNetCore Integration. I tried here:
public async Task SendMessage(string message)
{
await Clients.All.InvokeAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
}
您现在可以download v3.4.1
of the template with the Abp.AspNetCore.SignalR预览。
更新
您可能没有像 SignalRAspNetCoreHelper.ts 那样传入 encryptedAuthToken
:
var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);
var qs = AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken);
然后将其作为查询字符串传递到 SignalR 集线器的 URL 中。