Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling .NET WebAssembly code from views #1526

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

tomasherceg
Copy link
Member

@tomasherceg tomasherceg commented Dec 4, 2022

This PR enables the user to reference a C# project compiled as a web assembly and invoke methods on imported "modules".

There are several things that need to be done:

  • Decide if we'll allow importing multiple modules in the same view; currently, only one is supported
  • More tests for SPA, combination with JS modules
  • Production builds and trimming - in Debug mode, we are using reflection, and trimming is disabled
  • Publishing experience - MSBuild task that will take compiled app bundle and ship it as part of the web app
  • Better indication of errors and error handling - invoking the error screen from the client
  • Serialization of enums with EnumMember attribute

Copy link
Member

@exyi exyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the changes in the framework fine (except for the dotvvm-with-view-modules omission).

It's harder to tell how well will the WASM related code work, It's a bit sad we use reflection to invoke the methods, but I don't see any way around it 🤷

throw new DotvvmCompilationException($"The type {type} referenced in the @dotnet directive must have exactly one public constructor with one parameter of IViewModuleContext!");
}
// TODO: check parameter type
return Expression.New(constructors[0], Expression.Constant(null, typeof(object)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Expression.New(constructors[0], Expression.Constant(null, typeof(object)))
return Expression.New(constructors[0], Expression.Constant(null, typeof(IViewModuleContext)))

It won't compile with object.

However, I'm thinking if we want to allow invoking the methods server-side. I guess it makes sense, but passing null into the constructor will just make it fail on the unexpected null reference. Maybe only allow it if the type also has parameterless constructor?

this.imports = imports;
}

public override string DirectiveName => ParserConstants.CsharpViewModuleDirective;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to rename CsharpViewModuleDirective to DotnetViewModuleDirective

@@ -107,6 +107,7 @@ const dotvvmExports = {
call: viewModuleManager.callViewModuleCommand,
registerMany: viewModuleManager.registerViewModules
},
get virtualDirectory() { return getVirtualDirectory() },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find where this is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants