A free and open-source web framework that enables developers to create web apps using C# and HTML, developed by Microsoft.
In .NET 10, the interop model for Blazor WebAssembly has indeed changed, which affects how you can call C# methods from JavaScript in non-Blazor applications. Here are some points to consider based on the current context:
- JS Interop Changes: The traditional
[JSInvokable]approach you used in .NET 9 is still valid but may require adjustments due to changes in the runtime initialization. Ensure that your project is correctly set up to use the new interop model. - Using
[JSExport]: This is the recommended approach for calling C# from JavaScript in .NET 10. However, if you are encountering issues likecreateDotNetRuntime is not defined, it indicates that the required runtime setup is not being executed properly. You may need to ensure that the JavaScript files generated during the publish process are correctly included and initialized in your Angular application. - Loading the .NET WASM Runtime: While the new model may not expose a simple bootstrap API for non-Blazor apps, you can still load the runtime manually. You will need to include the generated JavaScript and WASM files in your Angular project and ensure that the runtime is initialized correctly before making any calls to C# methods.
- Calling C# Methods: For calling C# methods from JavaScript, you will use the
[JSExport]methods, but you must ensure that the runtime is initialized correctly. TheDotNet.invokeMethodAsyncmethod is part of the Blazor hosting model, and if you are not using Blazor, you will need to adapt your approach to use the new interop methods available with[JSExport]. - Workarounds: If you find that the new model does not meet your needs, consider looking into community solutions or examples that demonstrate how to integrate .NET WebAssembly into non-Blazor applications. The official documentation may also provide insights into best practices for this scenario.
In summary, while there are significant changes in .NET 10, it is still possible to call C# methods from JavaScript, but it requires adapting to the new interop model and ensuring proper runtime initialization.
References: