Web .Net Core 2.0 AppSettings.json

I’ve looked at this issue regarding .net core and setting the licence information on stackoverflow and was wondering if there has been any updates to SS to get it to look at the AppSettings,json file for the licence information? I dont want to be having two types of config files within the project.

You can use .NET Core’s IConfiguration or the NetCoreAppSettings IAppSettings adapter to read config from .NET Core’s appsettings.json, e.g:

public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration) => Configuration = configuration;

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        var appSettings = new NetCoreAppSettings(Configuration);
        Licensing.RegisterLicense(appSettings.GetString("servicestack:license"));
        app.UseServiceStack(new AppHost {
            AppSettings = appSettings
        });
    }
}

But I’ve just added a change which will auto register the license key stored in servicestack:license appSetting in this commit, so you can register your key in the “servicestack:license” in appsettings.json:

{
    "servicestack:license": "{LICENSE_KEY}"
}

and register your AppHost with the NetCoreAppSettings adapter:

app.UseServiceStack(new AppHost
{
    AppSettings = new NetCoreAppSettings(Configuration)
});

and ServiceStack will auto register the license key during initialization.

This change is available from v5.0.3 that’s now available on MyGet.

Although my preference is till to register the SERVICESTACK_LICENSE environment variable once on my workstation and the servers where apps are deployed as it applies to all ServiceStack projects.

There is an incompatibility between this and the IConfuguration of .net core in such that normally when you would get ServiceStack:License with .net core, it would expect:

"ServiceStack": {
  "License": "value"
}

But then the AppSettings from ServiceStack is not reading this… Is this something you would change in a future version?

From v5.5 the key now follows .NET Core’s Configuration API conventions:

{
    "servicestack" : {
        "license": "{LICENSE_KEY}"
    }
}

Hey, I found this post concerning the AppSettings.json some what confusing and I wanted to clarify the code for setting it using the AppSettings.json in case somebody else comes here. Please let me know if I am incorrect.
mythz posted that it needed to be like this:

{
“servicestack:license”: “{LICENSE_KEY}”
}
in the appsettings.json file. I couldn’t make this work since the format is “KEY” : { “Value”: “”} at least in my version of Visual Code. So the code

var appSettings = new NetCoreAppSettings(Configuration);
Licensing.RegisterLicense(appSettings.GetString(“servicestack:license”));

Would not find the key since its looking for the other format. So, if you create it like this:

“servicestack” : {
“license”: “licenses”
}
It works with the previous code.

Yeah this behavior changed in order to improve compatibility with accessing nested keys in AppSettings where the : object notation adopts the same behavior in .NET Core appsettings.json so it now needs to be the same as .NET Core Configuration API, i.e:

{
    "servicestack" : {
        "license": "{LICENSE_KEY}"
    }
}

Where ServiceStack would automatically register it, (i.e. without needing to manually register it yourself).

The alternative is to use your own key without the ‘:’, e.g:

{
    "servicestack_license": "{LICENSE_KEY}"
}

Then you can register the key as a normal string app setting, e.g:

Licensing.RegisterLicense(AppSettings.GetString("servicestack_license"));

I’ll have to update the docs.