I've ported a web application to .NET Core and I'm finding that it's able to communicate with a microcontroller over USB when running on Windows but not when running on Linux. I'm trying to work out:
Why this is the case (is it a bug in the libraries I'm using, or have I misconfigured the Linux environment?)
How I can resolve or work around it (alternative libraries, changes to environment, etc.)
If anyone has successfully managed to create an application based on .NET Core that can communicate over USB in a platform-agnostic manner, I'd be very grateful for your input.
For reference in the remainder of this post, these are some of the details about my current environment:
I formerly had a .NET Framework-based web service that I was using to control a microcontroller. Being based on the .NET Framework, it was restricted to running on Windows. In order to achieve cross-platform support I began porting the application to .NET Core. That task is now more-or-less complete - for instance, I can now build and run my application as a Docker service which I imagine will be of great benefit.
My application is split across multiple projects, but for the purpose of this post I'll only address two:
The ASP.NET Core-based web service project which adapts the core functionality of the application to be controllable via web communication. 1 It ties everything else together and is what I actually execute using dotnet <<application.dll>>
.
A USB communications service project which handles communication with my USB device. 2 It currently uses the CoreCompat.LibUsbDotNet library (v2.2.8-r101), which acts as a cross-platform C# adapter over the WinUSB
and libusb
libraries on Windows and Unix respectively. This project is distributed as a Nuget package that's consumed by the web service, and is where the problem lies.
The application runs fine when I run it directly under Windows. However, if I try to run it on a Linux virtual machine or as a Docker service, the web service will fail to initialise with an error complaining that the libusb-1.0 library
could not be located. 3
Based on this error message, I've tried inspecting the environment of both the Linux VMs and Docker containers that I've attempted running the application on. 4
After mounting my USB device on the Docker virtual machine and running a container based off my web app image in privileged mode, I can confirm that it sees my USB device:
root@19e8929e1814:/app# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2b87:0001
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
I also confirm that libusb-1.0
is installed 5 and check that it's available from the ldconfig
cache:
root@19e8929e1814:/app# ldconfig -p | grep libusb
libusb-1.0.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libusb-1.0.so.0
libusb-1.0.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libusb-1.0.so
libusb-0.1.so.4 (libc6,x86-64) => /lib/x86_64-linux-gnu/libusb-0.1.so.4
libusb-0.1.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libusb-0.1.so.4
Based on this, I'm not sure why CoreCompat.LibUsbDotNet
throws an exception about not being able to find libusb-1.0
, and can only assume it's a bug in the library.
Has anyone successfully managed to use this library for communication under a Unix environment? Alternatively, has anyone found another way of communicating with USB devices in a platform-agnostic manner for a .NET Core-based application?
[1] project.json
for web service project
{
"dependencies": {
"<<Company>>.Communications.<<Product>>Usb": "0.4.9",
"<<Company>>.<<Product>>Web.Core": {
"target": "project"
},
"<<Company>>.<<Product>>WebComponentPackage": "0.4.9-beta0002",
"Autofac": "4.2.1",
"Common.Logging": "3.4.0-Beta2",
"Microsoft.AspNetCore.Diagnostics": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0-preview1-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.AspNetCore.WebSockets.Server": "0.1.0",
"Microsoft.Extensions.DependencyModel": "1.1.0-preview1-001100",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.NETCore.App": {
"version": "1.1.0",
"type": "platform"
},
"Serilog.Enrichers.Thread": "3.0.0",
"Serilog.Sinks.Literate": "2.1.0-dev-00034",
"Serilog.Sinks.RollingFile": "3.3.0",
"System.Runtime.Loader": "4.3.0",
"Thrower": "3.0.4"
},
"tools": {
},
"frameworks": {
"netcoreapp1.1": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
[2] project.json
for USB communication project
{
"version": "0.4.9-*",
"description": "Provides a type of communication service that facilitates communication between .NET applications and <<Product>> over USB.",
"authors": ["<<Author>>"],
"dependencies": {
"Microsoft.Composition": "1.0.30",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"Thrower": "3.0.4",
"CoreCompat.LibUsbDotNet": "2.2.8-r101",
"Common.Logging": "3.4.0-Beta2",
"<<Company>>.Message": "0.4.3",
"NETStandard.Library": "1.6.1",
"<<Company>>.Communications.Core": "0.4.5",
"Serilog": "2.3.0"
},
"packOptions": {
"owners": ["<<Company>>"],
"repository": {
"type": "git",
"url": "https://bitbucket.org/<<Company>>-dev/<<Product>>usb"
}
},
"frameworks": {
"netstandard1.4": {
"imports": [ "dnxcore50", "portable-net45+win8" ]
}
},
"buildOptions": {
"xmlDoc": true
}
}
[3] USB communication failure exception
Unhandled Exception: System.DllNotFoundException: libusb-1.0 library not found. This is often an indication that libusb-1.0 was installed to '/usr/local/lib' and
mono.net is not looking for it there. To resolve this, add the path '/usr/local/lib' to '/etc/ld.so.conf' and run 'ldconfig' as root. (http://www.mono-project.co
m/DllNotFoundException) ---> System.DllNotFoundException: Unable to load DLL 'libusb-1.0.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)
at MonoLibUsb.MonoUsbApi.Init(IntPtr& pContext)
at MonoLibUsb.MonoUsbSessionHandle..ctor()
--- End of inner exception stack trace ---
at MonoLibUsb.MonoUsbSessionHandle..ctor()
at MonoLibUsb.MonoUsbEventHandler.Init(UnixNativeTimeval unixNativeTimeval)
at MonoLibUsb.MonoUsbEventHandler.Init()
at MonoLibUsb.MonoUsbApi.InitAndStart()
at LibUsbDotNet.LudnMonoLibUsb.MonoUsbDevice.get_MonoUsbDeviceList()
at LibUsbDotNet.Main.LegacyUsbRegistry.get_DeviceList()
at LibUsbDotNet.UsbDevice.get_AllLibUsbDevices()
at LibUsbDotNet.UsbDevice.get_AllDevices()
at LibUsbDotNet.UsbDevice.OpenUsbDevice(Predicate`1 findDevicePredicate)
at LibUsbDotNet.UsbDevice.OpenUsbDevice(UsbDeviceFinder usbDeviceFinder)
at <<Company>>.Communications.<<Product>>Usb.UsbCommunicationService.Start()
at <<Company>>.<<Product>>Web.Core.DeviceController.Initialize()
at <<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.Startup.ConfigureServices(IServiceCollection services)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at <<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.Program.Main(String[] args)
[4] Web service Dockerfile
FROM microsoft/aspnetcore:1.1.0
ENTRYPOINT ["dotnet", "bin/Debug/netcoreapp1.1/publish/<<Company>>.<<Product>>Web.ServiceAdapters.WebSockets.dll"]
ARG source=.
WORKDIR /app
EXPOSE 80
COPY $source .
RUN apt-get update
RUN apt-get install -y libusb-1.0-0-dev usbutils
[5] Output of find / -name libusb*
on Docker container
root@19e8929e1814:/app# find / -name libusb*
/lib/x86_64-linux-gnu/libusb-1.0.so.0
/lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
/lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
/lib/x86_64-linux-gnu/libusb-0.1.so.4
/usr/lib/x86_64-linux-gnu/libusb-1.0.a
/usr/lib/x86_64-linux-gnu/pkgconfig/libusb-1.0.pc
/usr/lib/x86_64-linux-gnu/libusb-1.0.so
/usr/lib/x86_64-linux-gnu/libusb-0.1.so.4
/usr/include/libusb-1.0
/usr/include/libusb-1.0/libusb.h
/usr/share/doc/libusb-1.0-0
/usr/share/doc/libusb-1.0-doc
/usr/share/doc/libusb-1.0-doc/html/libusb_8h_source.html
/usr/share/doc/libusb-1.0-0-dev
/usr/share/doc/libusb-0.1-4
/usr/share/doc-base/libusb-1.0-doc
/var/lib/dpkg/info/libusb-1.0-0-dev:amd64.md5sums
/var/lib/dpkg/info/libusb-1.0-doc.list
/var/lib/dpkg/info/libusb-1.0-0:amd64.md5sums
/var/lib/dpkg/info/libusb-1.0-0:amd64.symbols
/var/lib/dpkg/info/libusb-1.0-0:amd64.shlibs
/var/lib/dpkg/info/libusb-1.0-0:amd64.list
/var/lib/dpkg/info/libusb-1.0-0:amd64.postrm
/var/lib/dpkg/info/libusb-1.0-0:amd64.postinst
/var/lib/dpkg/info/libusb-1.0-doc.md5sums
/var/lib/dpkg/info/libusb-1.0-0-dev:amd64.list
/var/lib/dpkg/info/libusb-0.1-4:amd64.postrm
/var/lib/dpkg/info/libusb-0.1-4:amd64.shlibs
/var/lib/dpkg/info/libusb-0.1-4:amd64.symbols
/var/lib/dpkg/info/libusb-0.1-4:amd64.postinst
/var/lib/dpkg/info/libusb-0.1-4:amd64.list
/var/lib/dpkg/info/libusb-0.1-4:amd64.md5sums
Link the actual shared library to the shared libraries location of the .Net Core version you have. I have managed to get the library loaded using:
ln -s /lib/x86_64-linux-gnu/libusb-1.0.so.0 /opt/dotnet/shared/Microsoft.NETCore.App/1.1.0/libusb-1.0.dll
I didn't get the list of devices yet though.
See this issue on Github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With