ClosedXML new XLWorkbook(memorystream) Works in VS Debugger But Not the Published App

Steve Rehling 145 Reputation points
2025-04-24T15:29:08.45+00:00

I have a large WASM app that works with multi-worksheet Excel files the user selects via the InputFile component. After the user selects one or more files, I use the code below to put the contents in a ClosedXML XLWorkbook. This works as expected when run using the VS Community 2022 debugger, but the self-contained Published app triggers the error shown below on the "new XLWorkbook" statement. I am targeting .Net8 and use AOT, WasmStripILAfterAOT, and PublishTrimmed, which I’ve toggled off and on while still getting the error.

Github CoPilot says that ClosedXML relies on DocumentFormat.OpenXml, which uses APIs that are not supported in the WebAssembly runtime. But it would seem to me that the new XLWorkbook statement that's triggering the error isn't using any such APIs or it wouldn't work under the VS debugger. Am I wrong to think that? In any case, is there a way around this that doesn't involve adding a server component or moving to a different package (i.e. Syncfusion) as CoPilot suggests? Also, I'm curious to know why the catch block didn't capture the error (I confirmed this with a console message in the catch block).

public static async Task<string> GetExcelWorkbook(IBrowserFile xlsxFile)
    {
    try
        {
        MemoryStream ms = new MemoryStream();
        await xlsxFile.OpenReadStream().CopyToAsync(ms);
        MyWorkbook = new XLWorkbook(ms);
        }
    catch (Exception ex)
        {
        return ex.Message;
        }
    return string.Empty;
    }

WASM XLWorkbook Error

Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,084 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Steve Rehling 145 Reputation points
    2025-04-26T16:14:40.2666667+00:00

    I ended up getting around the problem by upgrading from .Net8 to .Net9.

    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.