Till this launch, it was attainable to make use of WebAssembly with out the necessity for Blazor by leveraging the Mono WASM SDK, Uno.Wasm.Bootstrap library or the NativeAOT LLVM. Though the brand new workload and options are the idea for Blazor WebAssembly, with .NET 7, utilizing .NET code inside a WebAssembly context is feasible with out use of specialised libraries or Blazor and leveraging solely utilities from inside .NET.
import dotnet from './dotnet.js' const getAssemblyExports, getConfig, runMainAndExit = await dotnet.create(); const configuration = getConfig(); const exports = await getAssemblyExports(configuration.mainAssemblyName); const myValue = exports.MyClass.MyMethod(); // Name into your technique from ```js await dotnet.run(); // Run the Program Fundamental technique
import dotnet from './dotnet.js' const setModuleImports, getConfig, runMainAndExit = await dotnet.create(); const configuration = getConfig(); // Set module imports that may be known as from .NET setModuleImports("major.js", window: location: href: () => globalThis.window.location.href ); await dotnet.run(); // Run the Program Fundamental technique
await JSHost.ImportAsync("major.js", "./major.js");
And on the .NET facet, that is how one can import the strategy, whatever the publicity variant you employ:
[JSImport("window.location.href", "main.js")] inner static partial string GetHRef();
Pavel Šavara’s GitHub repository presents extra code samples, together with a more detailed explanation of some elements of the code within the accompanying article. A video overview of the identical pattern is on the market within the ASP.NET Community Standup recording.
Debugging the interop code shouldn’t be attainable from Visible Studio but, however the utility will be debugged by working the appliance from the command-line and attaching the Visible Studio Code or Chrome debuggers.
The .NET 7 RC 1 SDK is on the market for obtain and after set up, you’ll be able to set up the brand new WebAssembly workload and create a brand new WebAssembly browser utility by working the next instructions:
dotnet workload set up wasm-tools dotnet workload set up wasm-experimental dotnet new wasmbrowser
This launch makes extra enhancements for Blazor functions together with Debugging Improvements, availability of WebAssembly build tools for .NET 6, and Dynamic authentication requests in Blazor WebAssembly.