Aicrosoft.Extensions.Hosting
8.0.0-beta.251110.1
dotnet add package Aicrosoft.Extensions.Hosting --version 8.0.0-beta.251110.1
NuGet\Install-Package Aicrosoft.Extensions.Hosting -Version 8.0.0-beta.251110.1
<PackageReference Include="Aicrosoft.Extensions.Hosting" Version="8.0.0-beta.251110.1" />
<PackageVersion Include="Aicrosoft.Extensions.Hosting" Version="8.0.0-beta.251110.1" />
<PackageReference Include="Aicrosoft.Extensions.Hosting" />
paket add Aicrosoft.Extensions.Hosting --version 8.0.0-beta.251110.1
#r "nuget: Aicrosoft.Extensions.Hosting, 8.0.0-beta.251110.1"
#:package Aicrosoft.Extensions.Hosting@8.0.0-beta.251110.1
#addin nuget:?package=Aicrosoft.Extensions.Hosting&version=8.0.0-beta.251110.1&prerelease
#tool nuget:?package=Aicrosoft.Extensions.Hosting&version=8.0.0-beta.251110.1&prerelease
Aicrosoft.Extensions.Hosting
Implemented plugin framework and several foundational service base classes. Added logger scope extensions and enhanced dependency injection utilities with extended DI features.
Lastupdate: v8.0.0 2025-11-08 by Fisher
- Aicrosoft.Extensions.Hosting
Ussage
Use in console project
- Create a New dotnet8 Console Project.
- Add the required package references.
NuGet\Install-Package Aicrosoft.Extensions.Hosting
NuGet\Install-Package Aicrosoft.Extensions.NLog
- Modify the content of
Program.csto the following
using Aicrosoft.Logging.NLog;
using Aicrosoft.Services;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var start = Environment.TickCount64;
var logger = NLogHelper.GetLogger();
logger.CaptureGlobalException();
logger.LogTrace($"Loggger was created TimeElapsed:{Environment.TickCount64 - start} ms");
try
{
NLogHelper.De(logger);
NLogHelper.SetConfigurationVariableWhenRelease("logLevel", "Info");
start = Environment.TickCount64;
logger.LogTrace($"Begin Build Host Envirment ...");
//normal flow.
using IHost host = Host.CreateDefaultBuilder(args)
.AddServices(typeof(SuperJobCoreExtensions).Assembly) //add SuperJobs.Core assembly to DI
.AddPluginsService() //enable plugins support.
.AddNLog()
.AddServiceDIDebug() //show DI table. for develop.
.Build()
;
ServiceLocator.ServiceProvider = host.Services;
logger.LogTrace($"End Build. TimeElapsed:{Environment.TickCount64 - start} ms");
await host.RunAsync();
return 0;
}
catch (Exception ex)
{
logger.LogError(ex, "Build and run IHost has a exception");
return -9;
}
finally
{
NLogHelper.Shutdown();
}
Use in Web project
- Create a New dotnet8 Web Project.
- Add the required package references.
NuGet\Install-Package Aicrosoft.Extensions.Hosting
NuGet\Install-Package Aicrosoft.Extensions.NLog
NuGet\Install-Package Aicrosoft.Extensions.Mvc
NuGet\Install-Package Aicrosoft.Extensions.Swagger
- Modify the content of
Program.csto the following
using Aicrosoft.Logging.NLog;
using Aicrosoft.WebAPI.Host;
var start = Environment.TickCount64;
var logger = NLogHelper.GetLogger();
logger.CaptureGlobalException();
logger.LogTrace($"Loggger was created TimeElapsed:{Environment.TickCount64 - start} ms");
try
{
NLogHelper.De(logger);
NLogHelper.SetConfigurationVariableWhenRelease("logLevel", "Info");
start = Environment.TickCount64;
logger.LogTrace($"Begin Build Host Envirment ...");
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddCommandLine(args);
builder.AddServices(); //add other services.
var app = builder.Build();
app.UseServices(); //use other services.
logger.LogTrace($"End Build. TimeElapsed:{Environment.TickCount64 - start} ms");
start = Environment.TickCount64;
await app.RunAsync();
logger.LogInformation($"The running time of this program TimeElapsed:{TimeSpan.FromMilliseconds(Environment.TickCount64 - start)}");
return 0;
}
catch (Exception ex)
{
logger.LogError(ex, "Build and run IHost has a exception");
return -9;
}
finally
{
NLogHelper.Shutdown();
}
Using Microsoft DependencyInjection
This series of Aicrosoft.Extensions.* defaults to using the DI system of Microsoft.Extensions.DependencyInjection.
Service classes marked with these interfaces will be automatically injected into the DI system after calling the .AddServices() extension.
DependencyInjection interfaces
ISingleton- marked as a singleton instance for class.IScoped- marked as a scoped instance for class.ITransient- marked as a transient instance for class.
The way to obtain service classes marked with the above interfaces:
var biz = ServiceProvider.GetService<IBizInterface>(); // no biz service return null.
var biz = ServiceProvider.GetRequiredService<IBizInterface>(); // no biz service throw exception.
Keyed DependencyInjection interfaces
Keyed naming of service classes to more easily obtain the injected service classes from DI.
IKeyedSingleton- marked as a KeyedName singleton instance for class.IKeyedScoped- marked as a KeyedName scoped instance for class.IKeyedTransient- marked as a KeyedName transient instance for class.
The way to obtain service classes marked with the above keyed interfaces:
var biz = ServiceProvider.GetKeyedService<IBizInterface>("keyedName"); // no biz service return null.
var biz = ServiceProvider.GetKeyedRequiredService<IBizInterface>("keyedName"); // no biz service throw exception.
KeyedName Attribute
You can attach KeyedName to the top of a class to implement a custom Keyed name.
public interface IBiz
{
void Run();
}
//IKeyedTransient and keyedName is must
[KeyedName]
public abstract class BizBase : IBiz, IKeyedTransient
{
public virtual void Run()
{
}
}
public sealed class Biz1 : BizBase
{
}
[KeyedName]
public sealed class Biz2 : BizBase
{
}
[KeyedName("Biz1")]
public sealed class Biz3 : BizBase
{
}
//DI Result:
/*
[Transient] IBiz Biz1 [Keyed:Biz1]
[Transient] IBiz Biz2 [Keyed:Biz2]
[Transient] IBiz Biz3 [Keyed:Biz1]
*/
//---------------------------------
//no [KeyedName] attribute will inject by type full name.
public abstract class FooBase : IBiz, IKeyedTransient
{
public virtual void Run()
{
throw new NotImplementedException();
}
}
public sealed class FooBiz1 : FooBase
{
}
[KeyedName]
public sealed class FooBiz2 : FooBase
{
}
[KeyedName("FooBiz33333")]
public sealed class FooBiz3 : BizBase
{
}
//DI Result:
/*
[Transient] IBiz FooBiz1 [Keyed:Aicrosoft.Services.FooBiz1, SuperJobs.Core]
[Transient] IBiz FooBiz2 [Keyed:FooBiz2]
[Transient] IBiz FooBiz3 [Keyed:FooBiz33333]
*/
Automatic injection assebmlies
If you want to inject service classes from a specific assembly into DI, there are several ways to achieve this.
//Automatic injection, a interface can have multiple implementing service classes.
services.AddService(assemblies);
//Automatic injection, a interface can have the first inject service classes.
services.TryAddService(assemblies);
Manual injection service
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, WorkshopService>());
services.TryAddSingleton<IServiceCollection>(services);
var desc = new ServiceDescriptor(regIf, serviceName, implServiceType, GetServiceLifetime(keyedIf));
services.TryAdd(desc);
//Quick register
services.AddTransient<ReportRunner>();
services.AddTransient<IDemo, DemoService>();
services.AddHostedService<Startup>();
Using plugin functionality
Add Plugins Service
using IHost host = Host.CreateDefaultBuilder(args)
.AddServices(true, typeof(SuperJobCoreExtensions).Assembly) //add SuperJobs.Core assembly to DI
.AddNLog()
.AddPluginsService() //enable plugins support.
.Build()
;
Create a plugin module
- Create a new dotnet8 library project and add the reference.
NuGet\Install-Package Aicrosoft.Scheduling
NuGet\Install-Package Aicrosoft.Extensions.Hosting
- Modify
<EnableDynamicLoading>true</EnableDynamicLoading>to the project properties of the plugin assembly to copy all the package DLLs. - Add a
AssemblyName.jsonconfiguration file to the project ,It's will be loaded by default. - It will automatically add service classes marked with the injection interface into DI.
- Add a setup plugin class
//Implementing console plugin configuration.
public class JobAppSetup : PluginSetupBase
{
}
//Implementing web plugin configuration.
public class WebApp1Setup : WebPluginSetupBase
{
}
- If you have custom configuration instances, override the
ConfigureServicesmethod.
public class JobAppSetup : PluginSetupBase
{
protected override void ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services)
{
base.ConfigureServices(hostBuilderContext, services);
services.Configure<DDNSOption>(hostBuilderContext.Configuration.GetSection(DDNSOption.SectionName));
// Add custom service to DI
services.AddSingleton<IMyBiz, MyBiz>();
}
}
- Copy the files generated in the bin directory of the assembly to the
Bin\Plugins\PluginName\directory of you application.
Plugin Configuration
Example:
{
"Plugins": {
"PluginsRoot": "Plugins",
"DisableAutoload": true,
"AssemblyNames": {
"SampleJobs": true,
"DDNSJob": false
}
}
}
PluginsRoot- The root directory name for plugins, default isPlugins.DisableAutoload- When set totrue, it will load plugins based on theAssemblyNamesconfiguration instead of automatically loading them.AssemblyNames- Specifies which plugins to load.- If
false, it will automatically load all plugins under thePluginsRootdirectory.
- If
PluginLoader
It implements plugin functionality. Generally, there is no need to call it directly; simply use the plugin extension methods.
Some ServiceBase classes
ServiceBase
By inheriting the ServiceBase class, there will be two important protected properties:
- IServiceProvider ServiceProvider
- ILogger Logger
DisposeableServiceBase
By inheriting the ServiceBase class, there are some protected properties:
- IServiceProvider ServiceProvider
- ILogger Logger
- string CreateLogScopeTag()
- void SafeFireAndForget(Func<Task> action)
- It also implements the IDisposeable interface for releasing managed and unmanaged code.
Static class ServiceLocator
- This method should only be used when a ServiceProvider instance is needed but cannot be injected.
- Used to obtain a reference to IServiceProvider at the entry point.
- Obtain an instance of the service class through the method T GetRequiredService<T>().
- The reference to the instance of ServiceProvider needs to be set at the entry point.
using IHost host = Host.CreateDefaultBuilder(args)
.AddPluginsService() //enable plugins support.
.Build()
;
ServiceLocator.ServiceProvider = host.Services; //set ServiceProvider reference.
Abstract HostedService
- Service classes that inherit from HostedService can be injected into the Host to start related services.
- Here is a example class named
Startup. You can directly use it for testing, and it will output logs at each level for display testing.
Some Extensions
IConfiguration
//get value from configuration.
var val = config.GetValue("keyName",defaultValue)
IServiceCollection
//Inject assemblies to DI.
service.AddService(Assemblies);
//Inject the assembly where the type represented by T is located.
service.AddService<T>();
//Inject the assembly where the type is located.
service.AddService(type);
//There are also corresponding TryAddService.
IHostBuilder
//Prints the DI table to the console for debugging purposes.
builder.AddServiceDIDebug();
//Dynamically load the Plugins assembly in the form of a plugin.
builder.AddPluginsService();
//Add the Startup servcie of application.
builder.AddHostedService<AzureDeployerService>();
//add SuperJobs.Core assembly to DI.
builder.AddServices(true, typeof(SuperJobCoreExtensions).Assembly);
//Register the assembly where the entry method is located as a Transient service.
builder.AddEntryTransientServices(types);
ILogger
//Set the current ScopeLog context.
var dis = Logger.BeginScopeLog();
//Obtain a ScopeLogger and get the ScopeId
var dis = logger.BeginScopeLog(scopeId: out string scopeId);
//Pass in ScopeId to obtain a ScopeLogger
using (_logger.BeginScopeWithId(traceId));
//Create a Logger of the specified type
Logger = App.Instance.ServiceProvider.CreateLogger(typeof(WinServiceManager));
//Global Exception Handling
Logger.CaptureGlobalException();
Links
| Product | Versions 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. |
-
net8.0
- Aicrosoft.Extensions (>= 8.0.0-beta.251110.1)
- Microsoft.Extensions.Hosting (>= 8.0.1)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on Aicrosoft.Extensions.Hosting:
| Package | Downloads |
|---|---|
|
Aicrosoft.Extensions.AspectCore
Some extensions and implementations related to Aspect-Oriented Programming [AOP]. |
|
|
Aicrosoft.Extensions.Mvc
Extensions for dotnet Mvc Web. |
|
|
Aicrosoft.Extensions.Swagger
Extension for Swagger. |
|
|
Aicrosoft.Extensions.NLog
Log-Related Extensions with NLog. |
|
|
Aicrosoft.Scheduling
> Keywords: `jobsFactory`, `jobs`, `task`, `job`, `routine`, `schedule`, `setup`, `startup`, `interval`, `cronexpression`, `jobagent` [JobsFactory](https://www.nuget.org/packages/Aicrosoft.Scheduling/) is a task scheduling framework that supports multiple types of jobs(tasks) and is easily extensible. It is written in [dotnet8](https://dotnet.microsoft.com/en-us/). An implementation example of the JobsFactory framework: [JobAgent](https://github.com/neatFactory/JobAgent) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.0.0-beta.251110.1 | 345 | 11/10/2025 |