Oakrey.Applications.UsbNotifying
7.0.0
See the version list below for details.
dotnet add package Oakrey.Applications.UsbNotifying --version 7.0.0
NuGet\Install-Package Oakrey.Applications.UsbNotifying -Version 7.0.0
<PackageReference Include="Oakrey.Applications.UsbNotifying" Version="7.0.0" />
<PackageVersion Include="Oakrey.Applications.UsbNotifying" Version="7.0.0" />
<PackageReference Include="Oakrey.Applications.UsbNotifying" />
paket add Oakrey.Applications.UsbNotifying --version 7.0.0
#r "nuget: Oakrey.Applications.UsbNotifying, 7.0.0"
#:package Oakrey.Applications.UsbNotifying@7.0.0
#addin nuget:?package=Oakrey.Applications.UsbNotifying&version=7.0.0
#tool nuget:?package=Oakrey.Applications.UsbNotifying&version=7.0.0
Oakrey.Applications.UsbNotifying
A Windows-only .NET library that monitors USB device arrival and removal events through the
Windows PnP notification API and exposes them as an IObservable<UsbEventArgs> stream.
Features
- Wraps
DeviceNotificationListenerfromNefarius.Utilities.DeviceManagement. - Exposes USB events through
IUsbNotifyingService. - Uses a synchronized
ReplaySubject<UsbEventArgs>(1); late subscribers receive the latest event and concurrent callbacks are serialized. - Logs device status, VID, and PID with placeholder-based
Oakrey.Logmessages without exposing a unique serial number. - Contains callback exceptions so they cannot escape into the native listener.
- Provides idempotent
IDisposablecleanup and completes observers during shutdown.
Architecture
classDiagram
IObservable~UsbEventArgs~ <|-- IUsbNotifyingService
IUsbNotifyingService <|.. UsbNotifyingService
IDisposable <|.. UsbNotifyingService
UsbNotifyingService --> DeviceNotificationListener : wraps
UsbNotifyingService --> ReplaySubject~UsbEventArgs~ : publishes through
Public API
public interface IUsbNotifyingService : IObservable<UsbEventArgs> { }
public sealed class UsbNotifyingService : IUsbNotifyingService, IDisposable
{
public UsbNotifyingService();
public IDisposable Subscribe(IObserver<UsbEventArgs> observer);
public void Dispose();
}
UsbEventArgs is provided by Oakrey.Usb and contains:
DeviceInfo.VID: vendor identifierDeviceInfo.PID: product identifierDeviceInfo.DeviceId: symbolic link or serial numberUsbDeviceChangeStatus:InsertedorRemoved
Requirements
- .NET 10 for Windows
Nefarius.Utilities.DeviceManagement5.2.xOakrey.Log.Abstractions3.0.xOakrey.Usb3.2.xSystem.Reactive6.1.x
Installation
dotnet add package Oakrey.Applications.UsbNotifying
Install-Package Oakrey.Applications.UsbNotifying
Usage
Manual instantiation
using Oakrey.Applications.UsbNotifying;
using UsbNotifyingService service = new();
using IDisposable subscription = service.Subscribe(usbEvent =>
{
Console.WriteLine(
$"{usbEvent.UsbDeviceChangeStatus}: VID={usbEvent.DeviceInfo.VID} PID={usbEvent.DeviceInfo.PID}");
});
// Keep the application alive while notifications are required.
Dependency injection
services.AddSingleton<IUsbNotifyingService, UsbNotifyingService>();
public sealed class MyHandler : IDisposable
{
private readonly IDisposable subscription;
public MyHandler(IUsbNotifyingService usbService)
{
subscription = usbService.Subscribe(usbEvent =>
{
if (usbEvent.UsbDeviceChangeStatus == UsbDeviceChangeStatus.Inserted)
{
// Handle device arrival.
}
});
}
public void Dispose()
{
subscription.Dispose();
}
}
Development notes
- The service must run on Windows because
DeviceNotificationListeneruses Windows PnP APIs. ReplaySubject(1)replays the most recent event to a new subscriber.- Subject access is synchronized to protect state when PnP callbacks arrive concurrently.
- Parsing and observer failures are logged and contained at the listener callback boundary.
- Always dispose the service, or its DI container, before application exit.
License
MIT. Copyright (c) Oakrey 2016-present.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- Nefarius.Utilities.DeviceManagement (>= 5.2.0)
- Oakrey.Log.Abstractions (>= 3.0.0)
- Oakrey.Usb (>= 3.2.0)
- System.Reactive (>= 6.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Oakrey.Applications.UsbNotifying:
| Package | Downloads |
|---|---|
|
Oakrey.Applications.Ohm.CAN
Service layer library for managing CAN bus hardware devices and channels. Provides device discovery, connection lifecycle management, reactive IObservable streams for received frames and status changes, per-channel transmit, USB hot-plug detection, and persistent JSON configuration. |
GitHub repositories
This package is not used by any popular GitHub repositories.