Peerpay.Internal.Backend
1.1.2
See the version list below for details.
dotnet add package Peerpay.Internal.Backend --version 1.1.2
NuGet\Install-Package Peerpay.Internal.Backend -Version 1.1.2
<PackageReference Include="Peerpay.Internal.Backend" Version="1.1.2" />
<PackageVersion Include="Peerpay.Internal.Backend" Version="1.1.2" />
<PackageReference Include="Peerpay.Internal.Backend" />
paket add Peerpay.Internal.Backend --version 1.1.2
#r "nuget: Peerpay.Internal.Backend, 1.1.2"
#:package Peerpay.Internal.Backend@1.1.2
#addin nuget:?package=Peerpay.Internal.Backend&version=1.1.2
#tool nuget:?package=Peerpay.Internal.Backend&version=1.1.2
InternalBackendLib SDK
An internal SDK for Peerpay Digital Asset backend services, including Kafka logging and Identity management.
Installation
Add the package to your project:
dotnet add package Peerpay.Internal.Backend
# Install a specific version
dotnet add package Peerpay.Internal.Backend --version 1.0.0
Setup & Dependency Injection
Register the services in your Program.cs or Startup.cs.
1. Identity Service
Use AddIdentityService to register the IIdentityService. You can configure the environment via options.
using Peerpay.Internal.Backend.Extensions;
// For Test Environment
builder.Services.AddIdentityService(options =>
{
options.IsProd = false;
});
// For Production Environment
builder.Services.AddIdentityService(options =>
{
options.IsProd = true;
});
Usage Examples
Identity Service
Inject IIdentityService into your class.
public class MyService
{
private readonly IIdentityService _identityService;
public MyService(IIdentityService identityService)
{
_identityService = identityService;
}
public async Task LoginAsync()
{
var request = new AuthorizeRequest
{
Email = "user@example.com",
Password = "password",
ApplicationId = "guid"
};
var response = await _identityService.AuthorizeAsync(request);
if (response.Status)
{
var token = response.Data.Token;
}
}
public async Task GetInternalTokenAsync()
{
var request = new InternalTokenRequest
{
ClientId = "client_id",
ClientSecret = "client_secret",
AppCode = "app_code"
};
// Pass the API Key/Token required for the Authorization header
var response = await _identityService.GetInternalTokenAsync(request, "your-api-key");
}
}
2. Validation Features
The SDK provides strong validation support against XSS and SQL Injection.
String Extensions
Standalone validation for any string.
using Peerpay.Internal.Backend.Extensions;
string input = "<script>alert('xss')</script>";
// Check if valid
bool isValid = input.XssValidation().IsValid; // false
// Get detailed error
var (valid, message) = input.XssValidation();
if (!valid)
{
Console.WriteLine(message); // "Input contains potentially malicious XSS content."
}
FluentValidation Integration
Easily integrate into your existing validators.
using FluentValidation;
using Peerpay.Internal.Backend.Validation;
public class CreateUserValidator : AbstractValidator<CreateUserRequest>
{
public CreateUserValidator()
{
RuleFor(x => x.Bio)
.XssCsrfValidation(); // Checks for XSS, SQLi, and malicious patterns
RuleFor(x => x.Website)
.MustBeValidUrl();
}
}
// Register validators from assembly
services.AddValidatorsFromAssembly(typeof(Program).Assembly);
3. Resilient Kafka Producer (AWS MSK)
The SDK includes a resilient Kafka producer with AWS MSK support (SASL/OAuthBearer) and Polly policies (Retry, Circuit Breaker, Timeout).
Configuration
Register the producer in Program.cs.
using Peerpay.Internal.Backend.Extensions;
builder.Services.AddKafkaProducer(options =>
{
options.BootstrapServers = "b-1.msk-cluster.amazonaws.com:9098";
// Enable MSK specific settings (SASL/SSL + OAuthBearer)
options.IsMsk = true;
options.Region = "us-east-1";
// IMPORTANT: AccessKey and SecretKey are MANDATORY for MSK
options.AccessKey = "YOUR_ACCESS_KEY";
options.SecretKey = "YOUR_SECRET_KEY";
// Optional: Map internal topic names to actual Kafka topics
options.Topics = new Dictionary<string, string>
{
{ "orders", "orders-topic-v1" },
{ "logs", "app-logs-prod" }
};
// Resilience settings
options.MessageSendMaxRetries = 3;
options.Ack = Confluent.Kafka.Acks.All;
});
Producing Messages
Inject IResilientKafkaProducer for robust messaging.
public class OrderService
{
private readonly IResilientKafkaProducer _producer;
public OrderService(IResilientKafkaProducer producer)
{
_producer = producer;
}
public async Task CreateOrderAsync(Order order)
{
// policies (retry/circuit-breaker) are applied automatically
bool success = await _producer.ProduceWithRetryAsync("orders", order);
if (!success)
{
// Handle failure after retries exhausted
}
}
}
4. Kafka Consumer (Background Service)
The SDK provides an optimized KafkaConsumerBase<T> to simplify consuming messages. It handles:
- MSK Authentication: Automatic OAuth token refresh.
- Deserialization: Automated JSON deserialization using
System.Text.Json. - Resilience: Poison pill skipping (bad JSON/nulls) and error logging.
- Graceful Shutdown: Handles cancellation tokens properly.
Implementation
Inherit from KafkaConsumerBase<TMessage>.
public class OrderProcessedConsumer : KafkaConsumerBase<OrderProcessedEvent>
{
private readonly IServiceProvider _serviceProvider;
public OrderProcessedConsumer(
ILogger<KafkaConsumerBase<OrderProcessedEvent>> logger,
IOptions<KafkaOptions> kafkaConfig,
IHostEnvironment env,
IServiceScopeFactory scopeFactory,
IServiceProvider serviceProvider)
: base(logger, kafkaConfig, env, scopeFactory)
{
_serviceProvider = serviceProvider;
}
// Map to config keys in KafkaOptions.Topics and .ConsumerGroups
protected override string TopicName => "orders";
protected override string GroupIdName => "order-processors";
protected override async Task ProcessMessageAsync(OrderProcessedEvent message, CancellationToken ct)
{
// Use Scoped Services safely
using var scope = _scopeFactory.CreateScope();
var handler = scope.ServiceProvider.GetRequiredService<IOrderHandler>();
await handler.HandleAsync(message, ct);
// Log processed
_logger.LogInformation("Processed order {OrderId}", message.OrderId);
}
}
Registration
Register as a Hosted Service.
services.AddHostedService<OrderProcessedConsumer>();
// Ensure Config is valid
services.Configure<KafkaOptions>(options =>
{
options.Topics["orders"] = "actual-order-topic-v1";
options.ConsumerGroups["order-processors"] = "group-id-v1";
});
| Product | Versions 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 was computed. 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. |
-
net8.0
- AWS.MSK.Auth (>= 1.1.2)
- AWSSDK.Core (>= 4.0.3.5)
- AWSSDK.Extensions.NETCore.Setup (>= 3.7.300)
- AWSSDK.SecurityToken (>= 4.0.1.5)
- Confluent.Kafka (>= 2.10.0)
- FluentValidation (>= 12.1.1)
- FluentValidation.DependencyInjectionExtensions (>= 12.1.1)
- HtmlSanitizer (>= 9.0.889)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Logging (>= 8.0.1)
- Polly (>= 8.5.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release with standardized logging and configuration.