Grad.CsLib
2.0.14
See the version list below for details.
dotnet add package Grad.CsLib --version 2.0.14
NuGet\Install-Package Grad.CsLib -Version 2.0.14
<PackageReference Include="Grad.CsLib" Version="2.0.14" />
<PackageVersion Include="Grad.CsLib" Version="2.0.14" />
<PackageReference Include="Grad.CsLib" />
paket add Grad.CsLib --version 2.0.14
#r "nuget: Grad.CsLib, 2.0.14"
#:package Grad.CsLib@2.0.14
#addin nuget:?package=Grad.CsLib&version=2.0.14
#tool nuget:?package=Grad.CsLib&version=2.0.14
CsLib Core Package
This package contains utilities for C# API projects.
This package is very opinionated, as it's intended for Graduate College API projects:
- FastEndpoints is used, which is a thin layer on top of .NET Core minimal APIs.
- OpenAPI/Swagger docs are generated from endpoint definitions and XML documentation.
The JSON is exported to the
/specfolder for generating client libraries.- Kiota is used to generate the OpenAPI JSON, but not the client libraries.
- Authentication is through Entra.
For local development scenarios where involving Entra isn't desirable, CsLib also provides an opt-in combined
authentication mode that accepts a simple Api-Key header (see AddAuthAzureAdOrDevApiKey()).
Requirements
- .NET Core 10
Dependencies
Outside of Microsoft packages, these are the main dependencies:
CsLibWeb
Here's an example of how to set up a Program.cs file for a web API project:
using ExampleData;
using ExampleServer;
using FastEndpoints.ClientGen.Kiota;
using Grad.CsLib;
using Grad.CsLib.ClientErrorLogging;
using Microsoft.EntityFrameworkCore;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
builder.AddSwagger(args, "ExampleServer", "0.0.1", "Example Backend Server");
builder
.AddSerilog()
.AddCors()
.AddAuth();
builder.Services
.AddDbContext<ExampleContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("Example")))
.AddAuthorization(AuthPolicy.AddAuthPolicy)
.RegisterServicesFromExampleServer();
builder.AddEndpoints(DiscoveredTypes.All);
try
{
var app = builder.BuildAndConfigureApp(b => b.ReflectionCache.AddFromExampleData().AddFromExampleServer());
// Optional: Add client-side error logging endpoint
app.MapClientErrorLogging("/api/client-errors");
// This is only run with the --exportswaggerjson option
await app.ExportSwaggerJsonAndExitAsync("ExampleServer", "../spec");
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}
Development API key (optional)
If you want to allow a development-only API key as an alternative to Entra, replace AddAuth() with:
builder
.AddSerilog()
.AddCors()
.AddAuthAzureAdOrDevApiKey();
Configure the expected key in configuration (for example via appsettings.Development.json):
{
"CsLibWeb": {
"DevApiKey": "your-dev-key"
}
}
When the request includes the Api-Key header, API key authentication is used; otherwise CsLib falls back to
Entra bearer authentication.
Client-Side Error Logging
CsLib provides a convenient endpoint for logging client-side errors. This allows frontend applications to send error information to the backend for centralized logging and analysis.
For detailed documentation, see ClientErrorLogging/README.md.
Quick example:
// In Program.cs, after building the app
app.MapClientErrorLogging("/api/client-errors");
This creates an anonymous POST endpoint that accepts client errors with validation for error type, message, stack trace, and contextual data. All errors are logged to Serilog for collection by Splunk.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- FastEndpoints (>= 8.0.1)
- FastEndpoints.ClientGen.Kiota (>= 8.0.1)
- FastEndpoints.Security (>= 8.0.1)
- FastEndpoints.Swagger (>= 8.0.1)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.4)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 10.0.4)
- Microsoft.Identity.Web (>= 4.5.0)
- Microsoft.Identity.Web.DownstreamApi (>= 4.5.0)
- Microsoft.OpenApi.ApiManifest (>= 3.0.0-preview.1)
- Microsoft.OpenApi.Kiota.Builder (>= 1.30.0)
- Riok.Mapperly (>= 4.3.1)
- Serilog (>= 4.3.1)
- Serilog.AspNetCore (>= 10.0.0)
- Serilog.Sinks.Console (>= 6.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.