Blazor

Keeping Blazor JS interop alive after obfuscation

DEV Community: #blazor Zero Heartbeat
Blazor

Obfuscation renames the methods JavaScript calls in a Blazor app, and since [JSInvokable] defaults to the CLR method name, production calls fail with a JSException. The author moves that identifier into the attribute, where renaming and string encryption cannot reach it.

Key points

  • With a parameterless [JSInvokable], the dispatcher keys its lookup table by method.Name.
  • Passing an explicit identifier such as [JSInvokable("ComputeTotal")] keeps the JS key stable through renaming.
  • Attribute arguments live in the custom-attribute blob, so ldstr-based string encryption leaves them readable.
  • Blazor assemblies must keep custom attributes and must not have 'unused' public members demoted to internal.
  • [JSExport] has no identifier override; its namespaces, types and method names belong on the keep-list.
  • Renamed DTO properties change the JSON payload too, so they need [JsonPropertyName] the same way.

Why it matters

Anyone who turns on obfuscation for a shipped Blazor WebAssembly build is a candidate for this failure: everything works in development, one button throws in production, and the C# code looks fine. Because the fix changes where the contract lives rather than what the method does, teams writing new interop should set explicit identifiers up front instead of hunting them after a release.

Audit the build pipeline too. Passes that strip attributes or demote "unused" public members quietly starve Blazor's reflection scan. If you still have [JSExport] code on the marshalled interop, the rule inverts — there the managed names are the contract — so treat the two interop styles separately.

Real-world use

  • A Blazor WebAssembly app that is renamed and string-encrypted in its release build
  • Legacy static invokeMethodAsync calls that break when the assembly's simple name changes
  • Instance interop via DotNetObjectReference, where only the chosen identifier crosses the boundary

This summary was generated by the agent from the original post on DEV Community: #blazor. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message