Muonroi.BuildingBlock 1.0.0

Suggested Alternatives

Muonroi.BuildingBlock 1.5.9.1

Additional Details

Stable version

There is a newer version of this package available.
See the version list below for details.
dotnet tool install --global Muonroi.BuildingBlock --version 1.0.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local Muonroi.BuildingBlock --version 1.0.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Muonroi.BuildingBlock&version=1.0.0
                    
nuke :add-package Muonroi.BuildingBlock --version 1.0.0
                    

API Library with Dependency Injection and Serilog Configuration

Introduction

This library provides entities such as User, Role, Permission, and Language, and comes with built-in Dependency Injection features, Bearer Token management, JSON handling utilities, string conversion, and localization for multiple languages.

Usage Guide

1. Configuring Program.cs

Here's how to configure your Program.cs file:

using Serilog;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateBootstrapLogger();

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add application configurations
_ = builder.AddAppConfigurations();

// Add Autofac configuration
_ = builder.AddAutofacConfiguration();

// Configure Serilog
_ = builder.Host.UseSerilog(MSerilogAction.Configure);

Log.Information("Starting {ApplicationName} API up", builder.Environment.ApplicationName);

try
{
    IConfiguration configuration = builder.Configuration;
    Assembly assembly = Assembly.GetExecutingAssembly();
    IServiceCollection services = builder.Services;

    // Add services to DI container
    _ = services.AddApplication(assembly);
    _ = services.AddInfrastructure(configuration);
    _ = services.AddDbContextConfigure<ApiDbContext>(configuration);
    _ = services.SwaggerConfig(builder.Environment.ApplicationName);
    _ = services.AddScopeServices(typeof(ApiDbContext).Assembly);
    _ = services.AddValidateBearerToken<MTokenInfo>();

    WebApplication app = builder.Build();

    // Add localization and middleware
    _ = app.AddLocalization(assembly);
    _ = app.UseRouting();
    _ = app.UseAuthentication();
    _ = app.UseAuthorization();
    _ = app.ConfigureEndpoints();
    _ = app.MigrateDatabase();
    _ = app.UseMiddleware<MExceptionMiddleware>();

    await app.RunAsync();
}
catch (Exception ex)
{
    string type = ex.GetType().Name;

    if (type.Equals("StopTheHostException", StringComparison.Ordinal))
    {
        throw;
    }

    Log.Fatal(ex, "Unhandled exception: ", ex.Message);
}
finally
{
    Log.Information("Shut down ", builder.Environment.ApplicationName + " complete");
    await Log.CloseAndFlushAsync();
}

2. Example appsettings.json Configuration

Here's an example of the appsettings.json configuration that can be used with this library:

{
  "ConnectionStrings": {
    "DefaultConnection": ""
  },
  "ApiKey": "",
  "TokenConfigs": {
    "Issuer": "https://exampledomain.com",
    "Audience": "https://searchpartners.exampledomain.com",
    "SigningKeys": "",
    "ExpiryMinutes": 30,
    "PublicKey": "-----BEGIN PUBLIC KEY-----\n-----END PUBLIC KEY-----",
    "PrivateKey": "-----BEGIN RSA PRIVATE KEY-----

-----END RSA PRIVATE KEY-----"
  },
  "PaginationConfigs": {
    "DefaultPageIndex": 1,
    "DefaultPageSize": 10,
    "MaxPageSize": 10
  },
  "ResourceSetting": {
    "ResourceName": "Resources.ErrorMessages",
    "lang": "vi-VN"
  },
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Elasticsearch" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft.AspNetCore": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "Elasticsearch",
        "Args": {
          "nodeUris": "http://localhost:9200",
          "autoRegisterTemplate": true,
          "indexFormat": "notifications_system-{0:yyyy.MM.dd}",
          "inlineFields": true,
          "numberOfShards": 2,
          "numberOfReplicas": 2,
          "autoRegisterTemplateVersion": "ESv7"
        }
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "Properties": {
      "Application": "Notifications"
    }
  },
  "ErrorInAverageEveryNTime": 2,
  "WarningInAverageEveryNTime": 2
}

Main Components

1. Entities

  • User: Manages users.
  • Role: Manages user roles.
  • Permission: Manages access permissions.
  • Language: Manages languages.

2. Dependency Injection

  • Integrates necessary services via Dependency Injection (DI) to manage the application lifecycle.

3. Bearer Token

  • Manages Bearer Token, authentication, and authorization.

4. JSON Utilities

  • Provides utilities for handling JSON, such as converting between strings and objects.

5. Localization

  • Supports language localization within the application for various languages.

Contribution

Please submit pull requests or open issues on GitHub to contribute or report bugs for this project.

License

This library is licensed under the MIT License. Please see the LICENSE file for more details.


Please customize the configuration details (such as ConnectionStrings, ApiKey, TokenConfigs, etc.) according to your application�s requirements.

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.

This package has no dependencies.

Version Downloads Last Updated
1.8.0 50 1/25/2026
1.7.6 34 1/24/2026
1.7.5 34 1/24/2026
1.7.4 37 1/24/2026
1.7.3 59 1/15/2026
1.7.2 50 1/11/2026
1.7.1 41 1/11/2026
1.7.0 56 1/11/2026
1.6.21 50 1/4/2026
1.6.20 52 1/2/2026
1.6.19 52 1/2/2026
1.6.16 52 1/1/2026
1.6.15 48 12/29/2025
1.6.14 50 12/29/2025
1.6.13 46 12/29/2025
1.6.12 46 12/29/2025
1.6.11 44 12/29/2025
1.6.10 48 12/29/2025
1.6.9 45 12/28/2025
1.6.8 46 12/27/2025
1.6.7.1 131 8/17/2025
1.6.7 125 8/17/2025
1.6.6 118 8/17/2025
1.6.5 154 7/27/2025
1.6.4 559 7/23/2025
1.6.3 237 7/20/2025
1.6.2 147 6/22/2025
1.6.1 323 6/10/2025
1.6.0 156 6/7/2025
1.5.11 266 4/29/2025
1.5.10 247 4/17/2025
1.5.9.9 295 3/31/2025
1.5.9.8 530 3/25/2025
1.5.9.7 247 3/17/2025
1.5.9.6 231 3/13/2025
1.5.9.5 207 3/9/2025
1.5.9.4 278 3/6/2025
1.5.9.3 285 3/5/2025
1.5.9.2 194 2/23/2025
1.5.9.1 271 2/18/2025
1.5.9 179 2/17/2025 1.5.9 is deprecated.
1.5.8 177 2/11/2025 1.5.8 is deprecated.
1.5.7 205 2/11/2025 1.5.7 is deprecated.
1.5.6 204 2/9/2025 1.5.6 is deprecated.
1.5.5 219 2/9/2025 1.5.5 is deprecated.
1.5.4 259 2/5/2025 1.5.4 is deprecated.
1.5.3 238 2/3/2025 1.5.3 is deprecated.
1.5.2 244 2/3/2025 1.5.2 is deprecated.
1.5.1.1 262 2/2/2025 1.5.1.1 is deprecated.
1.5.1 221 2/2/2025 1.5.1 is deprecated.
1.5.0 239 1/24/2025 1.5.0 is deprecated.
1.4.9 210 1/24/2025 1.4.9 is deprecated.
1.4.8 195 1/23/2025 1.4.8 is deprecated.
1.4.7 242 11/30/2024 1.4.7 is deprecated.
1.4.6 190 11/30/2024 1.4.6 is deprecated.
1.4.5 232 11/30/2024 1.4.5 is deprecated.
1.4.4 209 11/26/2024 1.4.4 is deprecated.
1.4.3 230 11/23/2024 1.4.3 is deprecated.
1.4.2 200 11/19/2024 1.4.2 is deprecated.
1.4.1 215 11/16/2024 1.4.1 is deprecated.
1.4.0 203 11/16/2024 1.4.0 is deprecated.
1.3.9 229 11/15/2024 1.3.9 is deprecated.
1.3.8 228 11/15/2024 1.3.8 is deprecated.
1.3.7 233 11/15/2024 1.3.7 is deprecated.
1.3.6 225 11/11/2024 1.3.6 is deprecated.
1.3.5 214 11/11/2024 1.3.5 is deprecated.
1.3.4 209 11/11/2024 1.3.4 is deprecated.
1.3.3 234 11/10/2024 1.3.3 is deprecated.
1.3.2 190 11/10/2024 1.3.2 is deprecated.
1.3.1 224 11/10/2024 1.3.1 is deprecated.
1.3.0 238 11/10/2024 1.3.0 is deprecated.
1.2.9 231 11/8/2024 1.2.9 is deprecated.
1.2.8 257 10/27/2024 1.2.8 is deprecated.
1.2.7 251 10/27/2024 1.2.7 is deprecated.
1.2.6 288 10/27/2024 1.2.6 is deprecated.
1.2.5 267 10/27/2024 1.2.5 is deprecated.
1.2.4 291 10/26/2024 1.2.4 is deprecated.
1.2.3 276 10/25/2024 1.2.3 is deprecated.
1.2.2 268 10/18/2024 1.2.2 is deprecated.
1.2.1 310 10/1/2024 1.2.1 is deprecated.
1.2.0 299 9/8/2024 1.2.0 is deprecated.
1.1.9 305 9/2/2024 1.1.9 is deprecated.
1.1.8 277 9/2/2024 1.1.8 is deprecated.
1.1.7 273 9/1/2024 1.1.7 is deprecated.
1.1.6 257 9/1/2024 1.1.6 is deprecated.
1.1.5 299 9/1/2024 1.1.5 is deprecated.
1.1.4 287 9/1/2024 1.1.4 is deprecated.
1.1.3 291 9/1/2024 1.1.3 is deprecated.
1.1.2 295 9/1/2024 1.1.2 is deprecated.
1.1.1 284 8/31/2024 1.1.1 is deprecated.
1.1.0 252 8/29/2024 1.1.0 is deprecated.
1.0.9 280 8/29/2024 1.0.9 is deprecated.
1.0.8 362 8/27/2024 1.0.8 is deprecated.
1.0.7 333 8/26/2024 1.0.7 is deprecated.
1.0.6 287 8/26/2024 1.0.6 is deprecated.
1.0.5 289 8/25/2024 1.0.5 is deprecated.
1.0.4 297 8/25/2024 1.0.4 is deprecated.
1.0.3 305 8/25/2024 1.0.3 is deprecated.
1.0.2 284 8/25/2024 1.0.2 is deprecated.
1.0.1 279 8/25/2024 1.0.1 is deprecated.
1.0.0 454 8/25/2024 1.0.0 is deprecated.

This library will help you whenever you create a new project without needing much configuration, as everything is already set up, from bearer tokens to authentication, and more.