Connection is closing when using as a dependency

Hi, I am using OrmLiteAuthRepositoryMultitenancy to allow me to move my app to a multi-tenant architecture. I have wired it up as per the details in your docs. However, when I use IAuthRespository.GetUserAuth in other classes as a dependency I get:

‘ExecuteReader requires an open and available Connection. The connection’s current state is closed.’

I am using IAuthRepository as a standard property dependency, what am I doing wrong?

public class UserDataAppender : IUserDataAppender
{
    private ConcurrentDictionary<string, IUserAuth> _retrievedUsers;
    public IAuthRepository AuthRepository { get; set; }
    public UserDataAppender()
    {
        _retrievedUsers = new ConcurrentDictionary<string, IUserAuth>();
    }

    private IUserAuth GetUser(string id)
    {
        if (id == null) return null;
        if (_retrievedUsers.ContainsKey(id.ToString()))
            return _retrievedUsers[id.ToString()];
        else
        {
            **var user = AuthRepository.GetUserAuth(id.ToString());**
            _retrievedUsers.TryAdd(id.ToString(), user);
            return user;
        }
    }
}

my AppHost.GetAuthRepository looks like (RoleBasedAuthRepository derives from OrmLiteAuthRepositoryMultiTenancy and just concatenates permissions based on roles, cretainly not closing any connection).

 public override IAuthRepository GetAuthRepository(IRequest req = null)
        {
            return req != null
                ? new RoleBasedAuthRepository(GetDbConnection(req)) //At Runtime
                : TryResolve<IAuthRepository>();                              //On Startup
        }

Please close this, found the problem, I was using it via a globalrequestfitler and not calling the GetUserAuth appropriately.

The OrmLiteAuthRepositoryMultiTenancy is a per-instance dependency that should be ultimately retrieved from AppHost.GetAuthRepository() either by injecting the same base.AuthRepository from your Service into UserDataAppender or your dependencies can fetch it from the AppHost directly, e.g:

using (var authRepo = HostContext.AppHost.GetAuthRepository(httpReq)
{
    //...
}

But in this case they’d still need the IRequest context which you’d get from your Service, so it’s preferred to just pass base.AuthRepository into your dependencies that need it.