Dependency Injection extensions for StableDiffusionNet.Core. Provides Microsoft.Extensions.DependencyInjection integration, logging adapters, and IOptions pattern support for Stable Diffusion WebUI API client. Compatible with .NET Standard 2.0+, .NET Framework 4.7.2+, .NET Core 2.0+, .NET 5.0+.
$ dotnet add package StableDiffusionNet.DependencyInjectionEnglish | Русский
Extensions for Microsoft.Extensions.DependencyInjection to work with StableDiffusionNet.Core.
dotnet add package StableDiffusionNet.DependencyInjection
The package will automatically install StableDiffusionNet.Core as a dependency.
Or via NuGet Package Manager:
Install-Package StableDiffusionNet.DependencyInjection
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using StableDiffusionNet.DependencyInjection.Extensions;
var services = new ServiceCollection();
// Add logging (optional but recommended)
services.AddLogging(builder => builder.AddConsole());
// Register StableDiffusion client (simplest option)
services.AddStableDiffusion("http://localhost:7860");
var serviceProvider = services.BuildServiceProvider();
var client = serviceProvider.GetRequiredService<IStableDiffusionClient>();
services.AddStableDiffusion(options =>
{
options.BaseUrl = "http://localhost:7860";
options.TimeoutSeconds = 600;
options.RetryCount = 5;
options.RetryDelayMilliseconds = 2000;
options.ApiKey = "your-api-key";
options.EnableDetailedLogging = true; // Only for debugging
});
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// Register from configuration
builder.Services.AddStableDiffusion(options =>
{
builder.Configuration.GetSection("StableDiffusion").Bind(options);
});
var app = builder.Build();
// appsettings.json
{
"StableDiffusion": {
"BaseUrl": "http://localhost:7860",
"TimeoutSeconds": 300,
"RetryCount": 3
}
}using Microsoft.AspNetCore.Mvc;
using StableDiffusionNet.Interfaces;
using StableDiffusionNet.Models.Requests;
[ApiController]
[Route("api/[controller]")]
public class ImageGenerationController : ControllerBase
{
private readonly IStableDiffusionClient _sdClient;
private readonly ILogger<ImageGenerationController> _logger;
public ImageGenerationController(
IStableDiffusionClient sdClient,
ILogger<ImageGenerationController> logger)
{
_sdClient = sdClient;
_logger = logger;
}
[HttpPost("generate")]
public async Task<IActionResult> Generate([FromBody] TextToImageRequest request)
{
try
{
var response = await _sdClient.TextToImage.GenerateAsync(request);
return Ok(response);
}
catch (Exception ex)
{
_logger.LogError(ex, "Image generation failed");
return StatusCode(500, "Generation failed");
}
}
}You can also inject individual services:
public class MyService
{
private readonly ITextToImageService _textToImage;
private readonly IModelService _models;
public MyService(
ITextToImageService textToImage,
IModelService models)
{
_textToImage = textToImage;
_models = models;
}
public async Task DoSomething()
{
var models = await _models.GetModelsAsync();
// ...
}
}Important: Never store API keys in code or public repositories!
dotnet user-secrets set "StableDiffusion:ApiKey" "your-secret-key"services.AddStableDiffusion(options =>
{
options.ApiKey = builder.Configuration["StableDiffusion:ApiKey"];
});# Windows PowerShell
$env:SD_API_KEY="your-secret-key"
# Linux/Mac
export SD_API_KEY="your-secret-key"services.AddStableDiffusion(options =>
{
options.ApiKey = Environment.GetEnvironmentVariable("SD_API_KEY");
});var keyVaultUri = new Uri(builder.Configuration["KeyVaultUri"]);
builder.Configuration.AddAzureKeyVault(keyVaultUri, new DefaultAzureCredential());The library automatically integrates with Microsoft.Extensions.Logging:
// Configure logging
builder.Services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddDebug();
builder.SetMinimumLevel(LogLevel.Information);
});Logs include:
Warning: The EnableDetailedLogging option may log sensitive data (prompts, base64 images). Use only for debugging in a safe environment!
| Option | Type | Default | Description |
|---|---|---|---|
BaseUrl | string | "http://localhost:7860" | Base API URL |
TimeoutSeconds | int | 300 | Request timeout in seconds |
RetryCount | int | 3 | Number of retries on error |
RetryDelayMilliseconds | int | 1000 | Delay between retries in ms |
ApiKey | string? | null | API key (if required) |
EnableDetailedLogging | bool | false | Detailed logging (debugging only) |
If you don't need Dependency Injection integration, use the base StableDiffusionNet.Core package:
dotnet add package StableDiffusionNet.Corevar client = StableDiffusionClientBuilder.CreateDefault("http://localhost:7860");MIT License