Blazor WebbAssembly exception when calling javascript function?

Viewed 166

I have a C# function that is invoking a Javascript function, this is the C# function:

await _jsRuntime.InvokeVoidAsync("setMediaUsingStreaming", type, dotnetImageStream);

And this is the Javascript function:

   async function setMediaUsingStreaming(fileType, fileStream) {
            try {

                const arrayBuffer = await fileStream.arrayBuffer();
                const blob = new Blob([arrayBuffer]);
                const url = URL.createObjectURL(blob);

                var newHTMLElement;
                var fileHTMLElement = document.getElementById('fileDisplay');

                if (fileType == "image") {
                    newHTMLElement = document.createElement('img');
                }
                else {
                    newHTMLElement = document.createElement('video');
                    var attribute = document.createAttribute('controls');
                    newHTMLElement.setAttributeNode(attribute);
                }

                fileHTMLElement.appendChild(newHTMLElement);
                newHTMLElement.src = url;

            } catch (Exception)
            {
                console.log(Exception)
            }
   } 

When the C# function is invoking the Javascript function I receive this error :

blazor.webassembly.js:1 Uncaught (in promise) Error: Microsoft.JSInterop.JSException: Cannot set properties of null (setting 'src') TypeError: Cannot set properties of null (setting 'src')

The weird thing is that I receive this error when I run the app in publish mode (withouth a debugger) but it works perfectly in debug mode. The catch from the javascript function is not hit when I receive this error, it seems that is the C# method that is throwing it.

I know what the error is saying, that the url variable is null, but I don't understand why is null, and why it works on debug mode.

Do you have any ideas ?

0 Answers
Related