You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ServiceStack only allows a single App Host for each App Domain. As you might be able to infer from the name, the role of the Host project is to be the conduit for binding all your services concrete dependencies, plugins, filters and everything else your service needs. The configuration of your service should be immutable after everything is initialized in your AppHost.Configure() method. The Physical project structure wiki page wiki shows the recommended physical project structure for typical solutions.
Modularizing services in multiple assemblies
Whilst you can only have 1 AppHost, you can have multiple services spread across multiple assemblies. In that case you have to provide a list of assemblies containing the services to the AppHostBase constructor, e.g:
publicclassAppHost:AppHostBase{//Tell ServiceStack the name of your app and which assemblies to scan for servicespublicAppHost():base("Hello ServiceStack!",typeof(ServicesFromDll1).Assembly,typeof(ServicesFromDll2).Assembly/*, etc */){}publicoverridevoidConfigure(Containercontainer){}}
You can also provide your own strategy for discovering and resolving the service types that ServiceStack should auto-wire by overriding CreateServiceManager, e.g:
publicclassAppHost:AppHostBase{publicAppHost():base("Hello ServiceStack!",typeof(ServicesFromDll1).Assembly){}publicoverridevoidConfigure(Containercontainer){}//Provide Alternative way to inject IOC Container + Service Resolver strategyprotectedoverrideServiceManagerCreateServiceManager(paramsAssembly[]assembliesWithServices){returnnewServiceManager(newContainer(),newServiceController(()=>assembliesWithServices.ToList().SelectMany(x =>x.GetTypes())));}}
Encapsulating Services inside Plugins
One way of modularizing services is to encapsulate them inside Plugins which allows you to manually register services, custom routes, filters, content types, allow customization and anything else your module needs.
To illustrate this point, we'll show what a Basic Auth Feature example might look like:
publicclassBasicAuthFeature:IPlugin{publicstringHtmlRedirect{get;set;}//User-defined configurationpublicvoidRegister(IAppHostappHost){//Register Services exposed by this moduleappHost.RegisterService<AuthService>("/auth","/auth/{provider}");appHost.RegisterService<AssignRolesService>("/assignroles");appHost.RegisterService<UnAssignRolesService>("/unassignroles");//Load dependent pluginsappHost.LoadPlugin(newSessionFeature());}}
With everything encapsulated inside a plugin, your users can easily enable them in your AppHost with: