HarPer.TenantConnectionProvider 1.0.5

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

TenantConnectionProvider

Proveedor de cadenas de conexión multi-tenant con autenticación basada en token y soporte de caché. Diseñado para aplicaciones multi-tenant que requieren obtener dinámicamente cadenas de conexión desde una API administrativa, autenticándose con el token JWT actual del usuario


🚀 Instalación

Instala el paquete usando NuGet:

dotnet add package HarPer.TenantConnectionProvider

🛠️ Configuración

1️⃣ Registrar el servicio en Program.cs:

builder.Services.AddTenantConnectionProvider(builder.Configuration["AdminApiBaseUrl"]);

Configurar appsettings.json:

{
  "AdminApiBaseUrl": "https://tu-api-de-admin.com/"
}

🛠️ Cómo usarlo

Obtener la cadena de conexión:

Desde cualquier clase con acceso al ITenantConnectionProvider, puedes obtener la cadena de conexión del tenant actual:

public class MyService
{
    private readonly ITenantConnectionProvider _connectionProvider;

    public MyService(ITenantConnectionProvider connectionProvider)
    {
        _connectionProvider = connectionProvider;
    }

    public async Task DoSomethingAsync()
    {
        var connectionString = await _connectionProvider.GetConnectionStringAsync();
        // Usar connectionString para lo que necesites
    }
}

Ejemplo en DbContextFactory

public class MyDbContextFactory
{
    private readonly ITenantConnectionProvider _connectionProvider;

    public MyDbContextFactory(ITenantConnectionProvider connectionProvider)
    {
        _connectionProvider = connectionProvider;
    }

    public async Task<MyDbContext> CreateDbContextAsync()
    {
        var connectionString = await _connectionProvider.GetConnectionStringAsync();
        var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
        optionsBuilder.UseSqlServer(connectionString);
        return new MyDbContext(optionsBuilder.Options);
    }
}

Flujo de funcionamiento

  • El front-end envía el token JWT con el tenant_id en los claims.
  • TenantConnectionProvider extrae el tenant_id desde los claims del token.
  • Si la cadena de conexión ya está en caché (por tenant_id), se devuelve directamente.
  • Si no, consulta a la API administrativa usando el mismo token para autenticarse.
  • Cachea la cadena de conexión por tenant_id (ej.: 10 minutos) y la devuelve.

📤 Estructura de respuesta esperada

Tu API Tenant debe retornar este JSON:

{
  "isSuccess": true,
  "data": {
    "name": "Tenant Name",
    "description": "Tenant Description",
    "connectionString": "YourConnectionStringHere"
  },
  "error": null,
  "message": null,
  "timestamp": "2025-04-13T01:45:14.593Z"
}

🌟 Requisitos

  • .NET 9 o superior
  • Token JWT válido con tenant_id en los claims.
  • Configuración válida de AdminApiBaseUrl.

📄 Licencia

MIT License


🧑‍💻 Autor

HarPer SRL

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
1.0.5 164 5/20/2025
1.0.2 150 5/20/2025
1.0.1 149 4/13/2025
1.0.0 148 4/13/2025