locked
How to access usb device using ASP.NET core RRS feed

  • Question

  • User-1134842450 posted

    I created a desktop app that control usb device now i want to convert the app to a webapp but the problem is the ASP.net core doesn't detect the usb device i tried many solutions but the same result the usb device and website are all in my pc i tried other technologies like node js and python django it worked fine on those but i want to do it in asp.net core any other solutions thanks in advance

    Tuesday, April 7, 2020 6:24 AM

All replies

  • User711641945 posted

    Hi algebgeo,

    To detect the USB service,you could try to use `System.Management` package.

    Reference:

    https://stackoverflow.com/a/51806262/11398810

    Best Regards,

    Rena

    Tuesday, April 7, 2020 7:32 AM
  • User-1134842450 posted

    Thank you for your answer but i have the functions that can detect usb device it works well in the desktop app but when i try to run it in asp.net web app it doesn't work i tried using node js and it worked well but i want to use asp.net core

    Tuesday, April 7, 2020 7:40 AM
  • User711641945 posted

    Hi algebgeo,

    it works well in the desktop app but when i try to run it in asp.net web app it doesn't work i tried using node js and it worked well but i want to use asp.net core

    It seems you want to make your previous code work in a new asp.net core project?If so,you need to share a sample.

    Best Regards,

    Rena

    Tuesday, April 7, 2020 7:58 AM
  • User-1134842450 posted

    its not a sample code its a DLL that have functions that access usb device i just want to map it to asp.net web app and make it work when i run those functions on asp.net they don't work i managed to work the DLL in node js but i want to make it work on asp.net core web app

    Tuesday, April 7, 2020 8:07 AM
  • User753101303 posted

    Hi,

    Always tell what actually happens rather than just that "it doesn't work". My guess is that this is server side code and so it tries to use a USB device that would be plugged on the server side (even if it may appear to work on your development machine).

    Instead I would try https://developer.mozilla.org/en-US/docs/Web/API/USB

    If you need further help the final goal could help (this is a usb drive, it is used for which purpose ???)

    Tuesday, April 7, 2020 8:16 AM
  • User-1134842450 posted

    What happens is the DLL have a function that returns the number of usb device connect to the PC it works fine in desktop app but when i run this function in asp.net app it returns 0 devices detected and its not a serverside code actually all things exist in my PC the web app and usb device so why asp.net web app cannot detect the usb device i gave all the permissions to the web app to access my devices.

    Tuesday, April 7, 2020 8:24 AM
  • User753101303 posted

    And using a separate web server won't be your final architecture? If yes it won't work anyway once deployed.

    Which kind of device is this? I can only assume the DLL does something wrong.

    My first move would be to see if using WMI as shown at https://social.msdn.microsoft.com/Forums/en-US/8d81b8a3-c101-4b47-8274-dd743fa41ec4/get-devices-by-connection-as-in-device-manager?forum=csharpgeneral does detect something.

    It runs from Visual Studio or is it deployed on your local IIS server?

    Tuesday, April 7, 2020 8:58 AM
  • User-1134842450 posted

    Its a usb camera and DLL is fine i tested it in node js app and i run it from visual studio

    Tuesday, April 7, 2020 9:04 AM
  • User753101303 posted

    It really does nothing else than enumerating devices? If a 3rd party API what if you try their support site. I can only guess that there is an underlying error which is ignored rather than really not finding any device? The final goal is still unclear.

    If you want to show in a browser images taken by a remote camera attached to a web server, AFAIK https://en.wikipedia.org/wiki/IP_camera are mots often used.

    If you want to capture a video from a browser, you have browser side APIs to do this kind of capture.

    Tuesday, April 7, 2020 11:57 AM
  • User475983607 posted

    DLLs run on a web server not the client machine.   The camera must be attached to the server's USB port.   

    Your USB camera might come with software installed on the client the allows streaming video. You'll need to read the documentation for the USB camera you are using or contact support for the camera to make sure the camera will work as expected.  You'll certainly need to configure your network to allow a web server or other internet connected devices to see the camera images by connection to your machine.  Your machine becomes a web server.

    Tuesday, April 7, 2020 12:12 PM
  • User-474980206 posted

    .net 5 will have better support of desktop features. Asp.net core can call c/c++ dlls, but not 4.8 libraries. Look at the c/c++ APIs.

    i’d probably stick with node.

    Tuesday, April 7, 2020 2:53 PM
  • User-1134842450 posted

    The DLL have many APIs that control the camera and every thing run on my machine i cannot stick with node i have to make it with ASP.net core

    Tuesday, April 7, 2020 11:26 PM
  • User-474980206 posted

    If the dll has a c/c++ interface, which it probably does for node to call it, you should be able to call from c#

    Wednesday, April 8, 2020 12:47 AM
  • User-1134842450 posted

    yeah i am able to call it from c# desktop app and the functions work properly but from asp.net core i can call the functions and the functions work but they cannot detect the usb camera thats the problem.

    Wednesday, April 8, 2020 1:22 AM
  • User753101303 posted

    You are 100% sure it doesn't return some error code you could check? What if you show the minimal amount of code showing that the camera is not detected? And so I would try on my own to see if it seems an ASP.NET Core side issue or rather a problem in this 3rd party DLL.

    I'm still not sure what you'll do with a camera directly attached to a web server rather than to a browser side computer. You want to monitor the web server surrounding ?

    If the web site will always run on the same computer to which a camera is attached it could be better to just keep your current desktop app rather than having to install a web server on each and every machine to do the same thing.

    Wednesday, April 8, 2020 12:32 PM
  • User-1134842450 posted

    I am sure that there is no errors it only doesn't detect usb camera the functions work properly but the problem is the detection of the usb camera there is no problem with the DLL and i don't have the source code of the DLL to show it and the code is simple i just use p/invoke to call the DLL and use its functions it works well in desktop apps but not in asp.net core or any asp.net

    Thursday, April 9, 2020 4:52 AM
  • User753101303 posted

    Once again I would like to see the source code and possibly the documentation to make 100% sure you are testing a possible error code. I tried something like :

                // needs install-package System.Management
                ManagementScope sc = new ManagementScope();
                ObjectQuery query = new ObjectQuery("select * from Win32_USBControllerDevice");
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query))
                {
                    using (ManagementObjectCollection result = searcher.Get())
                    {
                        System.Diagnostics.Debug.WriteLine("Devices: {0}",result.Count); // Shown on the output window
                    }
                }

    from an ASP.NET 3.1 Core (runs from VS on my machine) and it seems to find 41 devices (didn't expected that much). For now there is nothing pointing to ASP.NET Core not being able to find USB devices.

    Also I would like to make sure it makes sense. As you are moving from a desktop app where the camera is attached to each and every client computer to a web app where the camera is attached to a remote web server I'm still in doubt that you are not trying that just to realize later that it can't work. Edit: Or your intent is really to replace a desktop app with a web server on each and every machine??

    A common catch when starting a web app is to NOT realize that you are in the very special case where your own machine is both the browser and the server side which can give the impression the web server can access local resources.

    Thursday, April 9, 2020 8:26 AM
  • User-474980206 posted

    does the asp.net core fail if you run it from the command line (so it runs as your login)

       dotnet run

    Thursday, April 9, 2020 3:35 PM
  • User-1134842450 posted

    It runs and i get the same problem usb devices detected is zero

    Thursday, April 9, 2020 11:07 PM
  • User753101303 posted

    I would like to make 100% sure the code you are testing runs. What if you try to add:

    if (result.Count==0) throw new Exception("no device");

    Then do a last test. Do you see a "no device" exception?. If yes I hardly see what could block enumerating devices on your side.

    And so on my side I tried a PowerShell command line prompt and I do find the same count:

    PS C:\Users\****> gwmi Win32_USBControllerDevice | Measure-Object
    
    
    Count    : 41
    Average  :
    Sum      :
    Maximum  :
    Minimum  :
    Property :

    Friday, April 10, 2020 2:15 PM
  • User-474980206 posted

    algebgeo

    It runs and i get the same problem usb devices detected is zero

    the. I’d start simple. As you know it works in a desktop app (.net I assume, you don’t give much info), create as simple classic .net command line project that can call the dll for usb device list. Once this working, check the dlls in the bin folder. Be sure no config files are used.

    now create a new .net core console app, and copy the code from above. Build it and check the bin folder has the required dlls. 

    note: you don’t tell much about the dll, but it’s probably c/c++ if callable from node. It will not throw an error only return error status from calls. Look at the node docs for debugging hints. If it’s callable by node you can call from .net core

    Friday, April 10, 2020 5:50 PM
  • User-1134842450 posted

    You got me wrong the code you sent me it detected 12 devices i was speaking of the DLL.

    Saturday, April 11, 2020 1:43 AM
  • User-1134842450 posted

    I tried it with Console .net core and .net framework and it didn't detect the usb camera it only detects it in WinForm and WPF apps both .net framework and .net core.

    Saturday, April 11, 2020 3:02 AM
  • User-663662722 posted

    Try Usb.Net. It supports .NET Core and it works good.

    Tuesday, February 16, 2021 9:46 AM