Search code examples
c#.netservicestackhttpserver

ServiceStack AuthFeature null reference exception


I have a simple servicestack self-host app which aside of everything else tries to use authentication. In AppHost constructor i make a call

                Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[]
                {
                    new RoomsAuthProvider(),
                }));

but i always get null reference exception during that call. I've tried separating the calls - create the auth feature in one string, add in another - AuthFeature creation fails. I've also tried calling

                Container.Register(new MemoryCacheClient());

This changes nothing. Any ideas are welcome.Stacktrace attached

ServiceStack.AuthFeature.<.ctor>b__a(String s)
ServiceStack.AuthFeature..ctor(Func`1 sessionFactory, IAuthProvider[] authProviders, String htmlRedirect)
CoreServer.Program.AppHost..ctor() в C:\Users\Sorrow\documents\visual studio 2015\Projects\RoomsServicestack\CoreServer\Program.cs
CoreServer.Program.Main(String[] args) в C:\Users\Sorrow\documents\visual studio 2015\Projects\RoomsServicestack\CoreServer\Program
System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
System.Threading.ThreadHelper.ThreadStart()

Solution

  • In AppHost constructor i make a call

    Don't register any plugins inside the AppHost constructor, all configuration should happen within AppHost.Configure(), e.g:

    public override void Configure(Container container)
    {
        Plugins.Add(new AuthFeature(() => new AuthUserSession(),
            new IAuthProvider[] {
                new RoomsAuthProvider(),
            }));
    }
    

    This is unrelated to your issue, but if you want to register a Caching Provider you need to register against the ICacheClient interface, e.g:

    Container.Register<ICacheClient>(new MemoryCacheClient());
    

    Which will let you resolve the dependency via the ICacheClient interface:

    var cache = Container.Resolve<ICacheClient>();
    

    This is needed because any built-in Service that uses caching resolves the registered ICacheClient dependency.