Application composition and lifecycle
Core-only console application
Create a .NET console project and install only GeWuYou.GFramework.Core as a direct dependency. The package supplies its own transitive dependencies. Copy the Core lifecycle example into the new project, replace Program.cs with this line, and run dotnet run.
dotnet new console -n CoreLifecycle
cd CoreLifecycle
dotnet add package GeWuYou.GFramework.Core --version 1.1.0await MinimalApp.CoreOnlyLifecycle.RunAsync().ConfigureAwait(false);// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Lifecycle;
using GFramework.Core.Architectures;
using Microsoft.Extensions.DependencyInjection;
namespace MinimalApp;
/// <summary>Runs the lifecycle using only the Core package and its dependencies.</summary>
internal static class CoreOnlyLifecycle
{
/// <summary>Starts the architecture, runs one operation and joins business stop and disposal.</summary>
internal static async Task RunAsync()
{
var app = new ConsoleArchitecture();
await app.InitializeAsync().ConfigureAwait(false);
try
{
Console.WriteLine("Application work");
}
finally
{
await app.DestroyAsync().ConfigureAwait(false);
}
}
/// <summary>Composition root for the console host.</summary>
private sealed class ConsoleArchitecture : Architecture
{
/// <inheritdoc />
protected override void ConfigureServices(IServiceCollection services)
{
services.AddComponent<Worker>();
}
}
/// <summary>Receives startup and shutdown callbacks from Architecture.</summary>
private sealed class Worker : IInitializable, IDestroyable, IDisposable
{
/// <inheritdoc />
public void Initialize() => Console.WriteLine("Started");
/// <inheritdoc />
public void Destroy() => Console.WriteLine("Stopped");
/// <inheritdoc />
public void Dispose() => Console.WriteLine("Disposed");
}
}Expect Started, Application work, Stopped, and Disposed in that order. The IDestroyable callback stops business work, then the DI provider disposes the instance it created. When using a long-lived host, stop accepting work and join active operations before awaiting DestroyAsync at the host's shutdown boundary.
Register and start
Register dependencies in ConfigureServices. Use standard DI registration for ordinary classes and AddComponent for services that need initialization or shutdown. Pass dependencies through constructors and use one registration key per component.
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Core.Architectures;
using Microsoft.Extensions.DependencyInjection;
namespace MinimalApp;
/// <summary>
/// 示例架构:只使用标准 DI 注册普通服务,不引入 Model/System/Utility 角色。
/// Architecture 在这里只提供实例级的初始化/停止编排与日志工厂所有权。
/// </summary>
internal sealed class OrderArchitecture : Architecture
{
/// <summary>描述服务注册;此阶段只记录描述,不解析实例。</summary>
/// <param name="services">当前架构将构建和拥有的服务集合。</param>
protected override void ConfigureServices(IServiceCollection services)
{
// 普通服务:无角色标记、无 ContextAware、无基类要求。
services.AddSingleton<OrderService>();
services.AddSingleton<OrderProcessor>();
}
/// <summary>宿主准备完成后通过标准解析使用普通服务。</summary>
protected override void OnStarted()
{
var processor = Services.GetRequiredService<OrderProcessor>();
processor.Process("boot");
}
/// <summary>提交一条示例订单并输出处理日志。</summary>
/// <param name="orderId">订单编号。</param>
public void PlaceDemoOrder(string orderId)
{
var processor = Services.GetRequiredService<OrderProcessor>();
processor.Process(orderId);
}
}Lifecycle
- Await
InitializeAsyncto create the provider, start components in order, and connect the host. - Resolve services and enable user operations when the host is ready. Create a scope for operations that use scoped services.
- Await
DestroyAsyncon exit. Components stop in reverse order, then the host detaches and the provider is released.
order sorts startup callbacks from lowest to highest, with registration order breaking ties. Put work that requires a running component in a lifecycle callback.
Ownership
DI-created disposable services are released with their provider or scope. If you register an existing component instance, its caller owns disposal and Architecture runs the registered shutdown work.
Related topics: asynchronous initialization and dependency injection.