Getting Started - Configuring your AppHost

Different ways for getting started with ServiceStack:
Project templates, Walkthroughs, Docs and Videos, choose what's best for you

Registering IoC Dependencies

In your application's AppHost, you will see a Configure(Container container) method which is fired during your application startup and is the location that should contain all your configuration, including registering resources you want easy access to in your Services.

ServiceStack uses a modified version of Funq as the default Inversion of Control (IoC) container. This the primary way to share code across services as well as configuration your might need.

ServiceStack defaults to public property injection of registered IoC dependencies. This means for if you want a service class to take a dependency on one of your registered IoC instances, you just need to declare a public property with the matching type and the AppHost will wire up the dependency.

Add features via Plugins

ServiceStack has an easy way to add packaged up functionality into your application with the use of the Plugin API. Within your AppHost.Configure method, Plugins can be added using the function Plugins.Add which takes an instance of an IPlugin.

ServiceStack automatically registers several plugins by default including CSV Format, HTML Format, and Predefined Routes but also has a range of other Plugins available to add functionality to configure and add to your application with minimal fuss.

You can create your own Plugins using the IPlugin interface as a way to isolate a feature set you have built which can help if you need to share features among multiple hosts.

Manage AppSettings

ServiceStack has several ways to make it easy to populate application configuration from various data sources into a flexible IAppSettings instance including Web.config, database, environment variables, text files and more.

A MultiAppSettings builder is also provided which creates a cascading configuration that checks various sources in priority order. AppSettings provides a good base for your own custom strongly typed class of settings that can be initialized from AppSettings and then shared in your application as your own custom settings class.

AppSettings can be registered and used like any other auto wired dependency in your IoC container to make it easy to share runtime configuration among your services. This creates an easy to manage a central place for your application settings that can be used anywhere in your application.

Mix in functionality using templates

A great way to speed up development is to use the ServiceStack dotnet x tool to create new projects as well as mix in additional functionality. Once installed, using the command x mix will show a list of features that can be mixed into an existing application.

For example, using the command x mix sqlite in the root of your project folder will add the required ServiceStack SQLite related dependencies to your project as well as a startup module Configure.Db.cs file with all the required code to incorporate a SQLite OrmLite connection straight into your application.

The dotnet x tool is cross platform, and can be installed using the command dotnet tool install -g x.

Register a Database connection

ServiceStack's bundled Object Relational Mapper (ORM), OrmLite, is the standard way to register a RDBMS connect for the rest of your application to use. This can be done by registering an IDbConnectionFactory with your IoC container.

When registering an OrmLiteConnectionFactory, a Provider must be specified to match which vendor technology you are using. For example, if you are using PostgreSQL as your database backend, you'll need to add the ServiceStack.OrmLite.PostgreSQL dependency to your AppHost project and specify the PostgreSqlDialect.Provider.

Once the IDbConnectionFactory is registered, it can be resolved from IoC using container.Resolve<IDbConnectionFactory> to be used in other configuration as well as accessed in your services using the Db property helper to open a new database connection. Your other projects will only need the ServiceStack.OrmLite package to access the OrmLite API.

Register a Redis connection

ServiceStack also comes bundled with first class Redis client in the ServiceStack.Redis package. It provides several APIs including a native client, strongly typed client as well as integrating into your application as a cache provider.

Registering a Redis connection requires registering a IRedisClientManager with your IoC container. To take advantage of Redis as a caching provider you can register a factory method with your IoC container returning an ICacheClient from the client manager. The will enable use of the Cache and CacheAsync properties in your services.