DotNetJS 0.2.5

Suggested Alternatives

Bootsharp

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotNetJS --version 0.2.5                
NuGet\Install-Package DotNetJS -Version 0.2.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetJS" Version="0.2.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetJS --version 0.2.5                
#r "nuget: DotNetJS, 0.2.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DotNetJS as a Cake Addin
#addin nuget:?package=DotNetJS&version=0.2.5

// Install DotNetJS as a Cake Tool
#tool nuget:?package=DotNetJS&version=0.2.5                

NuGet npm CodeFactor codecov CodeQL

This project is dedicated to providing a user-friendly workflow for consuming .NET C# programs and libraries in any JavaScript environments: be it browsers, node.js or custom restricted spaces, like web extensions for VS Code.

Quick Start

In C# project (.csproj) specify Microsoft.NET.Sdk.BlazorWebAssembly SDK and import DotNetJS NuGet package:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <LangVersion>10</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="DotNetJS" Version="0.1.0"/>
    </ItemGroup>

</Project>

To invoke a JavaScript function in C# use JS.Invoke(functionName, args) method. To expose a C# method to JavaScript, use [JSInvokable] attribute:

using System;
using DotNetJS;
using Microsoft.JSInterop;

namespace HelloWorld;

public static class Program
{
    public static void Main ()
    {
        var hostName = JS.Invoke<string>("getName");
        Console.WriteLine($"Hello {hostName}, DotNet here!");
    }

    [JSInvokable]
    public static string GetName () => "DotNet";
}

Publish the project with dotnet publish. A single-file UMD library containing the dotnet runtime and project assemblies will be produced in the "bin" directory. Namespace of the program will be used for both the library file name and main export object. Consume the library depending on the environment:

Browser

<script src="HelloWorld.js"></script>

<script>
    
    // This function is invoked by DotNet.
    window.getName = () => "Browser";
    
    window.onload = async function () {
        // Booting the DotNet runtime and invoking entry point.
        await HelloWorld.boot();
        // Invoking 'GetName()' method from DotNet.
        const guestName = HelloWorld.invoke("GetName");
        console.log(`Welcome, ${guestName}! Enjoy your global space.`);
    };
    
</script>

Node.js

const HelloWorld = require("HelloWorld");

// This function is invoked by DotNet.
global.getName = () => "Node.js";

(async function () {
    // Booting the DotNet runtime and invoking entry point.
    await HelloWorld.boot();
    // Invoking 'GetName()' method from DotNet.
    const guestName = HelloWorld.invoke("GetName");
    console.log(`Welcome, ${guestName}! Enjoy your CommonJS module space.`);
})();

Example Projects

You can find the following sample projects in this repository:

  • Hello World — Consume DotNetJS-compiled program as a global import in browser, CommonJS or ECMAScript (ES) module in node.
  • Web Extension — Consume the library in VS Code web extension, which works with both web and standalone versions of the IDE.
  • Runtime Tests — Integration tests featuring various usage scenarios: async invocations, interop with instances, sending raw byte arrays, streaming, etc.

Build Properties

You can specify the following optional properties in .csproj to customize the build:

  • <Clean>false<Clean> Do not clean the build output folders
  • <LibraryName>CustomName</LibraryName> Provide a custom name for the compiled library and export object.

For example, the following configuration will preserve the WebAssembly build artifacts and produce my-dotnet-lib.js library with my-dotnet-lib export object:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <LangVersion>10</LangVersion>
        <Clean>false</Clean>
        <LibraryName>my-dotnet-lib</LibraryName>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="DotNetJS" Version="0.1.0"/>
    </ItemGroup>

</Project>

Compiling Runtime

To compile and test the runtime run the following in order (under Runtime folder):

scripts/install-emsdk.sh
scripts/compile-runtime.sh
scripts/compile-test.sh
npm build
npm test

Publishing Runtime

A memo for the publishing process after modifying JS runtime.

  1. Bump NPM version on ./Runtime/package.json and:
  • npm run build
  • scripts/publish-package.sh
  1. Bump NuGet version on ./DotNetJS/DotNetJS.csproj and:
  • dotnet pack -c Release --output bin
  • dotnet nuget push bin/DotNetJS.{VER}.nupkg --api-key {KEY} --source https://api.nuget.org/v3/index.json
  1. Wait for the package indexing, bump NuGet version on ./Runtime/test/Test.csproj and:
  • script/compile-test.sh
  1. Remind myself that this should be automated.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DotNetJS:

Package Downloads
Plotly.WPF

Plotly.js in a WPF Control

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.23.4 1,783 5/14/2023 0.23.4 is deprecated because it is no longer maintained.
0.23.3 870 2/21/2023 0.23.3 is deprecated because it is no longer maintained.
0.23.2 287 2/21/2023 0.23.2 is deprecated because it is no longer maintained.
0.23.1 551 1/27/2023 0.23.1 is deprecated because it is no longer maintained.
0.23.0 343 1/27/2023 0.23.0 is deprecated because it is no longer maintained.
0.22.0 451 1/21/2023 0.22.0 is deprecated because it is no longer maintained.
0.21.1 381 12/23/2022 0.21.1 is deprecated because it is no longer maintained.
0.21.0 461 12/5/2022 0.21.0 is deprecated because it is no longer maintained.
0.20.0 401 12/4/2022 0.20.0 is deprecated because it is no longer maintained.
0.19.0 393 11/24/2022 0.19.0 is deprecated because it is no longer maintained.
0.18.0 827 11/20/2022 0.18.0 is deprecated because it is no longer maintained.
0.17.1 528 10/15/2022 0.17.1 is deprecated because it is no longer maintained.
0.17.0 479 10/10/2022 0.17.0 is deprecated because it is no longer maintained.
0.16.0 553 10/1/2022 0.16.0 is deprecated because it is no longer maintained.
0.15.1 499 9/1/2022 0.15.1 is deprecated because it is no longer maintained.
0.15.0 553 8/24/2022 0.15.0 is deprecated because it is no longer maintained.
0.14.0 598 7/1/2022 0.14.0 is deprecated because it is no longer maintained.
0.13.8 573 6/6/2022 0.13.8 is deprecated because it is no longer maintained.
0.13.7 454 6/4/2022 0.13.7 is deprecated because it is no longer maintained.
0.13.5 452 6/4/2022 0.13.5 is deprecated because it is no longer maintained.
0.13.4 479 6/4/2022 0.13.4 is deprecated because it is no longer maintained.
0.13.3 443 6/3/2022 0.13.3 is deprecated because it is no longer maintained.
0.13.2 479 5/24/2022 0.13.2 is deprecated because it is no longer maintained.
0.13.1 490 5/23/2022 0.13.1 is deprecated because it is no longer maintained.
0.13.0 488 5/22/2022 0.13.0 is deprecated because it is no longer maintained.
0.12.0 486 5/21/2022 0.12.0 is deprecated because it is no longer maintained.
0.11.0 505 5/21/2022 0.11.0 is deprecated because it is no longer maintained.
0.10.1 465 5/19/2022 0.10.1 is deprecated because it is no longer maintained.
0.10.0 545 5/1/2022 0.10.0 is deprecated because it is no longer maintained.
0.9.2 462 4/29/2022 0.9.2 is deprecated because it is no longer maintained.
0.9.1 490 4/17/2022 0.9.1 is deprecated because it is no longer maintained.
0.9.0 460 4/8/2022 0.9.0 is deprecated because it is no longer maintained.
0.8.2 524 3/22/2022 0.8.2 is deprecated because it is no longer maintained.
0.8.1 474 3/9/2022 0.8.1 is deprecated because it is no longer maintained.
0.8.0 487 2/20/2022 0.8.0 is deprecated because it is no longer maintained.
0.7.3 627 2/16/2022 0.7.3 is deprecated because it is no longer maintained.
0.6.3 516 2/12/2022 0.6.3 is deprecated because it is no longer maintained.
0.6.2 496 2/9/2022 0.6.2 is deprecated because it is no longer maintained.
0.5.4 493 2/8/2022 0.5.4 is deprecated because it is no longer maintained.
0.5.3 601 2/1/2022 0.5.3 is deprecated because it is no longer maintained.
0.5.2 527 1/17/2022 0.5.2 is deprecated because it is no longer maintained.
0.5.1 484 1/13/2022 0.5.1 is deprecated because it is no longer maintained.
0.5.0 513 1/13/2022 0.5.0 is deprecated because it is no longer maintained.
0.4.1 352 1/10/2022 0.4.1 is deprecated because it is no longer maintained.
0.4.0 358 12/24/2021 0.4.0 is deprecated because it is no longer maintained.
0.3.13 336 12/23/2021 0.3.13 is deprecated because it is no longer maintained.
0.3.12 654 12/4/2021 0.3.12 is deprecated because it is no longer maintained.
0.3.11 923 12/1/2021 0.3.11 is deprecated because it is no longer maintained.
0.3.8 366 11/30/2021 0.3.8 is deprecated because it is no longer maintained.
0.2.5 889 11/21/2021 0.2.5 is deprecated because it is no longer maintained.
0.2.0 498 11/19/2021 0.2.0 is deprecated because it is no longer maintained.
0.1.0 374 11/18/2021 0.1.0 is deprecated because it is no longer maintained.