MathCore.Hosting.WPF
1.0.0
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Framework 4.6.1
This package targets .NET Framework 4.6.1. The package is compatible with this framework or higher.
dotnet add package MathCore.Hosting.WPF --version 1.0.0
NuGet\Install-Package MathCore.Hosting.WPF -Version 1.0.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="MathCore.Hosting.WPF" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MathCore.Hosting.WPF" Version="1.0.0" />
<PackageReference Include="MathCore.Hosting.WPF" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MathCore.Hosting.WPF --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MathCore.Hosting.WPF, 1.0.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.
#:package MathCore.Hosting.WPF@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MathCore.Hosting.WPF&version=1.0.0
#tool nuget:?package=MathCore.Hosting.WPF&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MathCore.Hosting.WPF
Инфраструктурный NuGet‑пакет для интеграции Generic Host (Microsoft.Extensions.Hosting) и DI (Microsoft.Extensions.DependencyInjection) в WPF‑приложения без ручного шаблонного кода.
Задачи которые решает
- Единый
IHostвнутри WPFApplication - Простая регистрация сервисов и фоновых служб
- Автоматическое сканирование сборок и подхват сервисов
- Удобный доступ к
IHost,IServiceProvider,IConfiguration - Встраиваемый
ServiceLocatorкак ресурс XAML
Установка
dotnet add package MathCore.Hosting.WPF
Быстрый старт
- Наследуйтесь от
ApplicationHosting:
using MathCore.Hosting.WPF;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace MyApp;
public interface IMyService { void Do(); }
public class MyService : IMyService { public void Do() { /* логика */ } }
public partial class App : ApplicationHosting
{
static App() => ConfigureServices += OnConfigureServices; // подписка один раз при загрузке типа
private static void OnConfigureServices(HostBuilderContext context, IServiceCollection services)
{
services.AddSingleton<IMyService, MyService>(); // регистрация сервисов
}
}
- Корневой тег
App.xaml:
<local:App x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
StartupUri="MainWindow.xaml" />
- Использование сервиса в окне:
using Microsoft.Extensions.DependencyInjection;
using MathCore.Hosting.WPF;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var svc = ApplicationHosting.Services.GetRequiredService<IMyService>();
svc.Do();
}
}
Регистрация через методы
// Добавление (например в статическом конструкторе App)
ServicesAdd((ctx, services) => services.AddSingleton<IOtherService, OtherService>());
// Удаление
ServicesRemove(myConfigurator);
// Очистка
ServicesClear();
Настройка HostBuilder
HostBuilderConfiguratorAdd(builder =>
{
builder.ConfigureLogging(logging => logging.AddDebug());
});
Фоновая служба пример
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _Logger;
public Worker(ILogger<Worker> logger) => _Logger = logger;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while(!stoppingToken.IsCancellationRequested)
{
_Logger.LogInformation("Tick {Time}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
}
}
}
ConfigureServices += (_, services) => services.AddHostedService<Worker>();
Доступ к инфраструктуре
var host = ApplicationHosting.Hosting; // IHost
var services = ApplicationHosting.Services; // IServiceProvider
var configuration = ApplicationHosting.Configuration; // IConfiguration
Авто‑регистрация сервисов
Пакет пытается вызвать services.AddServicesFromAssembly(assembly) для сборок:
- содержащих тип с методом
Main - имеющих атрибуты с именем содержащим
Service(Системные/Interop/Blend/Microsoft сборки пропускаются.) Ошибочные сборки сохраняются вErrorLoadingServicesAssemblies.
Жизненный цикл
OnStartupстроит и запускает хостOnExitкорректно останавливает хост- После запуска списки конфигураторов очищаются чтобы избежать повторной регистрации
Советы
- Подписывайтесь на события один раз (статический конструктор)
- Долгие операции выносите в
BackgroundService - Используйте DI для ViewModel и служб вместо прямого ServiceLocator
Цели
.NET 6–10, .NET Framework 4.6.1–4.8
Лицензия
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0-windows7.0 is compatible. net7.0-windows was computed. net7.0-windows7.0 is compatible. net8.0-windows was computed. net8.0-windows7.0 is compatible. net9.0-windows was computed. net9.0-windows7.0 is compatible. net10.0-windows was computed. net10.0-windows7.0 is compatible. |
| .NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 is compatible. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6.1
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
.NETFramework 4.7
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
.NETFramework 4.8
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
net10.0-windows7.0
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
net6.0-windows7.0
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
net7.0-windows7.0
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
net8.0-windows7.0
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
-
net9.0-windows7.0
- MathCore.Hosting (>= 0.0.6)
- MathCore.WPF (>= 0.0.48.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 396 | 11/19/2025 |
| 0.0.14.2 | 366 | 6/13/2025 |
| 0.0.14.1 | 235 | 2/5/2025 |
| 0.0.14 | 458 | 11/13/2024 |
| 0.0.13.2 | 306 | 11/7/2024 |
| 0.0.13.1 | 315 | 7/3/2024 |
| 0.0.13 | 229 | 6/16/2024 |
| 0.0.12.2 | 196 | 6/12/2024 |
| 0.0.12.1 | 186 | 6/1/2024 |
| 0.0.12 | 215 | 4/25/2024 |
| 0.0.11 | 197 | 4/25/2024 |
| 0.0.10 | 299 | 12/5/2023 |
| 0.0.9.11 | 291 | 8/4/2023 |
| 0.0.9.10 | 276 | 6/15/2023 |
| 0.0.9.9 | 414 | 12/24/2022 |
| 0.0.9.8 | 393 | 12/24/2022 |
| 0.0.9.7 | 381 | 12/24/2022 |
| 0.0.9.6 | 387 | 12/22/2022 |
| 0.0.9.5 | 402 | 12/21/2022 |
| 0.0.9.4 | 402 | 12/20/2022 |
| 0.0.9.3 | 402 | 12/12/2022 |
| 0.0.9.2 | 408 | 12/12/2022 |
| 0.0.9.1 | 433 | 12/8/2022 |
| 0.0.9 | 423 | 12/5/2022 |
| 0.0.8.3 | 446 | 12/1/2022 |
| 0.0.8.2 | 426 | 11/30/2022 |
| 0.0.8.1 | 482 | 11/12/2022 |
| 0.0.8 | 484 | 11/12/2022 |
| 0.0.7.8 | 560 | 9/23/2022 |
| 0.0.7.7 | 551 | 9/18/2022 |
| 0.0.7.6 | 605 | 6/26/2022 |
| 0.0.7.5 | 594 | 6/11/2022 |
| 0.0.7.4 | 568 | 6/10/2022 |
| 0.0.7.3 | 558 | 6/10/2022 |
| 0.0.7.2 | 569 | 6/7/2022 |
| 0.0.7.1 | 562 | 5/19/2022 |
| 0.0.7 | 578 | 5/19/2022 |
| 0.0.6.13 | 2,100 | 4/27/2022 |
| 0.0.6.11 | 586 | 3/13/2022 |
| 0.0.6.10 | 565 | 3/3/2022 |
| 0.0.6.9 | 589 | 2/22/2022 |
| 0.0.6.8 | 581 | 2/13/2022 |
| 0.0.6.7 | 600 | 2/13/2022 |
| 0.0.6.6 | 589 | 2/10/2022 |
| 0.0.6.5 | 577 | 2/9/2022 |
| 0.0.6.4 | 583 | 2/4/2022 |
| 0.0.6.3 | 585 | 1/28/2022 |
| 0.0.6.2 | 456 | 12/24/2021 |
| 0.0.6.1 | 447 | 12/21/2021 |
| 0.0.6 | 426 | 12/21/2021 |
| 0.0.5 | 432 | 12/12/2021 |
| 0.0.4.3 | 749 | 10/8/2021 |
| 0.0.4.2 | 455 | 9/15/2021 |
| 0.0.4.1 | 471 | 9/7/2021 |
| 0.0.4 | 538 | 8/4/2021 |
| 0.0.3 | 600 | 7/29/2021 |
| 0.0.1.3 | 473 | 7/11/2021 |
| 0.0.1.2 | 607 | 7/10/2021 |
| 0.0.1.1 | 481 | 7/6/2021 |
| 0.0.1 | 516 | 7/6/2021 |
| 0.0.0.1 | 505 | 7/6/2021 |
Добавлен метод-расширение для MathCore.WPF.Command, обеспечивающий возможностьб логирования процесса выполнения