StableDiffusionNet.DependencyInjection
1.1.5
dotnet add package StableDiffusionNet.DependencyInjection --version 1.1.5
NuGet\Install-Package StableDiffusionNet.DependencyInjection -Version 1.1.5
<PackageReference Include="StableDiffusionNet.DependencyInjection" Version="1.1.5" />
<PackageVersion Include="StableDiffusionNet.DependencyInjection" Version="1.1.5" />
<PackageReference Include="StableDiffusionNet.DependencyInjection" />
paket add StableDiffusionNet.DependencyInjection --version 1.1.5
#r "nuget: StableDiffusionNet.DependencyInjection, 1.1.5"
#:package StableDiffusionNet.DependencyInjection@1.1.5
#addin nuget:?package=StableDiffusionNet.DependencyInjection&version=1.1.5
#tool nuget:?package=StableDiffusionNet.DependencyInjection&version=1.1.5
StableDiffusionNet.DependencyInjection
English | Русский
Extensions for Microsoft.Extensions.DependencyInjection to work with StableDiffusionNet.Core.
Features
- Microsoft.Extensions.DependencyInjection: integration with DI container
- Microsoft.Extensions.Logging: automatic logging integration
- IOptions Pattern: configuration via IOptions<T>
- IHttpClientFactory: HttpClient management via factory
- All Core Features: access to StableDiffusionNet.Core functionality
Installation
dotnet add package StableDiffusionNet.DependencyInjection
The package will automatically install StableDiffusionNet.Core as a dependency.
Or via NuGet Package Manager:
Install-Package StableDiffusionNet.DependencyInjection
Quick Start
Simple Registration
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>();
Advanced Configuration
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
});
ASP.NET Core Integration
// 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 in Controllers
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");
}
}
}
Using Individual Services
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();
// ...
}
}
API Key Security
Important: Never store API keys in code or public repositories!
User Secrets (for development)
dotnet user-secrets set "StableDiffusion:ApiKey" "your-secret-key"
services.AddStableDiffusion(options =>
{
options.ApiKey = builder.Configuration["StableDiffusion:ApiKey"];
});
Environment Variables
# 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");
});
Azure Key Vault (for production)
var keyVaultUri = new Uri(builder.Configuration["KeyVaultUri"]);
builder.Configuration.AddAzureKeyVault(keyVaultUri, new DefaultAzureCredential());
Logging
The library automatically integrates with Microsoft.Extensions.Logging:
// Configure logging
builder.Services.AddLogging(builder =>
{
builder.AddConsole();
builder.AddDebug();
builder.SetMinimumLevel(LogLevel.Information);
});
Logs include:
- Request and response information
- Errors and exceptions
- Retry attempts
- Operation progress
Warning: The EnableDetailedLogging option may log sensitive data (prompts, base64 images). Use only for debugging in a safe environment!
Available Options
| 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) |
Need Lightweight Version without DI?
If you don't need Dependency Injection integration, use the base StableDiffusionNet.Core package:
dotnet add package StableDiffusionNet.Core
var client = StableDiffusionClientBuilder.CreateDefault("http://localhost:7860");
License
MIT License
Links
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Http (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
- Microsoft.Extensions.Options (>= 6.0.0)
- StableDiffusionNet.Core (>= 1.1.5)
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- StableDiffusionNet.Core (>= 1.1.5)
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Options (>= 8.0.2)
- StableDiffusionNet.Core (>= 1.1.5)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.10)
- Microsoft.Extensions.Http (>= 9.0.10)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.10)
- Microsoft.Extensions.Options (>= 9.0.10)
- StableDiffusionNet.Core (>= 1.1.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
See CHANGELOG.md for details