c# - SignalR - Multi-Tenancy 依赖注入(inject)

标签 c# dependency-injection signalr autofac multi-tenant

我需要根据租户的owin值解析DbContext。但是在hub的OnDisconnected方法的管道中,HttpContext是not accessible .

我的集线器类:

public class UserTrackingHub : Hub
{
    private readonly UserContext _context;

    public UserTrackingHub(UserContext context) { ... }

    public override async Task OnConnected() { /* OK HERE...*/ }

    public override async Task OnDisconnected(bool stopCalled)
    {
        // NEVER FIRES WITH IF I USE THE CTOR INJECTION.

        var connection = await _context.Connections.FindAsync(Context.ConnectionId);
        if (connection != null)
        {
            _context.Connections.Remove(connection);
            await _context.SaveChangesAsync();
        }
    }
}

这是我的 Autofac 配置:

    public static IContainer Register(IAppBuilder app)
    {
        var builder = new ContainerBuilder();

        // Other registers...

        builder.Register<UserContext>(c =>
        {    
            // Details and conditions omitted for brevity.

            var context = HttpContext.Current; // NULL in OnDisconnected pipeline.
            var owinContext = context.GetOwinContext();
            var tenant = owinContext.Environment["app.tenant"].ToString();
            var connection = GetConnectionString(tenant); 

            return new UserContext(connection);
        });

        var container = builder.Build();
        var config = new HubConfiguration
        {
            Resolver = new AutofacDependencyResolver(container)
        };

        app.MapSignalR(config);

        return container;
    }

有人可以帮助我以这种方式或任何其他方式识别租户 OnDisconnected 吗?
谢谢!

最佳答案

对于任何感兴趣的人,我最终会注入(inject)一个上下文工厂而不是上下文本身:

public class UserTrackingHub : Hub
{
    private readonly Func<string, UserContext> _contextFactory;

    public UserTrackingHub(Func<string, UserContext> contextFactory) { ... }

    public override async Task OnConnected() { ... }

    public override async Task OnDisconnected(bool stopCalled)
    {
        var tenant = Context.Request.Cookies["app.tenant"].Value;

        using (var context = _contextFactory.Invoke(tenant))
        {
            var connection = await context.Connections.FindAsync(Context.ConnectionId);
            if (connection != null)
            {
                context.Connections.Remove(connection);
                await context.SaveChangesAsync();
            }
        }
    }
}

以及 Autofac 的配置:

 // Resolve context based on tenant
 builder.Register<Func<string, UserContext>>(c => new Func<string, UserContext>(tenant => UserContextResolver.Resolve(tenant)));

关于c# - SignalR - Multi-Tenancy 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527034/

相关文章:

c# - Windows 7 中的 FolderBrowserDialog 行为

json - 有没有办法自定义 Azure Function SignalR 服务绑定(bind) (Microsoft.Azure.WebJobs.Extensions.SignalRService) 的 json 设置?

SignalR 无法调用非委托(delegate)类型

c# - 我可以使用 StructureMap 返回特定类型参数的通用接口(interface)的所有实现吗

c# - Windows Server 2008 R2 共同托管遗留 .Net 应用程序与 .Net Core 应用程序

android - 主构造函数的 Kotlin Dagger2 错误

ios - 台风:将运行时参数注入(inject)单例

signalr - 如何从服务器调用 SignalR 集线器操作

c# - Linq 延迟操作

c# - 作为装饰者登录 vs. 依赖注入(inject)——如果我需要在类里面登录怎么办?