Skip to content

Blazor DLL -> WASM file extension change causing functionality failure when updating to .Net 8 #103071

Open
@gregsdennis

Description

@gregsdennis

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Similar to dotnet/aspnetcore#52162, I need to load assemblies after the website as loaded. However, I'm loading them into a compilation context so that users can enters some C# code which I then compile. (See https://json-everything.net/json-schema (source), and select "Generate a Schema".)

The problem on that site (because I haven't updated it yet) is that it can't find the .dlls... because they've been renamed to .wasms. I'm okay with the rename.

My problem, however, is that these .wasm files aren't accepted by the .Net emitter.

This code works in .Net 6, using .dll files:

Loading the references

var stream = await Client.GetStreamAsync(source);
references.Add(MetadataReference.CreateFromStream(stream));

Compiling

var syntaxTree = CSharpSyntaxTree.ParseText(fullSource);
var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), "dll");

var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath))
    .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
    .AddReferences(references)
    .AddSyntaxTrees(syntaxTree);

using var dllStream = new MemoryStream();
using var pdbStream = new MemoryStream();
using var xmlStream = new MemoryStream();
var emitResult = compilation.Emit(dllStream, pdbStream, xmlStream);
if (!emitResult.Success)
{
    // .net 6 doesn't enter here
    // but .net 8 does with no changes except for the file extension
    return "Compilation error";
}

var assembly = Assembly.Load(dllStream.ToArray());

The diagnostics in the emitResult are primarily to do with the references:

error CS0009: Metadata file '<in-memory assembly>' could not be opened -- PE image doesn't contain managed metadata.

This happens whether the code should compile or not.

Expected Behavior

I expect the files to load into the compilation context and the emit to work, just as it does with .Net 6.

Steps To Reproduce

Minimal repro: https://github.com/gregsdennis/aspnet56080-repro

The solution file is in /BlazorApp1 for some reason. Not sure how that happened.

The .Net 6 app works fine, but the .Net 8 app doesn't. The only thing the .Net 8 app does differently is load .wasms instead of .dlls.

Exceptions (if any)

There are no exceptions, just the emit diagnostics.

.NET Version

8.0.206

Anything else?

Happy to know if there's something I'm missing, but it's obvious to me that the contents of a .wasm are different from the contents of a .dll. The assembly loader may be set up to understand and account for the difference, but the compiler/emitter isn't.

workaround

disable the .wasm extension for your project:

<PropertyGroup>
  <WasmEnableWebcil>false</WasmEnableWebcil>
</PropertyGroup>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions