Blazor WASM JS Calls with an Object Argument Work in VS Debug But Not a Published Release

Steve Rehling 125 Reputation points
2025-03-25T18:48:29.5166667+00:00

I have a Blazor WASM app that uses JSRuntime to Get/Put IndexedDB data (mockup below). Calls to "Put" functions that pass an object (not a native type) to be stored work fine in the VS Community 2022 debugger, but when I run a published version of the app, the JS code for these calls is not being successfully invoked (no console.log output as shown below).

I've read that "complex framework types...might be trimmed away by the IL Trimmer on publish..." but don't really understand that. This is the only thing I've come up with that sounds possibly related to my problem. I'd appreciate some guidance. Thanks.

public class MyClass : ICloneable
    {
    public int MyInt { get; set; }
    public bool MyBool { get; set; }
    public string MyString { get; set; }
    public DateTime MyDateTime { get; set; }
    
    public MyClass()
        {
        MyInt = 1;
        MyBool = false;
        MyString = string.Empty;
        MyDateTime = DateTime.Now;
        }
    
    public object Clone()
        {
        return new MyClass()
            {
            MyInt = MyInt,
            MyBool = MyBool,
            MyString = MyString,
            MyDateTime = MyDateTime,
            };
        } // end Clone
    } // MyClass 
        
MyClass myClass = new MyClass();
await JSRuntime.InvokeAsync<string>("My_Put", myClass);

// Javascript
function My_Put (myClass)
    {
        console.log("(TS) My_Put Start");
        var request = myDatabase.transaction(myStoreName, "readwrite")
            .objectStore(myStoreName)
            .put(myClass);
        request.onsuccess = function (event)
        {
            console.log("(TS) My_Put: SUCCESS");
            return "(TS) SUCCESS";
        }
        request.onerror = function (event)
        {
            console.log("My_Put: ERROR");
            return "(TS) ERROR";
        }
    } // My_Put

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,672 questions
{count} votes

Accepted answer
  1. Pradeep M 8,185 Reputation points Microsoft External Staff
    2025-03-31T04:14:56.4066667+00:00

    Hi Steve Rehling

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer. 

    Ask: Blazor WASM JS Calls with an Object Argument Work in VS Debug But Not a Published Release 

    Solution: The problem has been fixed as a result of installing (or re-installing) the wasm-tools workload by running dotnet workload install wasm-tools from the command line as recommended here and here (see the AOT section). The 1st "here" says that installing wasm-tools is optional, while the 2nd one says it's required for AOT. I need to use AOT for a production publish, but have not been using it as part of this testing sequence. After running the install, removing the DynamicDependency attributes, and setting PublishTrimmed to true in the project file, the published app runs as it does in the VS debugger (with and without AOT). 

    When I ran the install, I watched a bunch of packages being installed and a few being removed, the latter making me wonder if I already had it installed and this corrected some problem. Anyway, installing wasm-tools added 32 packages in my Nuget package manager, none of which can be uninstalled by the Nuget manager. It looks like I don't need many of them and will be looking at how to get rid of them (maybe they will just be trimmed!). But for now, this is the solution to my problem.  

    If you have any other questions, please let me know. 

    Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members. 

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Vahid Ghafarpour 23,200 Reputation points
    2025-03-25T19:15:35.1466667+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    Use the [Preserve] attribute or add a DynamicDependency attribute to ensure the type is not trimmed. For example:

    [Preserve]
    public class MyClass : ICloneable
    {
        // Your properties and methods
    }
    

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful **


  2. Steve Rehling 125 Reputation points
    2025-03-27T15:27:45.7866667+00:00

    The problem has been fixed as a result of installing (or re-installing) the wasm-tools workload by running dotnet workload install wasm-tools from the command line as recommended here and here (see the AOT section). The 1st "here" says that installing wasm-tools is optional, while the 2nd one says it's required for AOT. I need to use AOT for a production publish, but have not been using it as part of this testing sequence. After running the install, removing the DynamicDependency attributes, and setting PublishTrimmed to true in the project file, the published app runs as it does in the VS debugger (with and without AOT).

    When I ran the install, I watched a bunch of packages being installed and a few being removed, the latter making me wonder if I already had it installed and this corrected some problem. Anyway, installing wasm-tools added 32 packages in my Nuget package manager, none of which can be uninstalled by the Nuget manager. It looks like I don't need many of them and will be looking at how to get rid of them (maybe they will just be trimmed!). But for now, this is the solution to my problem.

    Thanks to Ping, Vahid and Pradeep for guiding my search for a solution. I wouldn't have found it without your help.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.