{"record":{"id":"df757a1fb9135395","repo":"dotnet/AspNetCore.Docs","slug":"unknown-command-e-data-command","errorCode":null,"errorMessage":"Unknown command: ${e.data.command}","messagePattern":"Unknown command: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md","lineNumber":335,"sourceCode":"  const config = getConfig();\n  assemblyExports = await getAssemblyExports(config.mainAssemblyName);\n} catch (err) {\n  startupError = err.message;\n}\n\nself.addEventListener('message', async e => {\n  try {\n    if (!assemblyExports) {\n      throw new Error(startupError || 'worker exports not loaded');\n    }\n\n    let result;\n    switch (e.data.command) {\n      case 'generateQR':\n        result = assemblyExports.QRGenerator.Generate(e.data.text, e.data.size);\n        break;\n      default:\n        throw new Error(`Unknown command: ${e.data.command}`);\n    }\n\n    self.postMessage({ command: 'response', \n      requestId: e.data.requestId, result });\n  } catch (err) {\n    self.postMessage({ command: 'response', \n      requestId: e.data.requestId, error: err.message });\n  }\n});\n```\n\n## Bridge the worker to the Blazor UI\n\nCreate the following JavaScript file that manages the worker instance and exposes helper functions to Blazor.\n\n`Clients/Client.razor.js`:\n\n```javascript","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md#L317-L353","documentation":"The web-worker message handler uses a `switch (e.data.command)` and throws `Unknown command: ${e.data.command}` in the default branch. It guards against unrecognized message types so malformed posts surface immediately rather than silently no-op'ing.","triggerScenarios":"Posting any message whose `command` field is not `'generateQR'` (the only defined case). Example: a typo like `'generateQr'`, a polling message, or a framework handshake the worker doesn't expect.","commonSituations":"Case mismatch in command strings; evolving the protocol without updating both sides; third-party library posting to the worker (e.g. a devtools ping); stale main-thread code after adding a new command.","solutions":["Match the command string exactly — the worker expects `'generateQR'`. Compare against the literal in the switch.","Centralize command names as shared constants between main thread and worker.","Filter out unrelated messages (e.g. by checking `e.data && e.data.requestId`) before the switch."],"exampleFix":"// before\nworker.postMessage({ command: 'generateQr', text, size, requestId });\n\n// after\nworker.postMessage({ command: 'generateQR', text, size, requestId });","handlingStrategy":"validation","validationCode":"const COMMANDS = new Set(['generateQR']);\nfunction isKnownCommand(cmd) {\n  return COMMANDS.has(cmd);\n}\n// Worker:\nif (!isKnownCommand(e.data.command)) {\n  self.postMessage({ command: 'response', requestId: e.data.requestId, error: `Unknown command: ${e.data.command}` });\n  return;\n}","typeGuard":"function isWorkerCommand(data) {\n  return data && typeof data.command === 'string' && data.command === 'generateQR';\n}","tryCatchPattern":"// The worker already wraps in try/catch and posts the error back;\n// main thread handles it:\nif (resp.error && /Unknown command/.test(resp.error)) {\n  console.error('Protocol mismatch — check command spelling:', resp.error);\n}","preventionTips":["Share command-name constants between worker and main thread.","Filter unrelated messages before the switch.","Add new commands on both sides atomically."],"tags":["blazor","web-worker","protocol","validation"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}