Repository navigation
3. Getting started π
This guide covers the minimal setup to get a DirectN application running with Microsoft.Extensions.Hosting.
- A project targeting
net10.0-windows(see Supported Frameworks) - The
Scarlet.Extensions.Hosting.DirectNNuGet package installed
Add a Program.cs file as the entry point for your application:
using Microsoft.Extensions.Hosting;
using Scarlet.Extensions.Hosting.DirectN;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDirectN();
builder.Services.AddDirectNLifetime();
builder.Services.AddDirectNComponent<MainWindowComponent>();
using var host = builder.Build();
host.Run();-
AddDirectN()registers everything needed to run a DirectN application within the Generic Host. There is no application class to write: the host creates DirectN'sApplicationon the UI thread. -
AddDirectNLifetime()is optional. It replaces the console lifetime with one suited to a windowed application, see DirectN Lifetime. -
AddDirectNComponent<T>()adds a component: the object that creates your UI.
Prefer an explicit Main? That works the same way:
public class Program
{
public static void Main(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddDirectN();
builder.Services.AddDirectNLifetime();
builder.Services.AddDirectNComponent<MainWindowComponent>();
using IHost host = builder.Build();
host.Run();
}
}A component is created with its dependencies from the container and initialized on the UI thread before the message loop starts:
using DirectN.Extensions.Utilities;
using Scarlet.Extensions.Hosting.DirectN;
public sealed class MainWindowComponent : IDirectNComponent
{
public void InitializeComponent()
{
var window = new Window("My app");
window.ResizeClient(800, 600);
window.Center();
window.Show();
}
}By default the application exits, and the host stops, when this window is closed. For a tray application without windows, see Application Options.
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<PublishAot>true</PublishAot>
<ApplicationIcon>app.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>The manifest should declare PerMonitorV2 DPI awareness and Common Controls 6 (visual styles, required by task dialogs):
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApp.app"/>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
</assembly>Your DirectN application is now running inside the .NET Generic Host. You immediately gain access to:
-
Dependency Injection β register and resolve services on
builder.Services -
Configuration β use
appsettings.json, environment variables, user secrets, etc. -
Logging β use
ILogger<T>throughout your application -
Hosted Services β add background services via
builder.Services.AddHostedService<T>()
The UI thread is created and started once every hosted service has started, and stopped before any of them is stopped. A hosted service can post to the UI thread at any time (work posted before it exists runs once it has started), but must not wait for it in StartAsync. See Architecture & How It Works.
All registrations are IServiceCollection extensions, so they work the same inside ConfigureServices. The lifetime shortcut is available on IHostBuilder too:
using IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services
.AddDirectN()
.AddDirectNComponent<MainWindowComponent>())
.UseDirectNLifetime()
.Build();
host.Run();- Learn more about components
- Configure shutdown, startup and exit
- Add a system tray icon
- Add thread switching
- HostingMinimal β top-level statements, a window created by a component, and a background service updating it
- HostingTrayIcon β a windowless tray application
Scarlet.Extensions.Hosting.DirectN
Getting Started
Core Features
Extensions
Reference