F# consumes the same KeyNub.LicenseDongle assembly as C# and VB.NET — there is no separate binding. What changes is that three of F#’s ordinary idioms each remove a piece of ceremony the other two languages have to write out.
dotnet add package KeyNub.LicenseDongleCode language: Bash (bash)
Reading a license
open KeyNub.LicenseDongle
use ctx = LicenseDongleContext.Create()
let devices = ctx.Enumerate()
if devices.Count = 0 then
printfn "Please plug in your KeyNub license dongle."
0
else
use dongle = ctx.Open() // first dongle found
if not dongle.IsGenuine then 1
else
use session = dongle.OpenSession()
let licence = session.ReadRecord("license")
0Code language: F# (fsharp)
Three things F# does better here
use instead of nested using. Each resource is disposed when its scope ends, without the indentation stack that the C# and VB.NET versions build up. With a context, a dongle and a session in play that is three levels saved.
Pattern matching instead of a catch ladder. Matching on the exception type reads as a decision rather than a sequence of filters:
with
| :? DeviceNotFoundException ->
printfn "The dongle was disconnected while we were talking to it."
0
| :? LicenseDongleException as ex ->
eprintfn "KeyNub error (%O): %s" ex.Status ex.Message
1Code language: F# (fsharp)
Structural equality on arrays. Verifying an app-crypto round trip is a single =, where C# and VB.NET both need an explicit element loop or a call to a comparison helper:
let recovered = session.AppDecrypt(sealedBlob)
if recovered = needed then "intact" else "MISMATCH"Code language: F# (fsharp)
One thing to watch
F#’s printf is type-checked, so the format specifier has to match. ProtocolVersion and FirmwareVersion are System.Version objects rather than strings, which means %O and not %s. That is a compile error rather than odd output at run time — which is the point.
Do not gate on a boolean
Where a licence check belongs is the same argument as everywhere else, and it is the one thing worth getting right: encrypt the data your program genuinely cannot compute, ship it encrypted, and decrypt it through the dongle at run time. Then removing the check does not unlock the feature — it removes the feature’s input.
Full F# sample on GitHub · the same thing in C# · in VB.NET
All supported languages · All industries · Buy a KeyNub · Ask us something