Service Registration
Register all WildwoodComponents services in your application's DI container with a single call to AddWildwoodComponents().
Overview
WildwoodComponents uses a single initialization call to wire up all services. The approach varies by platform:
Use the AddWildwoodComponents() extension method on IServiceCollection
in your Program.cs. This registers authentication, AI, payments, messaging,
session management, theming, and all other services.
// Program.cs
builder.Services.AddWildwoodComponents(options =>
{
options.BaseUrl = "https://api.myapp.com";
options.AppId = "your-app-id";
options.EnableAutoTokenRefresh = true;
options.SessionExpirationMinutes = 60;
});
Wrap your app with WildwoodProvider. This creates a WildwoodClient
instance that provides all services via React context.
import { WildwoodProvider } from '@wildwood/react';
function App() {
return (
<WildwoodProvider config={{
baseUrl: 'https://api.myapp.com',
appId: 'your-app-id',
enableAutoTokenRefresh: true,
sessionExpirationMinutes: 60,
}}>
<Router />
</WildwoodProvider>
);
}
Access services via hooks: useAuth(), useTheme(), useNotifications(), etc.
Same provider pattern as React, with 'memory' storage by default (or pass an AsyncStorage adapter).
import { WildwoodProvider } from '@wildwood/react-native';
function App() {
return (
<WildwoodProvider config={{
baseUrl: 'https://api.myapp.com',
appId: 'your-app-id',
enableAutoTokenRefresh: true,
storage: 'memory',
}}>
<NavigationContainer>
{/* Your screens */}
</NavigationContainer>
</WildwoodProvider>
);
}
For server-side use, create middleware instances with your API key.
The @wildwood/core client can also be used directly for server-to-server calls.
import { createAuthMiddleware, createAdminClient } from '@wildwood/node';
// JWT validation middleware
const auth = createAuthMiddleware({
baseUrl: 'https://api.myapp.com',
apiKey: process.env.WILDWOOD_API_KEY,
});
// Server-side admin client
const admin = createAdminClient({
baseUrl: 'https://api.myapp.com',
apiKey: process.env.WILDWOOD_API_KEY,
});
Blazor Configuration Options
The .NET SDK has three overloads for AddWildwoodComponents():
| Overload | Use When |
|---|---|
AddWildwoodComponents(string baseUrl) |
You only need to specify the API URL and want sensible defaults for everything else. |
AddWildwoodComponents(Action<WildwoodComponentsOptions>) |
You want to configure options programmatically (API key, session settings, timeouts, etc.). |
AddWildwoodComponents(IConfiguration, string?) |
You want to read all options from appsettings.json or another configuration source. |
Option 1: Simple URL Registration
The simplest way to get started. Pass just the base URL of your WildwoodAPI instance:
// Program.cs
builder.Services.AddWildwoodComponents("https://localhost:7046");
This uses default values for all other settings. It is equivalent to:
builder.Services.AddWildwoodComponents(options =>
{
options.BaseUrl = "https://localhost:7046";
});
Option 2: Lambda Configuration
For full control over options, use the lambda overload:
// Program.cs
builder.Services.AddWildwoodComponents(options =>
{
options.BaseUrl = "https://api.myapp.com";
options.ApiKey = "your-api-key";
options.AppId = "d6e61c7a-eec5-4164-a004-9b99eb5eb6de";
// Session management
options.SessionExpirationMinutes = 60;
options.EnableAutoTokenRefresh = true;
options.SlidingExpiration = true;
options.PersistentSession = false;
// HTTP settings
options.RequestTimeoutSeconds = 30;
options.EnableRetry = true;
options.MaxRetryAttempts = 3;
// Security
options.EnableAntiforgeryValidation = true;
// Debugging
options.EnableDetailedErrors = false;
});
Option 3: Configuration from appsettings.json
Read all options from your application configuration. This is the recommended approach for production deployments because settings can vary per environment without code changes:
// Program.cs
builder.Services.AddWildwoodComponents(builder.Configuration, "WildwoodAPI");
The second parameter is the configuration section name. It defaults to "WildwoodAPI" if omitted:
// These two calls are equivalent:
builder.Services.AddWildwoodComponents(builder.Configuration, "WildwoodAPI");
builder.Services.AddWildwoodComponents(builder.Configuration);
Example appsettings.json
{
"WildwoodAPI": {
"BaseUrl": "https://api.myapp.com",
"ApiKey": "your-api-key",
"AppId": "d6e61c7a-eec5-4164-a004-9b99eb5eb6de",
"SessionExpirationMinutes": 60,
"EnableAutoTokenRefresh": true,
"SlidingExpiration": true,
"PersistentSession": false,
"RequestTimeoutSeconds": 30,
"EnableDetailedErrors": false,
"EnableAntiforgeryValidation": true
}
}
appsettings.Development.json for local settings (like https://localhost:7046)
and appsettings.Production.json for production API URLs. The configuration system merges them automatically.
Configuration Options Reference
The WildwoodComponentsOptions class exposes the following settings:
| Property | Type | Default | Description |
|---|---|---|---|
BaseUrl |
string |
"" |
Base URL of the WildwoodAPI server. Required. |
ApiKey |
string? |
null |
API key sent as an X-API-Key header on all requests. |
AppId |
string? |
null |
Application ID for configuration loading. |
SessionExpirationMinutes |
int |
60 |
How long the session stays alive via refresh tokens. Examples: 30 (30 min), 1440 (1 day), 43200 (30 days). |
EnableAutoTokenRefresh |
bool |
false |
When true, automatically refreshes the JWT when a 401/403 response is detected. |
SlidingExpiration |
bool |
true |
Extends session expiry on auth events (token refresh, login). Call TouchSessionAsync() on user activity to keep the session alive. |
PersistentSession |
bool |
false |
When true, sessions survive browser close (persistent cookies in server-side hosts). |
EnableAntiforgeryValidation |
bool |
true |
Require antiforgery token validation on authentication form submissions. |
RequestTimeoutSeconds |
int |
30 |
Timeout for HTTP requests to WildwoodAPI. |
EnableRetry |
bool |
true |
Enable automatic retry on failed HTTP requests. |
MaxRetryAttempts |
int |
3 |
Maximum number of retry attempts when EnableRetry is true. |
EnableCaching |
bool |
true |
Enable response caching for eligible requests. |
CacheDurationMinutes |
int |
10 |
Cache duration in minutes when caching is enabled. |
EnableDetailedErrors |
bool |
true |
Enable detailed error messages in component UI. Set to false in production. |
What Gets Registered
A single call to AddWildwoodComponents() registers all of the following services. You do not need to register them individually.
| Service Interface | Lifetime | Purpose |
|---|---|---|
IAuthenticationService |
Scoped | Login, registration, password reset, 2FA, OAuth, passkeys |
IAIService |
Scoped | AI chat, session management, TTS/STT |
ISecureMessagingService |
Scoped | Encrypted user-to-user messaging |
IPaymentProviderService |
Scoped | Payment provider configuration and setup |
IPaymentService |
Scoped | Payment processing (charges, refunds) |
ISubscriptionService |
Scoped | Subscription plan management and billing |
IWildwoodSessionManager |
Scoped | Automatic token refresh, session monitoring, expiry detection |
IComponentThemeService |
Scoped | Theme management and change notifications |
ILocalStorageService |
Scoped | Browser local storage access for token persistence |
ICaptchaService |
Scoped | CAPTCHA rendering and verification |
INotificationService |
Scoped | In-app notification delivery and management |
IPlatformDetectionService |
Scoped | Detecting the runtime platform (web, iOS, Android, etc.) |
IConfigurationService |
Scoped | Loading app configuration from the API |
ITwoFactorSettingsService |
Scoped | Two-factor authentication setup and management |
PaymentScriptLoader |
Scoped | Dynamic loading of payment provider scripts (Stripe, PayPal, Apple Pay, Google Pay) |
IHttpClientFactory is not already registered in the service collection,
AddWildwoodComponents() will call AddHttpClient() automatically. All services use the factory
to create HttpClient instances configured with the BaseUrl and optional ApiKey.
Blazor Server Additional Setup
If you are using Blazor Server (not WebAssembly), you also need to add server-side Blazor services.
This is typically already in your Program.cs but verify it is present:
// Program.cs - Blazor Server
builder.Services.AddServerSideBlazor();
builder.Services.AddWildwoodComponents("https://localhost:7046");
MAUI Additional Setup
For .NET MAUI Blazor Hybrid apps, register services in your MauiProgram.cs:
// MauiProgram.cs
builder.Services.AddMauiBlazorWebView();
builder.Services.AddWildwoodComponents(options =>
{
options.BaseUrl = "https://api.myapp.com";
options.AppId = "your-app-id";
options.EnableAutoTokenRefresh = true;
options.PersistentSession = true; // Keep sessions across app restarts
});
ILocalStorageService uses secure storage instead of
browser localStorage. Token persistence works automatically but the underlying storage mechanism differs.
Complete Program.cs Example
Here is a full Program.cs for a Blazor Server application with WildwoodComponents:
var builder = WebApplication.CreateBuilder(args);
// Add Razor Pages and Blazor
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// Register WildwoodComponents from configuration
builder.Services.AddWildwoodComponents(builder.Configuration, "WildwoodAPI");
var app = builder.Build();
// Configure the HTTP request pipeline
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
With services registered, you are ready to use your first WildwoodComponent. Continue to Your First Component to see it in action.