StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 9 · Options and Feature Management

Reloadable Feature Settings

medium100%1 of 1 solved

Problem statement

Complete FeatureSettingsService so runtime configuration changes are honoured without restarting the host. The app exposes a Feature configuration section (welcome message, discount percent, and max items). Reading the settings, updating them, and quoting a discounted price must all reflect the current configuration — an update made through PUT /settings must be visible on the very next GET /settings and GET /quote. The trick is resolving options with IOptionsSnapshot<FeatureOptions> (recomputed per request) rather than IOptions<FeatureOptions> (cached once at startup). Validation lives in the service layer, not the controller.

Seeded Configuration

appsettings.json seeds the Feature section:

{
  "Feature": {
    "WelcomeMessage": "Welcome",
    "DiscountPercent": 10,
    "MaxItems": 5
  }
}

API Endpoints to Implement

  • GET /settings — return the current feature settings.
  • PUT /settings — validate and apply new settings, then reload configuration so the change is observed without a restart. Invalid input (blank welcome message, discount outside 0..100, or maxItems < 1) returns 400.
  • GET /quote?price= — apply the current discount to price (clamped at 0). A price <= 0 (including a missing value) returns 400.

Sample Requests & Responses

Expand to view details on sample requests and responses for each endpoint.

GET /settings

Response code: 200

{
  "welcomeMessage": "Welcome",
  "discountPercent": 10,
  "maxItems": 5
}

PUT /settings

Request:

{
  "welcomeMessage": "Hello there",
  "discountPercent": 25,
  "maxItems": 8
}

Response code: 200

{
  "welcomeMessage": "Hello there",
  "discountPercent": 25,
  "maxItems": 8
}

GET /quote?price=100 (after the update above)

Response code: 200

{
  "price": 100,
  "discountPercent": 25,
  "finalPrice": 75
}

PUT /settings with discountPercent = 150

Response code: 400

GET /quote (missing price)

Response code: 400

More in Options and Feature Management

See the module
Named Tenant OptionsThis problem covers giving each tenant its own set of delivery settings, read from that tenant's own part of the configuration, so that one tenant's values never replace another's and the settings the application starts with are left alone.—No data yetmediumOptions Change NotificationsThis problem covers keeping a long lived cache in step with settings that change while the application is running, so that what it serves follows the new settings and it reports how many times it has been refreshed.—No data yetmediumFeature Flag ServiceThis problem covers serving feature flags that stay the same for the length of one request, keeping a long lived reader up to date as those flags change, and stopping the application from starting when a flag set carries a value it is not allowed to carry.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.