Oakrey.Applications.UsbNotifying
2.0.3
dotnet add package Oakrey.Applications.UsbNotifying --version 2.0.3
NuGet\Install-Package Oakrey.Applications.UsbNotifying -Version 2.0.3
<PackageReference Include="Oakrey.Applications.UsbNotifying" Version="2.0.3" />
<PackageVersion Include="Oakrey.Applications.UsbNotifying" Version="2.0.3" />
<PackageReference Include="Oakrey.Applications.UsbNotifying" />
paket add Oakrey.Applications.UsbNotifying --version 2.0.3
#r "nuget: Oakrey.Applications.UsbNotifying, 2.0.3"
#:package Oakrey.Applications.UsbNotifying@2.0.3
#addin nuget:?package=Oakrey.Applications.UsbNotifying&version=2.0.3
#tool nuget:?package=Oakrey.Applications.UsbNotifying&version=2.0.3
Oakrey.Applications.UsbNotifying
A Windows-only .NET library that monitors USB device arrival and removal events using the
Windows PnP notification API and exposes them as a reactive IObservable<UsbEventArgs> stream.
Features
- Wraps
DeviceNotificationListenerfromNefarius.Utilities.DeviceManagement� no raw Win32 interop required. - Exposes USB events as
IObservable<UsbEventArgs>viaIUsbNotifyingService. - Backed by a
ReplaySubject<UsbEventArgs>(1)� late subscribers receive the most recent event immediately. - Logs device VID, PID, and serial number on every connect/disconnect via
Oakrey.Log. - Implements
IDisposable� properly unregisters listeners and disposes subjects.
Architecture
classDiagram
IObservable~UsbEventArgs~ <|-- IUsbNotifyingService
IUsbNotifyingService <|.. UsbNotifyingService
IDisposable <|.. UsbNotifyingService
UsbNotifyingService --> DeviceNotificationListener : wraps
UsbNotifyingService --> ReplaySubject~UsbEventArgs~ : broadcasts via
Public API
// Service interface � subscribe to receive USB events
public interface IUsbNotifyingService : IObservable<UsbEventArgs> { }
// Concrete service � register with DI or instantiate directly
public sealed class UsbNotifyingService : IUsbNotifyingService, IDisposable
{
public UsbNotifyingService();
public IDisposable Subscribe(IObserver<UsbEventArgs> observer);
public void Dispose();
}
UsbEventArgs is provided by Oakrey.Usb and carries:
DeviceInfo.VID� vendor identifierDeviceInfo.PID� product identifierDeviceInfo.DeviceId� symbolic link / serial numberUsbDeviceChangeStatus�INSERTEDorREMOVED
Requirements
- .NET 10 (Windows)
Nefarius.Utilities.DeviceManagement5.2.xOakrey.Usb3.0.xOakrey.Log2.0.xSystem.Reactive6.1.x
Installation
.NET CLI
dotnet add package Oakrey.Applications.UsbNotifying
Package Manager Console
Install-Package Oakrey.Applications.UsbNotifying
NuGet Package Manager UI
Search for Oakrey.Applications.UsbNotifying in Visual Studio under
Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
Usage
Manual instantiation
using Oakrey.Applications.UsbNotifying;
await using UsbNotifyingService service = new();
IDisposable subscription = service.Subscribe(e =>
{
Console.WriteLine($"{e.UsbDeviceChangeStatus}: VID={e.DeviceInfo.VID} PID={e.DeviceInfo.PID}");
});
// Keep the application alive; dispose subscription when done.
subscription.Dispose();
Dependency injection
services.AddSingleton<IUsbNotifyingService, UsbNotifyingService>();
public sealed class MyHandler
{
private readonly IDisposable subscription;
public MyHandler(IUsbNotifyingService usbService)
{
subscription = usbService.Subscribe(e =>
{
if (e.UsbDeviceChangeStatus == UsbDeviceChangeStatus.INSERTED)
{
// handle device arrival
}
});
}
}
Development notes
- The service must run on Windows �
DeviceNotificationListenercalls Win32 PnP APIs. The target frameworknet10.0-windowsenforces this at build time. ReplaySubject(1)means any subscriber that registers after the last event still receives it immediately. Use a freshSubject<T>instead if that behaviour is undesirable.DeviceNotificationListener.StartListenis called forDeviceInterfaceIds.UsbDevice. To monitor a different device class, derive from or wrapUsbNotifyingServiceand pass a differentGuidtoStartListen.- Always dispose the service (or the DI container) before application exit to unregister the PnP listener cleanly.
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 (>= 2.0.0)
- Oakrey.Usb (>= 3.0.3)
- 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.