Aicrosoft.Extensions.Hosting 8.5.0

dotnet add package Aicrosoft.Extensions.Hosting --version 8.5.0                
NuGet\Install-Package Aicrosoft.Extensions.Hosting -Version 8.5.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="Aicrosoft.Extensions.Hosting" Version="8.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aicrosoft.Extensions.Hosting --version 8.5.0                
#r "nuget: Aicrosoft.Extensions.Hosting, 8.5.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.
// Install Aicrosoft.Extensions.Hosting as a Cake Addin
#addin nuget:?package=Aicrosoft.Extensions.Hosting&version=8.5.0

// Install Aicrosoft.Extensions.Hosting as a Cake Tool
#tool nuget:?package=Aicrosoft.Extensions.Hosting&version=8.5.0                

How to use Aicrosoft.Extensions.Hosting

通用主机,加入了一些简易的扩展,比如字符串处理,自动模块注入等。还能避免引入多个MS的包。只用这一个就好了(不用再次引入MS的Hosting) A general-purpose host, adding some simple extensions, such as string processing, automatic module injection, etc. It can also avoid introducing multiple MS packages. Just use this one (no need to introduce MS Hosting again)

该模块主要用于基于Console的程序,基于Web的反而用不上了。

模块引入及初始化

How to use

  1. add pakcage NLog.Extensions.Logging
  2. add nlog.config
  3. chanage the program code and add extesion of host

Program.cs file

<details> <summary>The Application start</summary>


using Aicrosoft.Logging;
using Microsoft.Extensions.Hosting;
using NLogAIApp;

var start = Environment.TickCount64;
var logger = LogHelper.GetCurrentClassLogger();
logger.Trace($"Loggger was created TimeElapsed:{Environment.TickCount64 - start} ms");

try
{
    start = Environment.TickCount64;
    logger.Trace($"Begin Build Host Envirment ...");
    using IHost host = Host.CreateDefaultBuilder(args)
        .AddAppServices()
        .Build();
    logger.Trace($"End Build. TimeElapsed:{Environment.TickCount64 - start} ms");
    await host.RunAsync();
    return 0;
}
catch (Exception ex)
{
    logger.Error(ex, "Build and run IHost has a exception");
    return -9;
}
finally
{
    LogHelper.Shutdown();
}



</details>


Startup.cs file

<details> <summary>The Application's Startup classs </summary>


using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NLogAIApp.Options;
using NLogAIApp.Services;
using System.Text;

namespace NLogAIApp;

public class Startup : HostedService
{
    private readonly IHelloWorldService helloWorldService;
    private readonly TasksOption tasksSetting;

    public Startup(
        IHostApplicationLifetime appLifetime, ILoggerFactory loggerFactory, IServiceProvider serviceProvider
        , IHelloWorldService helloService
        , IOptions<TasksOption> options
        ) : base(appLifetime, loggerFactory, serviceProvider)
    {
        helloWorldService = helloService;
        tasksSetting = options.Value;
        Logger.LogDebug($"setting json value is :{tasksSetting.ToJson()}");
    }
   

    public override async Task StartAsync(CancellationToken cancellationToken)
    {
        Logger.LogDebug("1. StartAsync has been called.");

        Console.WriteLine();
        Console.WriteLine("-------> LOGGING DEBUG TESTING BEGIN <--------");
        await helloWorldService.SayHelloAsync();
        Console.WriteLine("-------> LOGGING DEBUG TESTING END <--------");
        Console.WriteLine();

        Console.WriteLine($"------------Configuration demo--------------------");
        var cfg = ServiceProvider.GetRequiredService<IConfiguration>();
        var val1 = cfg.GetValue<int>("Tasks:Interval");
        Console.WriteLine($"cfg.GetValue<int>(\"Tasks:Interval\") = {val1}");
        Console.WriteLine($"-------------------------------------------------");


        //It's stop the application.
        AppLifetime.StopApplication();
    }

    public override async Task StopAsync(CancellationToken cancellationToken)
    {
        Logger.LogDebug("4. StopAsync has been called.");
        await Task.CompletedTask;
        //await _appLifetime.ShutdownAsync();
        return;
    }

}

</details>


ConfigureExtensions.cs file

<details> <summary>The Application's Host Extensions </summary>


using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NLog.Extensions.Logging;
using NLogAIApp.Options;

namespace NLogAIApp;

internal static class ConfigureExtensions
{

    public static IHostBuilder AddAppServices(this IHostBuilder hostBuilder)
    {
        hostBuilder
            .AddAppSetting()
            .AddServices()
            .AddNLog()
            //.UseAop()
            //.UseAopDI()
            .AddHostedService<Startup>()
            ;

        return hostBuilder;
    }


    /// <summary>
    /// config.json register
    /// </summary>
    /// <param name="hostBuilder"></param>
    public static IHostBuilder AddAppSetting(this IHostBuilder hostBuilder)
    {
        hostBuilder
            //.ConfigureAppConfiguration((hostContext, configuration) =>
            //{
            //    //configuration.Sources.Clear(); //在CreateDefaultBuilder(args)时如果有参数会被清除
            //    //configuration.AddCommandLine(args);
            //    var env = hostContext.HostingEnvironment;
            //    configuration
            //        .AddJsonFile("./appsettings.json")
            //        .AddJsonFile($"./appsettings.{env.EnvironmentName}.json", true, true);
            //})
            .ConfigureServices((hostContext, services) =>
            {
                var config = hostContext.Configuration;
                services.Configure<TasksOption>(config.GetSection(TasksOption.SectionName));

                // 可以直接取得上面配置的值
                var tasksOption = config.GetSection(TasksOption.SectionName).Get<TasksOption>();

            });

        return hostBuilder;
    }

}

</details>

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

Showing the top 5 NuGet packages that depend on Aicrosoft.Extensions.Hosting:

Package Downloads
Aicrosoft.Extensions.AspectCore

Package Description

Aicrosoft.Extensions.Mvc

Package Description

Aicrosoft.Extensions.Swagger

Package Description

Aicrosoft.Extensions.NLog

Package Description

Aicrosoft.Scheduling

A multifunctional and extensible task scheduling framework

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.5.0 143 10/12/2024
8.3.0 173 9/10/2024
8.2.0 148 8/7/2024
6.4.0 550 11/20/2023
6.3.2 322 11/1/2023
6.2.1 331 8/25/2023
6.1.7 360 7/25/2023
6.1.5 538 3/13/2023
6.1.4 465 3/11/2023
6.1.3 534 2/21/2023
6.1.1 555 12/1/2022
6.1.1-beta.221203.1 183 12/3/2022
1.2.1-dev6-1129 689 11/29/2022
1.2.1-dev5-1115 200 11/29/2022
1.2.1-dev4-1114 224 11/14/2022
1.0.0 175 5/17/2023
0.2.1.1 549 11/10/2022
0.2.1-dev2-1110 214 11/10/2022