StableDiffusionNet.DependencyInjection 1.1.5

dotnet add package StableDiffusionNet.DependencyInjection --version 1.1.5
                    
NuGet\Install-Package StableDiffusionNet.DependencyInjection -Version 1.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StableDiffusionNet.DependencyInjection" Version="1.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StableDiffusionNet.DependencyInjection" Version="1.1.5" />
                    
Directory.Packages.props
<PackageReference Include="StableDiffusionNet.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StableDiffusionNet.DependencyInjection --version 1.1.5
                    
#r "nuget: StableDiffusionNet.DependencyInjection, 1.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StableDiffusionNet.DependencyInjection@1.1.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StableDiffusionNet.DependencyInjection&version=1.1.5
                    
Install as a Cake Addin
#tool nuget:?package=StableDiffusionNet.DependencyInjection&version=1.1.5
                    
Install as a Cake Tool

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

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.1.5 167 10/28/2025
1.1.4 164 10/27/2025
1.1.3 158 10/26/2025
1.1.2 160 10/26/2025
1.1.1 149 10/26/2025
1.1.0 152 10/26/2025

See CHANGELOG.md for details