JSON based configuration

The JSON-file was the first supported way of configuring API Framework and given its combination of flexibility and easy-to-use, it's the ideal choice for many projects.

The JSON-file was the first supported way of configuring API Framework and given its combination of flexibility and easy-to-use, it's the ideal choice for many projects. JSON-configuration provides the option of changing things without having to rebuild and publish a newer version.

Loading the configuration

To read a configuration from JSON-file, modify Program.cs to include an additional configuration file:

public static IWebHostBuilder CreateHostBuilder(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .AddApiFrameworkJsonConfigurationFile()
        .UseStartup<Startup>();
}

By default, a file called "apiframework.json" is read. That can be changed by giving the filename to AddApiFrameworkJsonConfigurationFile:

public static IWebHostBuilder CreateHostBuilder(string[] args)
{
    return WebHost.CreateDefaultBuilder(args)
        .AddApiFrameworkJsonConfigurationFile("myconfiguration.json")
        .UseStartup<Startup>();
}

Configuration

When the configuration file is used, there's no need to define APIs and Endpoints in code. We only have to register API Framework itself:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddApiFrameworkStarterKit();
}

The following configuration example shows how APIs and Endpoints can be configured:

{
	"ApiFramework": {
		"Apis": [{
				"Name": "Weikio.ApiFramework.Plugins.SqlServer",
				"Version": "1.2.1-alpha.0.1"
			},
			{
				"Name": "Weikio.ApiFramework.Plugins.OpenApi",
				"Version": "1.0.0-beta.1"
			}
		],
		"Endpoints": {
			"/adventures": {
				"Api": "Weikio.ApiFramework.Plugins.SqlServer",
				"ApiVersion": "1.1.0.0",
				"Configuration": {
					"ConnectionString": "Server=tcp:adafydevtestdb001.database.windows.net,1433;User ID=docs;Password=3h1@*6PXrldU4F95;Integrated Security=false;Initial Catalog=adafyweikiodevtestdb001;",
					"Tables": [
						"Product*"
					]
				}
			},
			"/petstore": {
				"Api": "Weikio.ApiFramework.Plugins.OpenApi",
				"Configuration": {
					"SpecificationUrl": "https://petstore3.swagger.io/api/v3/openapi.json",
					"ApiUrl": "https://petstore3.swagger.io/api/v3",
					"IncludeHttpMethods": [
						"GET"
					]
				}
			}
		}
	}
}

Sample

Sample for using JSON-configuration file is available from GitHub: https://github.com/weikio/ApiFramework.Samples/tree/main/misc/JsonConfiguration