ControlR.ApiClient
0.17.20
dotnet add package ControlR.ApiClient --version 0.17.20
NuGet\Install-Package ControlR.ApiClient -Version 0.17.20
<PackageReference Include="ControlR.ApiClient" Version="0.17.20" />
<PackageVersion Include="ControlR.ApiClient" Version="0.17.20" />
<PackageReference Include="ControlR.ApiClient" />
paket add ControlR.ApiClient --version 0.17.20
#r "nuget: ControlR.ApiClient, 0.17.20"
#:package ControlR.ApiClient@0.17.20
#addin nuget:?package=ControlR.ApiClient&version=0.17.20
#tool nuget:?package=ControlR.ApiClient&version=0.17.20
ControlR API Client
A .NET client library for interacting with the ControlR API. Built with Kiota, this library provides a strongly-typed interface for making API calls to the backend of a ControlR server.
Features
- Strongly-typed API client generated from OpenAPI specification
- Built-in support for dependency injection
- Static builder pattern for quick setup
- Efficient HTTP connection management via
IHttpClientFactory - Automatic request/response serialization
Installation
dotnet add package ControlR.ApiClient
Quick Start
The library supports two usage patterns: dependency injection (recommended for most applications) and a static builder pattern (useful for scripts or simple scenarios).
Option 1: Dependency Injection (Recommended)
Service Registration
Configure the client in your Program.cs or startup file:
using ControlR.ApiClient;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddControlrApiClient(options =>
{
options.BaseUrl = new Uri("https://your-controlr-server.com");
options.PersonalAccessToken = "your-personal-access-token";
});
You can also load options from configuration using the overload that accepts IConfiguration:
using ControlR.ApiClient;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddControlrApiClient(
builder.Configuration,
ControlrApiClientOptions.SectionKey);
With the following configuration in appsettings.json:
{
"ControlrApiClient": {
"BaseUrl": "https://your-controlr-server.com",
"PersonalAccessToken": "your-personal-access-token"
}
}
Using the Client
Inject either IControlrApiClientFactory or ControlrApiClient directly into your services:
using ControlR.ApiClient;
public class MyService
{
private readonly IControlrApiClientFactory _clientFactory;
private readonly ILogger<MyService> _logger;
public MyService(IControlrApiClientFactory clientFactory, ILogger<MyService> logger)
{
_clientFactory = clientFactory;
_logger = logger;
}
public async Task GetDevicesAsync(CancellationToken cancellationToken)
{
var client = _clientFactory.GetClient();
var devices = await client.Api.Devices.GetAsync(cancellationToken: cancellationToken);
if (devices is null)
{
_logger.LogError("Response was empty.");
return;
}
_logger.LogInformation("Retrieved {DeviceCount} devices.", devices.Count);
foreach (var device in devices)
{
_logger.LogInformation(" - {DeviceName} (ID: {DeviceId})", device.Name, device.Id);
}
}
}
Option 2: Static Builder
The static builder pattern is useful for console applications, scripts, or scenarios where you prefer not to use dependency injection.
Initialization
Initialize the builder once at application startup:
using ControlR.ApiClient;
ControlrApiClientBuilder.Initialize(options =>
{
options.BaseUrl = new Uri("https://your-controlr-server.com");
options.PersonalAccessToken = "your-personal-access-token";
});
Using the Client
Get a client instance whenever needed:
var client = ControlrApiClientBuilder.GetClient();
var devices = await client.Api.Devices.GetAsync(cancellationToken: cancellationToken);
if (devices is null)
{
Console.WriteLine("Response was empty.");
return;
}
Console.WriteLine($"Retrieved {devices.Count} devices.");
foreach (var device in devices)
{
Console.WriteLine($" - {device.Name} (ID: {device.Id})");
}
Configuration
ControlrApiClientOptions
| Property | Type | Required | Description |
|---|---|---|---|
BaseUrl |
Uri |
Yes | The base URL of your ControlR server |
PersonalAccessToken |
string |
Yes | Your personal access token for authentication |
Obtaining a Personal Access Token
- Log in to your ControlR server
- Navigate to your account settings
- Generate a new Personal Access Token
- Store it securely (e.g., using User Secrets in development or secure configuration in production)
How It Works
IHttpClientFactory Integration
The ControlR API Client uses IHttpClientFactory under the hood to manage HttpClient instances. This provides several benefits:
- Prevents socket exhaustion: Automatically manages the lifecycle of HTTP connections
- Handles DNS changes: Respects DNS TTL by periodically recycling connections
- Efficient resource usage: Pools and reuses connections
- Handler pipeline: Supports middleware-style message handlers for cross-cutting concerns
This means you can safely create multiple client instances without worrying about common pitfalls associated with direct HttpClient usage.
Kiota Client Generation
The client is generated using Microsoft's Kiota tool, which provides:
- Type-safe request builders
- Automatic serialization/deserialization
- Fluent API design
- Built-in error handling
- Support for various authentication schemes
Additional Resources
- Kiota Documentation
- Kiota .NET Dependency Injection Guide
- IHttpClientFactory Best Practices
- ControlR Documentation
Example Project
For a complete working example, see the ControlR.ApiClientExample project in this repository.
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- ControlR.Libraries.DataRedaction (>= 0.17.20)
- Microsoft.Extensions.Compliance.Redaction (>= 10.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Http (>= 10.0.1)
- Microsoft.Kiota.Bundle (>= 1.21.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.17.20 | 110 | 12/31/2025 |
| 0.17.19-dev | 84 | 12/31/2025 |
| 0.17.17 | 84 | 12/31/2025 |