Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
Generating Webassembly from pure C# class

I'm making my backend on C# and frontend on JS.
I want to reuse some alghoritms written in C# in browser JavaScript.
Simplified example:

    class Fibonacci {
        int Fib(int x) {
            if (x == 0) return 0;
            int prev = 0;
            int next = 1;
            for (int i = 1; i < x; i++)
            {
                int sum = prev + next;
                prev = next;
                next = sum;
            }
           return next;
    }

Is it possible to compile one library-independent class to WebAssembly and use it from browser? How?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Yes, with NativeAOT-LLVM (https://github.com/dotnet/runtimelab/tree/feature/NativeAOT-LLVM). There's a similar question with a full answer at Compiling C# project to WebAssembly

about 4 years ago · Juan Pablo Isaza Denunciar

0

From what I understand, C# needs some middleware / framework to convert it to wasm. If your use case does not warrant Blazor, you might try the UNO platform: https://github.com/unoplatform/Uno.Wasm.Bootstrap

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can call .NET methods form JavaScript functions like this

Here is the razor html

@inject IJSRuntime JS

<button @onclick=@CallToJS >Call JS function</button>

Here is the code behind

 @code {
    /// <summary>
    /// This method calls javascript function with .net object reference parameter
    /// </summary>
    /// <returns>Awaitable task</returns>
    private async Task CallToJS() => await JS.InvokeVoidAsync("sayHi", DotNetObjectReference.Create(this));

    /// <summary>
    /// This method is called from javascript
    /// </summary>
    /// <param name="name">some string parameter coming from javascript</param>
    /// <returns>Returns string</returns>
    [JSInvokable] 
    public async Task<string> CalledFromJS(string name)
    {
        //  Here you can call your class library
        return $"Hi {name}";
    }
}

And this is the JavaScript code calling .NET method

function sayHi(dotNetHelper) {
    var promise = dotNetHelper.invokeMethodAsync('CalledFromJS', "Surinder");

    promise.then(function (result) {
        console.log(result);
    });    
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda