How to use OrmLite AppSettings?

I’m having a hard time figuring out how to use OrmLite AppSettings.

I have registered the following:

container.Register(c => 
    new OrmLiteAppSettings(c.Resolve<IDbConnectionFactory>()));
//Create the ConfigSettings table if it doesn't exist
container.Resolve<OrmLiteAppSettings>().InitSchema(); 

This created a table called config_setting in the database with id and value columns.

I am then trying to use it from within a Service, which is the part I’m not understanding. The provided example at https://docs.servicestack.net/appsettings#usage shows:

var config = appSettings.Get("config", 
    new MyConfig { Key = "DefaultValue" });

From within the Service I have added

public IAppSettings AppSettings { get; set; }

I have a class defined as:

public class AppConfig
{
    public int MaxFailedAccessAttempts { get; set; }
    public bool RequireEmailConfirmation { get; set; }
}

This is where I’m getting stuck. How do I modify

var config = appSettings.Get("config", 
    new MyConfig { Key = "DefaultValue" });

to populate my AppConfig class from within the service using AppSettings?

You’d use Set<T> to save a typed POCO Config that you’d retrieve with Get<T>, e.g:

appSettings.Set("config", new AppConfig {
    MaxFailedAccessAttempts = 1,
    RequireEmailConfirmation = true
});

Getting closer

I did the following

AppSettings.Set("config", new AppConfig
{
    MaxFailedAccessAttempts = 1,
    RequireEmailConfirmation = true
});
var config = AppSettings.Get<AppConfig>("config");

While doing these back to back works, if I remove the .Set call, var config is null. It doesn’t seem to be writing this to the database.

What am I missing?

You’re likely not assigning your AppHost’s AppSettings, make sure the Type of AppSettings is OrmLiteAppSettings, if you didn’t you need to configure your AppHost.AppSettings to use it, i.e:

AppSettings = container.Resolve<OrmLiteAppSettings>();
1 Like