C# in the Browser Without Blazor
Monday, 28 November 2022
I’ve talked about an experiment with Blazor in the past, but wanted to see how it would go building something small in C# for the browser without going through that process. With .NET 7 it doesn’t seem too hard, so I went ahead and created an example repo to demonstrate.
I am relying on “JSExport” to expose C# to the JavaScript application and have decided to make things simple by using strings for the arguments and return values as well as forgetting about how inefficient this is (for what this is actually doing).
using System.Globalization; using System.Runtime.InteropServices.JavaScript; public partial class Calculator { // ... [JSExport] internal static string add(string first, string second) { Decimal firstNumber = Decimal.Parse(first, NumberStyles.Float); Decimal secondNumber = Decimal.Parse(second, NumberStyles.Float); return (firstNumber + secondNumber).ToString(); } // ... }
Using something like DotNetJS seems more appropriate for use cases like this in the short-term, and I wonder where WASI and Wasmtime .NET solutions will wind up going forward?