dotnet aspnet 6 usb device hot plug

Hello

I try to create an aspnet webapi container to detect all available usb devices on my host device.
It works to connect a USB device from the container that was already connected when the container was started. But if i disconnect and reconnect the device is not longer available at the container.

Also my primary logic cannot reach the serial port after reconnect /dev/ttyACM0.

For my docker container i use the official dotnet image from microsoft and install libc6-dev and libusb

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal
RUN apt-get update && apt-get install -y libc6-dev libusb-*

EXPOSE 5000/tcp

ENV ASPNETCORE_URLS "http://*:5000"
CMD dotnet /app/usbtest.dll

docker-compose.yml i think privileged and devices should be enough here
UDEV is according to my research, only relevant for Balena’s own containers.

  usbtest:
    privileged: true
    tty: true
    devices:
      - '/dev:/dev'
    image: registry.gitlab.company.com/test/test.balenausb:main-100020
    labels:
      io.balena.features.kernel-modules: true
    network_mode: host
    ports:
        - '5000'
    environment:
      UDEV: 1

In my WebApi Project i use the LibUsbDotNet package. → LibUsbDotNet 3.0.97-alpha

My Controller in the WebApi looks like this

[Route("UsbDiscovery")]
[HttpGet]
public UsbDevice[] UsbDiscovery()
{
    using (var context = new UsbContext())
    {
        var allDevices = context.List();
        return allDevices.Select(o =>
        {
            try
            {
                if (!o.TryOpen())
                {
                    return new UsbDevice
                    {
                        VendorId = o.VendorId,
                        ProductId = o.ProductId,
                    };
                }

                return new UsbDevice
                {
                    MoreDataAvailable = true,
                    VendorId = o.VendorId,
                    ProductId = o.ProductId,
                    Manufacturer = o.Info.Manufacturer,
                    Product = o.Info.Product,
                    SerialNumber = o.Info.SerialNumber
                };
            }
            finally
            {
                if (o.IsOpen)
                {
                    o.Close();
                }
                o.Dispose();
            }
        }).ToArray();
    }
}

The response look like this after reconnect

[
  {
    "moreDataAvailable": false,
    "manufacturer": null,
    "product": null,
    "serialNumber": null,
    "vendorId": 1027,
    "productId": 24577
  }
]

On first start with the connected device i have this

[
  {
    "moreDataAvailable": true,
    "manufacturer": "FTDI",
    "product": "USB Serial Converter",
    "serialNumber": "ftE2S9L7",
    "vendorId": 1027,
    "productId": 24577
  }
]

If i monitor my container with udevadm monitor i can see the reconnect events

root@66b43f8:/app# udevadm monitor
KERNEL[5492.312861] add /devices/pci0000:00/0000:00:14.0/usb1/1-5 (usb)
KERNEL[5492.322155] add /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0 (usb)
KERNEL[5492.322300] add /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0/tty/ttyACM0 (tty)
KERNEL[5492.322376] bind /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0 (usb)
KERNEL[5492.322456] add /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.1 (usb)
KERNEL[5492.322523] bind /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.1 (usb)
KERNEL[5492.322572] bind /devices/pci0000:00/0000:00:14.0/usb1/1-5 (usb)
KERNEL[5510.786568] remove /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0/tty/ttyACM0 (tty)
KERNEL[5510.786635] unbind /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.1 (usb)
KERNEL[5510.786898] unbind /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0 (usb)
KERNEL[5510.789572] remove /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0 (usb)
KERNEL[5510.789615] remove /devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.1 (usb)
KERNEL[5510.789653] unbind /devices/pci0000:00/0000:00:14.0/usb1/1-5 (usb)
KERNEL[5510.789688] remove /devices/pci0000:00/0000:00:14.0/usb1/1-5 (usb)

UPDATE 2022-08-31
With switch to the alpine image i can now access the device after reconnect but the usbcontext still does not provide data about the manufacturer and the product.

I have no change the runtime image to the official from balenalib for dotnet and now it works.

Dockerfile

FROM balenalib/generic-dotnet:6.0-aspnet-run

EXPOSE 5000/tcp

ENV ASPNETCORE_URLS "http://*:5000"
CMD dotnet /app/usbtest.dll

docker-compose.yml

  usbtest:
    privileged: true
    image: registry.gitlab.company.com/test/test.balenausb:main-100020
    ports:
        - '5000'
    environment:
      UDEV: 1