简体   繁体   English

SignalR - 多租户依赖注入

[英]SignalR - Multitenant Dependency Injection

I need to resolve a DbContext based on tenant's owin value.我需要根据租户的 owin 值解析 DbContext。 But in the pipeline of method OnDisconnected of hub, the HttpContext is not accessible .但是在 hub 的 OnDisconnected 方法的管道中, HttpContext 是不可访问的

My hub class:我的枢纽班:

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();
        }
    }
}

Here's my Autofac config:这是我的 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;
    }

Can someone help me to identify the tenant OnDisconnected in this or any other way?有人可以帮助我以这种方式或任何其他方式识别租户 OnDisconnected 吗?
Thanks!谢谢!

For anyone interested, I end up injecting a context factory instead the context itself:对于任何感兴趣的人,我最终会注入一个上下文工厂而不是上下文本身:

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();
            }
        }
    }
}

And Autofac's config:和 Autofac 的配置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM