Muonroi.BuildingBlock 1.3.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package Muonroi.BuildingBlock --version 1.3.7                
NuGet\Install-Package Muonroi.BuildingBlock -Version 1.3.7                
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="Muonroi.BuildingBlock" Version="1.3.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Muonroi.BuildingBlock --version 1.3.7                
#r "nuget: Muonroi.BuildingBlock, 1.3.7"                
#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.
// Install Muonroi.BuildingBlock as a Cake Addin
#addin nuget:?package=Muonroi.BuildingBlock&version=1.3.7

// Install Muonroi.BuildingBlock as a Cake Tool
#tool nuget:?package=Muonroi.BuildingBlock&version=1.3.7                

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. 
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 Muonroi.BuildingBlock:

Package Downloads
MProjectTemplate.API

This is sample project with MBuildingBlock library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.3 38 11/23/2024
1.4.2 72 11/19/2024
1.4.1 70 11/16/2024
1.4.0 70 11/16/2024
1.3.9 83 11/15/2024
1.3.8 70 11/15/2024
1.3.7 78 11/15/2024
1.3.6 90 11/11/2024
1.3.5 84 11/11/2024
1.3.4 87 11/11/2024
1.3.3 79 11/10/2024
1.3.2 71 11/10/2024
1.3.1 74 11/10/2024
1.3.0 78 11/10/2024
1.2.9 75 11/8/2024
1.2.8 81 10/27/2024
1.2.7 89 10/27/2024 1.2.7 is deprecated because it has critical bugs.
1.2.6 91 10/27/2024 1.2.6 is deprecated because it has critical bugs.
1.2.5 85 10/27/2024 1.2.5 is deprecated because it has critical bugs.
1.2.4 94 10/26/2024 1.2.4 is deprecated because it has critical bugs.
1.2.3 82 10/25/2024 1.2.3 is deprecated because it has critical bugs.
1.2.2 108 10/18/2024 1.2.2 is deprecated because it has critical bugs.
1.2.1 111 10/1/2024 1.2.1 is deprecated because it has critical bugs.
1.2.0 127 9/8/2024 1.2.0 is deprecated because it has critical bugs.
1.1.9 105 9/2/2024 1.1.9 is deprecated because it has critical bugs.
1.1.8 118 9/2/2024 1.1.8 is deprecated because it has critical bugs.
1.1.7 112 9/1/2024 1.1.7 is deprecated because it has critical bugs.
1.1.6 109 9/1/2024 1.1.6 is deprecated because it has critical bugs.
1.1.5 115 9/1/2024 1.1.5 is deprecated because it has critical bugs.
1.1.4 109 9/1/2024 1.1.4 is deprecated because it has critical bugs.
1.1.3 112 9/1/2024 1.1.3 is deprecated because it has critical bugs.
1.1.2 116 9/1/2024 1.1.2 is deprecated because it has critical bugs.
1.1.1 108 8/31/2024 1.1.1 is deprecated because it has critical bugs.
1.1.0 109 8/29/2024 1.1.0 is deprecated because it has critical bugs.
1.0.9 112 8/29/2024 1.0.9 is deprecated because it has critical bugs.
1.0.8 148 8/27/2024 1.0.8 is deprecated because it has critical bugs.
1.0.7 137 8/26/2024 1.0.7 is deprecated because it has critical bugs.
1.0.6 116 8/26/2024 1.0.6 is deprecated because it has critical bugs.
1.0.5 135 8/25/2024 1.0.5 is deprecated because it has critical bugs.
1.0.4 133 8/25/2024 1.0.4 is deprecated because it has critical bugs.
1.0.3 141 8/25/2024 1.0.3 is deprecated because it has critical bugs.
1.0.2 138 8/25/2024 1.0.2 is deprecated because it has critical bugs.
1.0.1 132 8/25/2024 1.0.1 is deprecated because it has critical bugs.
1.0.0 231 8/25/2024 1.0.0 is deprecated because it has critical bugs.

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.