Erp.Shared.API
1.1.2
See the version list below for details.
dotnet add package Erp.Shared.API --version 1.1.2
NuGet\Install-Package Erp.Shared.API -Version 1.1.2
<PackageReference Include="Erp.Shared.API" Version="1.1.2" />
<PackageVersion Include="Erp.Shared.API" Version="1.1.2" />
<PackageReference Include="Erp.Shared.API" />
paket add Erp.Shared.API --version 1.1.2
#r "nuget: Erp.Shared.API, 1.1.2"
#:package Erp.Shared.API@1.1.2
#addin nuget:?package=Erp.Shared.API&version=1.1.2
#tool nuget:?package=Erp.Shared.API&version=1.1.2
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
ServiceCollectionExtensionswith support for cross-service event-driven architecture - Integration with
Erp.Shared.Contractsv1.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)
Related Packages
- 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 | Versions 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. |
-
net9.0
- Erp.Shared.Contracts (>= 1.1.2)
- Erp.Shared.Infrastructure (>= 1.2.0)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.0)
- Microsoft.AspNetCore.Mvc.Versioning (>= 5.1.0)
- Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer (>= 5.1.0)
- Microsoft.EntityFrameworkCore (>= 9.0.0)
- Microsoft.EntityFrameworkCore.Design (>= 9.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 9.0.0)
- Serilog (>= 3.1.1)
- Serilog.Extensions.Hosting (>= 8.0.0)
- Serilog.Settings.Configuration (>= 8.0.0)
- Serilog.Sinks.Console (>= 5.0.1)
- Serilog.Sinks.File (>= 5.0.0)
- StackExchange.Redis (>= 2.7.17)
- Swashbuckle.AspNetCore (>= 6.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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