Veggerby.Ignition.SqlServer 0.5.0

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Veggerby.Ignition.SqlServer --version 0.5.0
                    
NuGet\Install-Package Veggerby.Ignition.SqlServer -Version 0.5.0
                    
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="Veggerby.Ignition.SqlServer" Version="0.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Veggerby.Ignition.SqlServer" Version="0.5.0" />
                    
Directory.Packages.props
<PackageReference Include="Veggerby.Ignition.SqlServer" />
                    
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 Veggerby.Ignition.SqlServer --version 0.5.0
                    
#r "nuget: Veggerby.Ignition.SqlServer, 0.5.0"
                    
#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 Veggerby.Ignition.SqlServer@0.5.0
                    
#: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=Veggerby.Ignition.SqlServer&version=0.5.0
                    
Install as a Cake Addin
#tool nuget:?package=Veggerby.Ignition.SqlServer&version=0.5.0
                    
Install as a Cake Tool

Veggerby.Ignition.SqlServer

SQL Server readiness signals for Veggerby.Ignition - verify database connections and schema during application startup.

Installation

dotnet add package Veggerby.Ignition.SqlServer
dotnet add package Microsoft.Data.SqlClient

Usage

The recommended approach uses a connection factory registered in DI for better connection management:

// Register connection factory in DI container
builder.Services.AddSingleton<Func<SqlConnection>>(() => 
    new SqlConnection("Server=localhost;Database=MyDb;Trusted_Connection=True;"));

// Add SQL Server readiness signal (automatically resolves factory from DI)
builder.Services.AddIgnition();
builder.Services.AddSqlServerReadiness();

var app = builder.Build();
await app.Services.GetRequiredService<IIgnitionCoordinator>().WaitAllAsync();

Why Connection Factory?

  • Cleaner DI integration: No connection string duplication across services
  • Centralized configuration: Single factory registration shared by all consumers
  • Testability: Easy to mock or replace for testing
  • Connection pooling: SQL Server connection pooling managed by ADO.NET

Legacy Pattern: Connection String

For simpler scenarios or when a connection factory isn't registered in DI:

builder.Services.AddIgnition();

builder.Services.AddSqlServerReadiness(
    "Server=localhost;Database=MyDb;Trusted_Connection=True;");

var app = builder.Build();
await app.Services.GetRequiredService<IIgnitionCoordinator>().WaitAllAsync();

With Validation Query

builder.Services.AddSingleton<Func<SqlConnection>>(() => new SqlConnection(connectionString));

builder.Services.AddSqlServerReadiness(options =>
{
    options.ValidationQuery = "SELECT 1";
    options.Timeout = TimeSpan.FromSeconds(5);
});

Advanced Schema Validation

builder.Services.AddSingleton<Func<SqlConnection>>(() => new SqlConnection(connectionString));

builder.Services.AddSqlServerReadiness(options =>
{
    // Verify specific table exists
    options.ValidationQuery = "SELECT TOP 1 1 FROM Users";
    options.Timeout = TimeSpan.FromSeconds(10);
});

Features

  • Connection Verification: Validates database connectivity
  • Optional Query Validation: Execute custom queries to verify schema readiness
  • Activity Tracing: Tags for server, database, and validation queries
  • Structured Logging: Information, Debug, and Error level diagnostics
  • Idempotent Execution: Cached results prevent redundant connection attempts
  • Thread-Safe: Concurrent readiness checks execute once

Configuration Options

Property Type Description Default
Timeout TimeSpan? Per-signal timeout override null (uses global timeout)
ValidationQuery string? SQL query to execute after connection null (connection only)

Logging

The signal emits structured logs at different levels:

  • Information: Connection attempts and successful completions
  • Debug: Connection established, validation query execution
  • Error: Connection failures, query execution failures

Activity Tracing

When tracing is enabled, the signal adds these tags:

  • sqlserver.server: Data source from connection string
  • sqlserver.database: Initial catalog from connection string
  • sqlserver.validation_query: Validation query if configured

Examples

Health Check Integration

builder.Services.AddIgnition();
builder.Services.AddSqlServerReadiness(connectionString);

builder.Services
    .AddHealthChecks()
    .AddCheck<IgnitionHealthCheck>("ignition-readiness");

Multiple Databases

// Primary database
builder.Services.AddSqlServerReadiness(
    primaryConnectionString,
    options =>
    {
        options.ValidationQuery = "SELECT 1";
        options.Timeout = TimeSpan.FromSeconds(5);
    });

// Reporting database
builder.Services.AddSqlServerReadiness(
    reportingConnectionString,
    options =>
    {
        options.ValidationQuery = "SELECT 1";
        options.Timeout = TimeSpan.FromSeconds(10);
    });

Note: Currently, multiple SQL Server signals will share the same name ("sqlserver-readiness"). For distinct signals, implement custom IIgnitionSignal with unique names.

Error Handling

Connection failures and query execution errors are logged and propagated:

try
{
    await coordinator.WaitAllAsync();
}
catch (AggregateException ex)
{
    foreach (var inner in ex.InnerExceptions)
    {
        if (inner is SqlException sqlEx)
        {
            // Handle SQL Server-specific errors
            Console.WriteLine($"SQL Error {sqlEx.Number}: {sqlEx.Message}");
        }
    }
}

Performance

  • Minimal allocations per signal invocation
  • Connection pooling handled by Microsoft.Data.SqlClient
  • Async throughout (no blocking I/O)
  • Idempotent execution (connection attempted once)

License

MIT License. See LICENSE.

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 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. 
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.