Erp.Shared.API 1.1.7

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

Erp.Shared.API

Shared API components and extensions for ERP microservices including service registration, middleware, API versioning, logging, and MassTransit integration.

Version

Current Version: 1.1.0

Features

Service Registration Extensions

  • AddRedisServices(): Redis caching with connection multiplexer
  • AddSerilogLogging(): File and console logging with Serilog
  • AddKubernetesLogging(): Kubernetes-optimized logging for Loki/Promtail/Grafana
  • AddApiVersioningServices(): URL-based API versioning
  • AddVersionedSwaggerGen(): Swagger with versioning and JWT support
  • AddVersionedApiServices(): Complete API versioning + Swagger setup
  • AddUserContextServices(): JWT authentication and user context
  • AddPostgresDbContext<TContext>(): PostgreSQL with Entity Framework Core
  • AddIntegrationEventPublisher(): MassTransit event publisher registration (New in v1.1.0)

User Context

  • IUserExtension: Access to current user claims (UserId, CompanyId, BranchId, Roles)
  • JWT token validation and role-based authorization

Installation

dotnet add package Erp.Shared.API --version 1.1.0

Usage

Basic Setup in Program.cs

using Shared.API.Extensions;

var builder = WebApplication.CreateBuilder(args);

// Add logging
builder.Services.AddSerilogLogging(
    builder.Configuration,
    serviceName: "MyService",
    enableFileLogging: true,
    enableConsoleLogging: true);

// Add Redis caching
builder.Services.AddRedisServices(builder.Configuration);

// Add API versioning with Swagger
builder.Services.AddVersionedApiServices(
    title: "My API",
    description: "My microservice API",
    includeJwtSecurity: true);

// Add user context and JWT authentication
builder.Services.AddUserContextServices(builder.Configuration);

// Add PostgreSQL database
builder.Services.AddPostgresDbContext<MyDbContext>(
    builder.Configuration,
    connectionStringName: "DefaultConnection");

// Add integration event publisher (NEW in v1.1.0)
builder.Services.AddIntegrationEventPublisher();

var app = builder.Build();

// Apply migrations at startup
app.ApplyMigrations<MyDbContext>();

// Use versioned Swagger
app.UseVersionedSwagger();

app.Run();

Using Integration Event Publisher

using Shared.Contracts.Common;

public class CompanyService
{
    private readonly IIntegrationEventPublisher _eventPublisher;

    public CompanyService(IIntegrationEventPublisher eventPublisher)
    {
        _eventPublisher = eventPublisher;
    }

    public async Task CreateCompany(Company company)
    {
        // Save company to database...

        // Publish integration event
        await _eventPublisher.PublishAsync(
            entityType: "Company",
            operation: "Created",
            payload: new {
                CompanyId = company.Id,
                Name = company.Name
            },
            source: "CompanyService");
    }
}

Accessing User Context

using Shared.API.Extensions.Auth;

public class MyController : ControllerBase
{
    private readonly IUserExtension _userExtension;

    public MyController(IUserExtension userExtension)
    {
        _userExtension = userExtension;
    }

    public IActionResult Get()
    {
        var userId = _userExtension.UserId;
        var companyId = _userExtension.CompanyId;
        var branchId = _userExtension.BranchId;
        var roles = _userExtension.Roles;

        // Use user context...
    }
}

Redis Caching

using Shared.Infrastructure.Redis.Services;

public class MyService
{
    private readonly IRedisService _redis;

    public MyService(IRedisService redis)
    {
        _redis = redis;
    }

    public async Task<MyData> GetData(string key)
    {
        return await _redis.GetAsync<MyData>(key);
    }

    public async Task SetData(string key, MyData data)
    {
        await _redis.SetAsync(key, data, TimeSpan.FromHours(1));
    }
}

PostgreSQL DbContext Registration

builder.Services.AddPostgresDbContext<ApplicationDbContext>(
    builder.Configuration,
    connectionStringName: "DefaultConnection",
    enableSensitiveDataLogging: false,
    enableDetailedErrors: false);

Your appsettings.json should include:

{
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Port=5432;Database=your_db;Username=your_user;Password=your_password"
  }
}

Automatic Database Migration at Startup

Apply pending migrations automatically at startup:

app.ApplyMigrations<ApplicationDbContext>();

Or with retry logic:

app.ApplyMigrationsWithRetry<ApplicationDbContext>(maxRetries: 5, retryDelaySeconds: 10);

Kubernetes Logging

For Kubernetes deployments with Loki/Promtail/Grafana:

builder.Services.AddKubernetesLogging(
    builder.Configuration,
    serviceName: "MyService",
    enableConsoleLogging: true,
    enableFileLogging: false); // Promtail reads from stdout

What's New in v1.1.0

  • AddIntegrationEventPublisher(): New extension method to register MassTransit integration event publisher
  • Enhanced ServiceCollectionExtensions with support for cross-service event-driven architecture
  • Integration with Erp.Shared.Contracts v1.1.0 for event publishing

Configuration

appsettings.json Example

{
  "ConnectionStrings": {
    "DefaultConnection": "Host=localhost;Database=mydb;Username=user;Password=pass",
    "Redis": "localhost:6379"
  },
  "JwtSettings": {
    "Secret": "your-secret-key-here",
    "Issuer": "YourIssuer",
    "Audience": "YourAudience",
    "ExpirationInMinutes": 60
  }
}

Dependencies

  • .NET 9.0
  • Microsoft.AspNetCore.App
  • Entity Framework Core 9.0
  • Npgsql.EntityFrameworkCore.PostgreSQL 9.0
  • Serilog 3.1.1
  • StackExchange.Redis 2.7.17
  • Swashbuckle.AspNetCore 6.5.0
  • Erp.Shared.Infrastructure (project reference)
  • Erp.Shared.Contracts (transitive dependency)
  • Erp.Shared.Contracts: Shared DTOs and interfaces
  • Erp.Shared.Infrastructure: Shared infrastructure components

License

MIT

Support

For issues and feature requests, please visit the GitHub repository.

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.1.7 341 11/16/2025
1.1.6 307 11/16/2025
1.1.5 313 11/16/2025
1.1.4 230 11/9/2025
1.1.3 224 11/9/2025
1.1.2 290 11/3/2025
1.1.0 253 10/26/2025
1.0.8 409 9/16/2025
1.0.7 358 9/15/2025

v1.1.0:
     - Added AddIntegrationEventPublisher() extension method for easy MassTransit publisher registration
     - Enhanced ServiceCollectionExtensions with integration event support
     - Updated to support cross-service event-driven architecture