How to use RFID / NFC using Blazor

Viewed 2795

As the title suggest, I want to implement an RFID reader into my blazor app.

Traditionally DotNetCore apps run serverside. But with blazor clientside would it be possible?

Alternatively does blazor support Active X?

2 Answers

Update:

Assuming you're referring to using desktop PC .NET assemblies that work with common RFID and NFC hardware over local communication ports (COM, USB, Bluetooth, etc), then no, it is not possible.

However, in a few years, if browser vendors add support for proposed JavaScript APIs for NFC and RFID hardware ("Web NFC") then you could use the those APIs from your code.

Some RFID hardware vendors (both system-integrators and actual hardware OEMs) provide infrastructure-based readers/scanners (the kind that are permanently mounted on a wall or ceiling) that provide HTTP-based web-services (SOAP, REST, something else, etc) that your Blazor code could also use.

Here's a list:

  • Internet/LAN website or PWA using in-browser JavaScript APIs:

    • Direct-attached (including PAN/Bluetooth) RFID+NFC reader hardware:
    • Infrastructure scanners/readers with HTTP-based web-services: Yes, provided you can access them using fetch/XMLHTTPRequest, EventSource or WebSocket.
  • Internet/LAN website or PWA using C# code and .NET libraries from within Blazor (so not calling in-browser JavaScript APIs from within Blazor)

    • Direct-attached (including PAN/Bluetooth) RFID+NFC reader hardware: No (because OS services/syscalls aren't available in Blazor).
    • Infrastructure scanners/readers with HTTP-based web-services: Yes, provided you can access them using HttpClient.
  • Device App (Mobile or Desktop) using Cordova/PhoneGap or Electron:

    • Direct-attached (including PAN/Bluetooth) RFID+NFC reader hardware: Potentially, based on what extra APIs are provided by the Cordova, PhoneGap or Electron environment.
    • Infrastructure scanners/readers with HTTP-based web-services: Yes, provided you can access them using fetch/XMLHTTPRequest, EventSource or WebSocket.
  • Desktop computer with a service process installed on the user's computer that interacts with the web-application over a localhost-only HTTP listener:

    • Yes. See my original answer below.
    • Note that this solution means that the service process could communicate with your web-application directly without the need for a web-browser application.

Original answer:

In short: No. It is not possible.

While Blazor code does indeed run on the client, it isn't running as native CPU instructions (it's actually running as interpreted or JIT-compiled Web-Assembly code in the browser's JavaScript engine) - which means it's also very tightly sandboxed.

In short: In Blazor, if there isn't a browser-provided API for interacting with the computer beyond the browser then you simply can't do it.

For an explanation: .NET code is able to run in Blazor because Blazor includes a tiny version of the .NET runtime engine (e.g. for managed-object garbage collection) as well as shrunken versions of the main .NET base-class-library assemblies which are downloaded on-demand - so browsers don't actually download your entire 20MB+ sized Blazor app all at once at first.

Blazor essentially ships its own version of mscorlib.dll, System.dll`` and other assemblies which stub-out all operations involving platform services that are unavailable in the browser and instead they throw new PlatformNotSupportedException - so if you try to use, for example, the filesystem API in .NET (System.IO.File, etc) then you'll get an exception/error.

Within any Turing Complete programming environment on a PC (like Blazor and WebAssembly) you can do any information processing task you want but any program activity that makes something happen in real-life (e.g. IO, making a syscall, controlling a hardware device) has to go through the operating-system via a syscall, .NET syscalls happen internally in the CLR (often seen with MethodImplOptions.InternalCall methods and P/Invoke ([DllImport]) methods) - which aren't made at all by the Blazor CLR - though some APIs are remapped to native JavaScript, e.g. Console.WriteLine is mapped to console.log.

As for ActiveX - no browser has supported unrestricted, non-sandboxed ActiveX controls for over 15 years. ActiveX is dead. Browsers have also dropped support for Java applets as well.

Possible workaround:

That said, provided you can install your own programs on the client computer then you can have your web-application (even if it's hosted on a public Internet website) communicate with a client program over a local HTTP server (which includes WebSockets) accessd from a fetch or XMLHttpRequest request made by a local browser (no external connections, fortunately). This is how the Dell Computers website's "Find my computer's service-tag" feature works: Dell computers come pre-installed with a small web-server that listens for loopback requests and replies with the service-tag (I think requests do need to be signed so third-party websites can't do the same thing to get the Dell service-tags of their visitors).

  1. Write a separate program (e.g. a Windows Service daemon) that:

    1. runs a micro HTTP server that listens on only 127.0.0.1:12345 and performs RFID/NFC actions in response to incoming requests.
    2. Add support for websocket requests for bi-directional communication too.
    3. Ensure that all requests are authorized, such as cryptographically signing requests using a server-side private key and your program must verify the validity of incoming requests to prevent unauthorized requests.
    4. You should also use a HTTP binding exclusively - though I'm unsure of the best way to generate and trust a certificate for localhost.
  2. Your client-side code would then use fetch( 'https://localhost:1234/do-stuff-with-rfid' ). If you're using signed requests and requests are initiated by the client-code (not the web-server code) then you'll need to be more creative with how requests are authenticated.

the answere is partial wrong. NFC with Blazor is possible. Yes the browser is a sandbox and Blazor lifes in it. So a sandbox in a sandbox. But with the JavaScript APi you can access each Browser API. Surprise there is a draft for it https://w3c.github.io/web-nfc/

Would I do this in browser? never ever. NFC in general is a complicated topic, cause of diffrent standards, format, devicess. I coded it using a library and UWP, within a few hours.

Related