pvNugsSemaphoreNc9MsSql 9.0.0

dotnet add package pvNugsSemaphoreNc9MsSql --version 9.0.0
                    
NuGet\Install-Package pvNugsSemaphoreNc9MsSql -Version 9.0.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="pvNugsSemaphoreNc9MsSql" Version="9.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="pvNugsSemaphoreNc9MsSql" Version="9.0.0" />
                    
Directory.Packages.props
<PackageReference Include="pvNugsSemaphoreNc9MsSql" />
                    
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 pvNugsSemaphoreNc9MsSql --version 9.0.0
                    
#r "nuget: pvNugsSemaphoreNc9MsSql, 9.0.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 pvNugsSemaphoreNc9MsSql@9.0.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=pvNugsSemaphoreNc9MsSql&version=9.0.0
                    
Install as a Cake Addin
#tool nuget:?package=pvNugsSemaphoreNc9MsSql&version=9.0.0
                    
Install as a Cake Tool

pvNugsSemaphoreNc9MsSql

NuGet License: MIT Build Status

✨ Distributed Semaphore for .NET with SQL Server

pvNugsSemaphoreNc9MsSql provides a robust, distributed semaphore (mutex) implementation for .NET, backed by Microsoft SQL Server. It enables safe, cross-process and cross-machine locking for critical sections, scheduled jobs, or resource coordination.


🚀 Features

  • Distributed locking across processes and servers
  • Atomic operations using SQL Server
  • Automatic table creation (optional)
  • Configurable via .NET configuration
  • Async/await support
  • Dependency Injection ready

📦 Installation

dotnet add package pvNugsSemaphoreNc9MsSql

⚙️ Configuration

Add the following to your appsettings or use in-memory configuration:

{
  "PvNugsMsSqlSemaphoreConfig": {
    "ConnectionStringName": "LoggingDb",
    "TableName": "MySemaphore",
    "SchemaName": "dbo",
    "CreateTableAtFirstUse": true
  }
}

🛠️ Usage Example

Below is a minimal integration test console showing how to configure and use the semaphore:

uusing Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using pvNugsCsProviderNc9MsSql;
using pvNugsLoggerNc9Abstractions;
using pvNugsLoggerNc9Seri;
using pvNugsSemaphoreNc9Abstractions;
using pvNugsSemaphoreNc9MsSql;

Console.WriteLine("Integration testing console for pvNugsSemaphoreNc9MsSql");

var inMemSettings = new Dictionary<string, string>
{
    // SERILOG
    { "PvNugsLoggerConfig:MinLogLevel", "trace" },
    
    // CS PROVIDER in Config mode
    // Here we mount a Docker container running postgres on port 5433
    { "PvNugsCsProviderMsSqlConfig:Rows:0:Name", "LoggingDb" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:Mode", "Config" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:Server", "Localhost" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:Schema", "dbo" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:Database", "IntTestingDb" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:Port", "1433" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:TimeoutInSeconds", "300" },
    { "PvNugsCsProviderMsSqlConfig:Rows:0:UseIntegratedSecurity", "true" },
    
    // MS SQL SEMAPHORE CONFIG
    { "PvNugsMsSqlSemaphoreConfig:ConnectionStringName", "LoggingDb" },
    { "PvNugsMsSqlSemaphoreConfig:TableName", "MySemaphore" },
    { "PvNugsMsSqlSemaphoreConfig:CreateTableAtFirstUse", "true" }
};

var config = new ConfigurationBuilder()
    .AddInMemoryCollection(inMemSettings!)
    .Build();

var services = new ServiceCollection();

services.TryAddPvNugsLoggerSeriService(config);
services.TryAddPvNugsCsProviderMsSql(config);
services.TryAddPvNugsMsSqlSemaphore(config);

var sp = services.BuildServiceProvider();
var logger = sp.GetRequiredService<IConsoleLoggerService>();
var svc = sp.GetRequiredService<IPvNugsSemaphoreService>();

const string theMutex = "MyUniqueMutex";

var si = await svc.AcquireSemaphoreAsync(
    theMutex, Environment.MachineName, TimeSpan.FromSeconds(10));
await logger.LogAsync(si.ToString()!);

📚 API Overview

  • AcquireSemaphoreAsync — Acquire or steal a named semaphore
  • ReleaseSemaphoreAsync — Release a semaphore
  • TouchSemaphoreAsync — Extend semaphore validity
  • GetSemaphoreAsync — Query semaphore state
  • IsolateWorkAsync — Run code in a protected context

See XML documentation in code for details.


📝 License

MIT © licheez

Product Compatible and additional computed target framework versions.
.NET 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 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. 
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
9.0.0 25 10/2/2025

Initial