Kristam.Utility 1.0.0

dotnet add package Kristam.Utility --version 1.0.0
                    
NuGet\Install-Package Kristam.Utility -Version 1.0.0
                    
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="Kristam.Utility" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kristam.Utility" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Kristam.Utility" />
                    
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 Kristam.Utility --version 1.0.0
                    
#r "nuget: Kristam.Utility, 1.0.0"
                    
#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 Kristam.Utility@1.0.0
                    
#: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=Kristam.Utility&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Kristam.Utility&version=1.0.0
                    
Install as a Cake Tool

Utility Extension Library This library provides a set of utility and extension methods to simplify common service configurations in ASP.NET Core applications. It uses Autofac for dependency injection and AutoMapper for object-to-object mapping, offering a convention-based approach to register services and configure standard application components like CORS, API versioning, Swagger, JWT authentication, and database contexts. Key Features

  • Convention-Based Service Registration: Automatically registers services based on custom attributes ([Scoped], [Singleton], [Transient]) using Autofac.
  • AutoMapper Integration: Simplifies the setup of AutoMapper profiles across assemblies.
  • ASP.NET Core Configuration: Provides convenient extension methods to configure: o CORS o API Versioning o Swagger/OpenAPI with JWT authentication support o JWT Bearer Authentication o Entity Framework Core database contexts for both MySQL and MSSQL

Classes Overview

  1. ContainerBuilderExtensions This static class contains extension methods for Autofac's ContainerBuilder. It automates the process of registering services and AutoMapper profiles by scanning assemblies. Methods
  • AutoRegisterTypes(this ContainerBuilder builder, string businessUnit): This is the main method for automatic service registration. o It scans all loaded assemblies whose full names start with the specified businessUnit string. o It finds interfaces and their concrete implementations within these assemblies. o It registers each implementation with Autofac based on the lifetime specified by a custom attribute ([Scoped], [Singleton], or [Transient]). o It also registers AutoMapper profiles found in the scanned assemblies. o Lifetime Resolution:
  • [SingletonAttribute]: Registered as a single instance (SingleInstance()).
  • [TransientAttribute]: Registered as per-dependency (InstancePerDependency()).
  • No Attribute: Defaults to scoped (InstancePerLifetimeScope()).
  1. ServiceCollectionExtensions This static class provides extension methods for IServiceCollection, the standard dependency injection container in ASP.NET Core. These methods encapsulate common setup tasks, making Program.cs or Startup.cs cleaner and more readable. Methods
  • AddCorsPolicys(this IServiceCollection services): Adds a default CORS policy that allows any origin, header, and method.
  • AddApiVersioningConfigurations(this IServiceCollection services): Configures API versioning with a default version of 1.0 and uses the api-version header for versioning.
  • AddJsonSerializerOptions(this IServiceCollection services): Configures JsonSerializerOptions to handle circular references by preserving object references. You can add more options here as needed.
  • AddSwaggerGenConfigurations(this IServiceCollection services, string title, string version): Sets up Swagger (OpenAPI) documentation. It also includes configuration for JWT Bearer token authentication in the Swagger UI.
  • AddDbContexts<TContext>(this IServiceCollection services) where TContext : DbContext: Registers a database context as a scoped service.
  • AddMySQLDatabaseContextPools<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionStr, string migrationsAssembly) where TContext : DbContext: Configures a MySQL database context pool using the provided connection string and migration assembly.
  • AddMSSQLDatabaseContextPools<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionStr, string migrationsAssembly) where TContext : DbContext: Configures a SQL Server database context pool using the provided connection string and migration assembly.
  • AddJwtAuthentications(this IServiceCollection services, IConfiguration configuration): Configures JWT Bearer authentication, validating the token against settings from IConfiguration.

How to Use Setup in Program.cs or Startup.cs

  1. Configure IServiceCollection: You can chain the extension methods from ServiceCollectionExtensions to set up your application's services. C# // Program.cs or Startup.cs var builder = WebApplication.CreateBuilder(args); var services = builder.Services; var configuration = builder.Configuration;

services.AddCorsPolicys() .AddApiVersioningConfigurations() .AddSwaggerGenConfigurations("My API", "v1") .AddJwtAuthentications(configuration) .AddMSSQLDatabaseContextPools<MyDbContext>(configuration, "DefaultConnection", "MyAssembly"); 2. Configure Autofac: You can use the ContainerBuilderExtensions.AutoRegisterTypes method within your Autofac module or in the ConfigureContainer method of a Startup class. This will automatically find and register services based on their attributes. C# // Example: Autofac setup builder.AutoRegisterTypes("YourProject.BusinessUnit"); Using Lifetime Attributes Apply the custom attributes ([Scoped], [Singleton], [Transient]) to your service implementations to control their lifetime in the dependency injection container. C# // Example of a Scoped service [Scoped] public class MyService : IMyService { // ... }

// Example of a Singleton service [Singleton] public class MySingletonService : IMySingletonService { // ... }

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 218 9/1/2025