AuthProvider and Multitenancy

I am using the AuthFeature and I feed it a custom CredentialsAuthProvider derived provider of my own (i.e. CustomProvider : CredentialsAuthProvider).

Like this:

appHost.Container.RegisterAutoWiredAs<MyDependency, IMyDependency>().ReusedWithin(ReuseScope.Request);

appHost.Plugins.Add(new AuthFeature(() => new AuthUserSession(), new IAuthProvider[]
            {
                new CustomProvider(
appHost.Container.Resolve<IAppSettings>(), 
appHost.Container.Resolve<IMyDependency>())
            }));

One of the dependencies of my CustomProvider is auto-wire-registered as ReuseScope.Request, but it is passed into the ctor of CustomProvider. As you can see above.

Clearly, the instance of this dependency is not being properly re-constructed every HTTP Request, and I am having problems each request.

How should I be injecting my dependency into my CustomProvider so that it will be resolved/recreated every HTTP Request?

You can’t AuthProviders are singleton instances.

You’d need to do something like pass in a factory lambda or fetch it from HostContext.TryResolve<T> inside your implementation on each request.

Is there a way if I derive from IAuthWithRequest? (Bearing in mind my CustomProvider derives from ServiceStack.Auth.CredentialsAuthProvider)

Could you tell me more about how to do that?
(Using the HostContext.TryResolve<T> is hard to mock I think.)

HostContext.TryResolve<T> just resolves from your AppHost Container so you can register it with any dependencies,

For the lambda factory I mean to pass in a factory you can call to retrieve the dependency and not the dependency itself, e.g:

new CustomProvider(
    () => appHost.Container.Resolve<IAppSettings>(), 
    () => appHost.Container.Resolve<IMyDependency>())

Both options are doing the same thing, i.e. retrieving the Dependency from the IOC at runtime instead of holding onto a resolved instance at startup.

Gotcha, thanks. that worked
(found a way to make HostContext.TryResolve<>() testable as well).