dotnet/AspNetCore.Docs · error · Exception
QR code size must be less than {MaxQrSize}.
Error message
QR code size must be less than {MaxQrSize}. What it means
The Blazor web-worker QR sample guards `Generate(text, qrSize)` with `if (qrSize >= MaxQrSize) throw` where MaxQrSize is 20. qrSize is the pixels-per-module passed to QRCoder's `GetGraphic`; large values blow up worker memory and render time, so the sample caps it.
Source
Thrown at aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md:285
`Workers/QRGenerator.razor.cs`:
```csharp
using System.Runtime.InteropServices.JavaScript;
using System.Runtime.Versioning;
using QRCoder;
[SupportedOSPlatform("browser")]
public partial class QRGenerator
{
private static readonly int MaxQrSize = 20;
[JSExport]
internal static byte[] Generate(string text, int qrSize)
{
if (qrSize >= MaxQrSize)
{
throw new Exception($"QR code size must be less than {MaxQrSize}.");
}
var generator = new QRCodeGenerator();
QRCodeData data = generator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);
var qrCode = new BitmapByteQRCode(data);
return qrCode.GetGraphic(qrSize);
}
}
```
Create a matching Razor component file (`.razor`) to act as an empty stub so that the build packs the worker script alongside the component assets:
`Workers/QRGenerator.razor`:
```razor
// dummy file to let blazor handle Worker.razor.js file loading
```View on GitHub (pinned to c67a80103a)
Solutions
- Pass a size strictly less than 20 (e.g. 10–15 for typical QR codes).
- Clamp the caller-supplied value before invoking the worker: `size = Math.min(size, 19)`.
- Validate input in the message handler and surface a friendly error to the UI rather than letting the throw propagate.
Example fix
// before const bytes = assemblyExports.QRGenerator.Generate(text, 32); // after const size = Math.min(requestedSize, 19); const bytes = assemblyExports.QRGenerator.Generate(text, size);
Defensive patterns
Strategy: validation
Validate before calling
function safeQrSize(requested: number, maxExclusive = 20): number {
if (!Number.isFinite(requested) || requested <= 0) throw new RangeError('qrSize must be a positive number');
return Math.min(Math.floor(requested), maxExclusive - 1);
} Type guard
function isValidQrSize(size: number, maxExclusive = 20): boolean {
return Number.isInteger(size) && size > 0 && size < maxExclusive;
} Try / catch
// Worker side already throws; guard on the caller:
try {
await postToWorker({ command: 'generateQR', text, size: safeQrSize(size) });
} catch (err) {
if (/QR code size/.test(err.message)) showUserError('Pick a smaller QR size.');
} Prevention
- Clamp user-supplied size before sending to the worker.
- Bound the UI input (e.g. `<input type="number" min="1" max="19">`).
- Share the MaxQrSize constant between C# and TS.
When it happens
Trigger: Calling the exported `QRGenerator.Generate(text, size)` with `size >= 20`. The check is `>=`, so the maximum allowed value is 19.
Common situations: Caller passes a default or guessed size (e.g. 25 or 32); UI control lets the user pick any number; size derived from device pixel ratio without clamping.
Related errors
- Unknown command: ${e.data.command}
- worker exports not loaded
- Validation failed. Status Code: {response.StatusCode}
- Must provide a non-empty value for the "src" attribute.
- When calling ko.fromJS, pass the object you want to convert.
AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13).
Data as JSON: /api/errors/682b223737dcb283.
Report an issue: GitHub.