LangMate.Middleware
1.0.3
See the version list below for details.
dotnet add package LangMate.Middleware --version 1.0.3
NuGet\Install-Package LangMate.Middleware -Version 1.0.3
<PackageReference Include="LangMate.Middleware" Version="1.0.3" />
<PackageVersion Include="LangMate.Middleware" Version="1.0.3" />
<PackageReference Include="LangMate.Middleware" />
paket add LangMate.Middleware --version 1.0.3
#r "nuget: LangMate.Middleware, 1.0.3"
#:package LangMate.Middleware@1.0.3
#addin nuget:?package=LangMate.Middleware&version=1.0.3
#tool nuget:?package=LangMate.Middleware&version=1.0.3
LangMate.Middleware
Part of the LangMate.Core SDK
📦 NuGet
LangMate.Middleware provides robust, pluggable middleware components for ASP.NET Core applications in the LangMate ecosystem. These middlewares improve resiliency, observability, and error handling—especially in AI-driven apps using local models like Ollama.
📦 Installation
To use LangMate.Middleware
, install the required NuGet package, or include the project reference in your solution.
dotnet add package LangMate.Middleware
✨ Middlewares and Features
- Resiliency Middleware with:
- Retry (Polly-based)
- Timeout
- Circuit Breaker
- Serilog and APM support for observe and write ability for logging all requests and errors
- Exception Handling Middleware
- Request Logging Middleware
- 📉 Graceful degradation with friendly error responses
- 🧩 Plug-and-play into any ASP.NET Core app
🛡️ Resiliency Middleware
The Resiliency Middleware is a centralized, fault-tolerant HTTP request wrapper built using the Polly resilience framework. It enhances the reliability and responsiveness of your ASP.NET Core application by automatically handling transient failures, long-running requests, and service degradation.
This middleware is designed for AI-driven or service-heavy applications, such as LangMate, that depend on local/remote models, file uploads, and dynamic APIs.
🔧 What It Does
Wraps every HTTP request in a resilient policy pipeline:
Policy | Behavior |
---|---|
Retry | Retries failed requests up to 3 times with exponential backoff. |
Timeout | Cancels requests that exceed 10 seconds, returns 504 Gateway Timeout. |
Circuit Breaker | Opens circuit after 2 consecutive failures, blocks requests for 30s. |
Blazor SignalR (
/_blazor
) and WebSocket requests are automatically excluded to avoid interfering with real-time behavior.
⚙️ Behavior at Runtime
- ✅ Automatically retries transient errors (e.g., exceptions from external calls).
- ⏱ Times out long requests and fails gracefully.
- 🔌 Breaks the circuit when failures reach a threshold.
- 📉 Returns user-friendly responses:
503 Service Unavailable
(circuit open)504 Gateway Timeout
(timeout triggered)
- 🧠 Logs events like retries, breaks, resets, and errors.
⚙️ Example Resiliency Middleware appsettings.json
{
"ResiliencyMiddlewareOptions": {
"RetryCount": 3,
"TimeoutSeconds": 10,
"ExceptionsAllowedBeforeCircuitBreaking": 2,
"CircuitBreakingDurationSeconds": 30
}
}
Serilog Logger
- Enrich logs with context and exceptions.
- Add correlation IDs for request tracing.
- Load settings from your
appsettings.json
.
⚙️ Example Serilog appsettings.json
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Elasticsearch" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithExceptionDetails", "WithCorrelationId" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {CorrelationId} {Message:lj}{NewLine}{Exception}"
}
},
// optional if you want to save the logs into a file
{
"Name": "File",
"Args": {
"path": "Logs/log-.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": 14,
"shared": true
}
},
// optional if using APM
{
"Name": "Elasticsearch",
"Args": {
"nodeUris": "http://localhost:9200",
"indexFormat": "myapp-logs-{0:yyyy.MM}",
"autoRegisterTemplate": true,
"autoRegisterTemplateVersion": "ESv7"
}
}
]
},
// optional if using APM
"ElasticApm": {
"ServerUrls": "http://localhost:8200",
"ServiceName": "MyApp.Service",
"Environment": "Development",
"SecretToken": "",
"TransactionSampleRate": 1.0
}
}
🔧 Service Registration
In your Program.cs
or inside a service registration method (already is done in LangMate.Core):
// Setting Serilog logger
var builder = WebApplication.CreateBuilder(args);
Log.Logger = LangMateLoggingExtensions.AddLangMateLogging(builder.Configuration);
...
// Adding Middleware
services.AddLangMateMiddleware(configuration, useApm: useApm);
...
// Hook Into ASP.NET Core Pipeline
var loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
app.UseLangMateMiddleware(configuration, loggerFactory);
📄 License
MIT License
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
- Elastic.Apm.NetCoreAll (>= 1.34.0)
- LangMate.Abstractions (>= 1.0.3)
- LangMate.Persistence (>= 1.0.3)
- Polly (>= 8.6.3)
- Serilog (>= 4.3.0)
- Serilog.AspNetCore (>= 9.0.0)
- Serilog.Enrichers.CorrelationId (>= 3.0.1)
- Serilog.Exceptions (>= 8.4.0)
- Serilog.Extensions.Logging (>= 9.0.2)
- Serilog.Sinks.Elasticsearch (>= 10.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LangMate.Middleware:
Package | Downloads |
---|---|
LangMate.Core
LangMate.Core is a lightweight, extensible .NET SDK designed to make working with Ollama-powered local AI models seamless and developer-friendly. It abstracts away the complexity of managing conversations, interacting with Ollama endpoints, and persisting chat history — all while offering resiliency, caching, and extensibility. |
GitHub repositories
This package is not used by any popular GitHub repositories.