LangMate.Middleware 1.0.3

There is a newer version of this package available.
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
                    
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="LangMate.Middleware" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LangMate.Middleware" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="LangMate.Middleware" />
                    
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 LangMate.Middleware --version 1.0.3
                    
#r "nuget: LangMate.Middleware, 1.0.3"
                    
#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 LangMate.Middleware@1.0.3
                    
#: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=LangMate.Middleware&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=LangMate.Middleware&version=1.0.3
                    
Install as a Cake Tool

LangMate.Middleware

Part of the LangMate.Core SDK

📦 NuGet

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 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 (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.

Version Downloads Last Updated
1.0.4 1 9/30/2025
1.0.3 29 9/30/2025
1.0.0 273 9/18/2025