I ended up getting around the problem by upgrading from .Net8 to .Net9.
ClosedXML new XLWorkbook(memorystream) Works in VS Debugger But Not the Published App
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;
}