Use of IAuthRepository outside AppHost


I have an API hosted with ServiceStack, connecting to a Microsoft Orleans backend.

In this example, webhooks are being delivered to ServiceStack, and then being passed on into my other Orleans application (separate executable). I have registered the IAuthRepository as follows, and can resolve this:

            Component.For<IDbConnectionFactory>().UsingFactoryMethod(x => new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider)),

But when I call the CreateAuth using the following:

_authRepository.CreateUserAuth(userAuth, "password");

I get the following exception:

'ServiceStack: AppHost does not exist or has not been initialized.
Make sure you have created an AppHost and started it with 'new AppHost().Init();'  in your Global.asax Application_Start() or alternative Application StartUp'

Do I really need an AppHost started in this other application? I just want to be able to create users here. I could move this up to the ServiceStack layer but other operations are happening here that need to happen together.

Yeah a lot of ServiceStack functionality is dependent on having an initialized AppHost available, but if you’re not hosting any Services you can initialize an empty one with:

using (new BasicAppHost().Init())

Understood Mythz, I assume I can change my resolve to the following?

Component.For<IAuthRepository>().UsingFactoryMethod(x => ServiceStackHost.Instance.GetAuthRepository())

I seem to get strange error trying to initialize a basic AppHost:

public class EmbeddedAppHost : BasicAppHost
    public override void Configure(Funq.Container container)
        var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
        container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
        container.Register<IUserAuthRepository>(c => new CustomAuthRepository(c.Resolve<IDbConnectionFactory>()));

_appHost = new EmbeddedAppHost(); _appHost.Init();

'Could not register Request 'ServiceStack.NativeTypes.TypeLinks' with service 'ServiceStack.NativeTypes.NativeTypesService' as it has already been assigned to another service.
Each Request DTO can only be handled by 1 service.'

Note that I am not creating or referencing any other AppHost in this executable.

You can only have 1 AppHost initialized at the same time and if you don’t pass in an assembly for ServiceStack to scan for Services it will fallback to use Assembly.GetExecutingAssembly(). You could give it another assembly to scan or you could remove all plugins and prevent them registering any Services with:


Thanks - I’m definitely not running any other AppHost in this exe but I did have break on all exceptions from debugging something else, this is caught internally and does not cause a problem.

This now resolved - thank you very much for your time and lightning quick response as always.

1 Like

FYI I’ve just cleared the plugins from BasicAppHost by default so you wont have to explicitly do it from v4.5.9 that’s on MyGet and future versions.