CG.Infrastructure.Core 3.10.8

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

CG.Infrastructure.Core

NuGet .NET

A core infrastructure library that provides essential service registration and dependency injection capabilities for .NET applications.

Overview

CG.Infrastructure.Core is a foundational library that simplifies service registration and dependency injection in .NET applications. It provides automatic service discovery and registration based on interfaces, making it easier to maintain clean architecture and reduce boilerplate code.

Features

  • Automatic Service Registration: Automatically discovers and registers services that implement specific interfaces
  • Assembly Scanning: Intelligently scans assemblies for service implementations
  • Dependency Injection Integration: Seamlessly integrates with Microsoft.Extensions.DependencyInjection
  • Infrastructure Assembly Support: Automatically discovers Infrastructure and Platform assemblies
  • Test Assembly Support: Includes support for test assemblies during development
  • Detailed Logging: Optional detailed logging for debugging and development purposes
  • Assembly Utilities: Provides utilities for assembly filtering and reflection operations

Requirements

  • .NET 9.0 or later
  • Microsoft.Extensions.DependencyInjection.Abstractions 9.0.7+
  • Scrutor 6.1.0+

Installation

dotnet add package CG.Infrastructure.Core

Or add the following to your .csproj file:

<PackageReference Include="CG.Infrastructure.Core" Version="3.10.6" />

Quick Start

1. Define Your Services

Create services that implement the IService interface:

using Infrastructure.Core.Interfaces;

public class UserService : IService
{
    public async Task<User> GetUserAsync(int id)
    {
        // Implementation
    }
}

public class EmailService : IService
{
    public async Task SendEmailAsync(string to, string subject, string body)
    {
        // Implementation
    }
}

2. Register Services

In your Program.cs or Startup.cs:

using Infrastructure.Core.Extensions;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Register all services that implement IService
builder.Services.RegisterInfraCoreServices();

// Or register services for a specific interface
builder.Services.RegisterServices<IMyCustomInterface>();

var app = builder.Build();

3. Use Your Services

[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
    private readonly UserService _userService;
    private readonly EmailService _emailService;

    public UserController(UserService userService, EmailService emailService)
    {
        _userService = userService;
        _emailService = emailService;
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<User>> GetUser(int id)
    {
        var user = await _userService.GetUserAsync(id);
        return Ok(user);
    }
}

Advanced Usage

Custom Interface Registration

Register services for any interface (not just IService):

// Register all services that implement IRepository
builder.Services.RegisterServices<IRepository>();

// Register all services that implement IValidator
builder.Services.RegisterServices<IValidator>();

Detailed Logging

Enable detailed logging to see which assemblies are being scanned:

builder.Services.RegisterInfraCoreServices(detailed: true);

This will output information about:

  • Which interface is being registered
  • Total assemblies loaded
  • Infrastructure assemblies discovered
  • Platform assemblies discovered

Test Assembly Support

Include test assemblies during development:

builder.Services.RegisterInfraCoreServices(
    detailed: true, 
    testAssemblyName: "MyProject.Tests"
);

Generic Type Handling

For generic types, use the non-generic overload:

// This will throw an ArgumentException
// builder.Services.RegisterServices<IRepository<>>();

// Use this instead
builder.Services.RegisterServices(typeof(IRepository<>));

Assembly Utilities

The library provides utilities for assembly-related operations, particularly useful for reflection-based scenarios:

using Infrastructure.Core.Utilities;

// Check if an assembly should be skipped during reflection operations
var shouldSkip = AssemblyUtilities.ShouldSkipAssembly(assembly);

// This is useful when scanning assemblies for types, views, or view models
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    if (AssemblyUtilities.ShouldSkipAssembly(assembly))
        continue;
        
    // Process assembly types...
}

The ShouldSkipAssembly method filters out problematic assemblies that commonly cause issues during reflection operations, such as:

  • Microsoft JSInterop assemblies
  • Serilog browser console sinks
  • ASP.NET Core Components
  • WebView2 assemblies
  • And other assemblies that may cause reflection exceptions

How It Works

The library uses reflection and assembly scanning to automatically discover services:

  1. Assembly Discovery: Scans the calling assembly, executing assembly, entry assembly, and interface assembly
  2. Referenced Assembly Scanning: Recursively scans Infrastructure and Platform assemblies
  3. Service Registration: Uses Scrutor to register discovered services with scoped lifetime
  4. Interface Mapping: Registers services as both concrete types and implemented interfaces

Service Lifetime

All services are registered with Scoped lifetime by default, which means:

  • A new instance is created for each HTTP request in web applications
  • A new instance is created for each scope in console applications
  • Services are disposed when the scope is disposed

Dependencies

  • Microsoft.Extensions.DependencyInjection.Abstractions: Provides the core DI abstractions
  • Scrutor: Enables assembly scanning and service registration

Contributing

This library is part of the CG Infrastructure suite. For contributions, please follow the established patterns and ensure all tests pass.

License

This project is licensed under the terms specified in the LICENSE file.

Version History

  • 3.10.7: Current stable release

    • Added AssemblyUtilities class for assembly filtering during reflection operations
    • Provides ShouldSkipAssembly method to filter problematic assemblies
    • Supports .NET 9.0
    • Includes comprehensive service registration capabilities
    • Provides detailed logging and debugging support
  • 3.10.6: Previous release

    • Supports .NET 9.0
    • Includes comprehensive service registration capabilities
    • Provides detailed logging and debugging support

Support

For issues, questions, or contributions, please refer to the project repository or contact the maintainers.

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 (15)

Showing the top 5 NuGet packages that depend on CG.Infrastructure.Core:

Package Downloads
CG.Infrastructure.Services

Infra Services library with shared services

CG.Infrastructure.Data

Infra Data library with dbup and dapper

CG.Infrastructure.Logging

Infra Logging library with Serilog setup, extensions and database contexts

CG.Infrastructure.Mediator

Infra Mediator library with MediatR setup, extensions and database contexts

CG.Platform.Presentation

Platform Presentation library with shared services

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.10.8 248 8/13/2025
3.10.7 167 8/10/2025
3.10.6 139 8/10/2025
3.10.5 548 7/22/2025
3.10.4 258 6/18/2025
3.10.3 164 6/17/2025
3.10.2 163 6/16/2025
3.10.1 229 6/3/2025
3.10.0 172 6/3/2025
3.9.0 286 12/10/2024
3.0.1 377 3/19/2024
3.0.0 237 2/19/2024
2.0.0 900 5/15/2023
1.0.2 744 6/21/2022
1.0.1 484 5/26/2022
1.0.0 3,873 5/26/2022