DotNetJS 0.3.11

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.3.11
NuGet\Install-Package DotNetJS -Version 0.3.11
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.3.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetJS --version 0.3.11
#r "nuget: DotNetJS, 0.3.11"
#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.3.11

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

DotNetJS

NuGet npm CodeFactor codecov CodeQL

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

The solution is based on two main components:

  • JavaScript/dotnet-runtime (npm). Consumes compiled C# assemblies and .NET runtime WebAssembly module to provide C# interoperability layer in JavaScript. The library is environment-agnostic — it doesn't depend on platform-specific APIs, like browser DOM or node modules and can be imported as CommonJS or ECMAScript module or consumed via script tag in browsers.
  • DotNet/DotNetJS (NuGet). Provides JavaScript interoperability layer in C# and packs project output into single-file JavaScript library via MSBuild task. Produced library contains dotnet-runtime initialized with the project assemblies and ready to be used as interoperability layer for the packaged C# project. Can optionally emit type definitions to bootstrap TypeScript development.

Quick Start

In C# project configuration file specify Microsoft.NET.Sdk.BlazorWebAssembly SDK and import DotNetJS NuGet package:

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

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

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

</Project>

To associate a JavaScript function with a C# method use JSFunction attribute. To expose a C# method to JavaScript, use JSInvokable attribute:

using System;
using DotNetJS;
using Microsoft.JSInterop;

namespace HelloWorld;

partial class Program
{
    // Entry point is invoked by the JavaScript runtime on boot.
    public static void Main ()
    {
        // Invoking 'dotnet.HelloWorld.GetHostName()' JavaScript function.
        var hostName = GetHostName();
        // Writing to JavaScript host console.
        Console.WriteLine($"Hello {hostName}, DotNet here!");
    }
    
    [JSFunction] // The interoperability code is auto-generated.
    public static partial string GetHostName ();

    [JSInvokable] // The method is invoked from JavaScript.
    public static string GetName () => "DotNet";
}

Publish the project with dotnet publish. A single-file dotnet.js library will be produced under the "bin" directory. Consume the library depending on the environment:

Browser


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

<script>

    // Providing implementation for 'GetHostName' function declared in 'HelloWorld' C# assembly.
    dotnet.HelloWorld.GetHostName = () => "Browser";
    
    window.onload = async function () {
        // Booting the DotNet runtime and invoking entry point.
        await dotnet.boot();
        // Invoking 'GetName()' C# method defined in 'HelloWorld' assembly.
        const guestName = dotnet.HelloWorld.GetName();
        console.log(`Welcome, ${guestName}! Enjoy your global space.`);
    };
    
</script>

Node.js

// Import as CommonJS module.
const dotnet = require("dotnet");
// ... or as ECMAScript module in node v17 or later.
import dotnet from "dotnet.js";

// Providing implementation for 'GetHostName' function declared in 'HelloWorld' C# assembly.
dotnet.HelloWorld.GetHostName = () => "Node.js";

(async function () {
    // Booting the DotNet runtime and invoking entry point.
    await dotnet.boot();
    // Invoking 'GetName()' C# method defined in 'HelloWorld' assembly.
    const guestName = dotnet.HelloWorld.GetName();
    console.log(`Welcome, ${guestName}! Enjoy your module space.`);
})();

Example Projects

Find the following sample projects in this repository:

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

Build Properties

Specify following optional properties in .csproj to customize the build:

  • <EmitSourceMap>true</EmitSourceMap> — emit JavaScript source map file.
  • <EmitTypes>true</EmitTypes> — emit TypeScript type definitions file.

For example, following configuration will emit source map and type definitions:

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

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <EmitSourceMap>true</EmitSourceMap>
        <EmitTypes>true</EmitTypes>
    </PropertyGroup>

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

</Project>

Compiling Runtime

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

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

Publishing Runtime

A memo for the publishing process after modifying dotnet-runtime:

  1. Bump NPM version on JavaScript/dotnet-runtime/package.json and:
  • npm run build
  • scripts/publish-package.sh
  1. Bump NuGet version on DotNet/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
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,568 5/14/2023
0.23.3 850 2/21/2023
0.23.2 273 2/21/2023
0.23.1 517 1/27/2023
0.23.0 311 1/27/2023
0.22.0 418 1/21/2023
0.21.1 366 12/23/2022
0.21.0 424 12/5/2022
0.20.0 370 12/4/2022
0.19.0 358 11/24/2022
0.18.0 794 11/20/2022
0.17.1 494 10/15/2022
0.17.0 439 10/10/2022
0.16.0 533 10/1/2022
0.15.1 456 9/1/2022
0.15.0 517 8/24/2022
0.14.0 560 7/1/2022
0.13.8 533 6/6/2022
0.13.7 438 6/4/2022
0.13.5 418 6/4/2022
0.13.4 443 6/4/2022
0.13.3 411 6/3/2022
0.13.2 464 5/24/2022
0.13.1 457 5/23/2022
0.13.0 455 5/22/2022
0.12.0 453 5/21/2022
0.11.0 470 5/21/2022
0.10.1 451 5/19/2022
0.10.0 510 5/1/2022
0.9.2 445 4/29/2022
0.9.1 458 4/17/2022
0.9.0 448 4/8/2022
0.8.2 489 3/22/2022
0.8.1 458 3/9/2022
0.8.0 453 2/20/2022
0.7.3 573 2/16/2022
0.6.3 481 2/12/2022
0.6.2 465 2/9/2022
0.5.4 461 2/8/2022
0.5.3 561 2/1/2022
0.5.2 492 1/17/2022
0.5.1 469 1/13/2022
0.5.0 480 1/13/2022
0.4.1 319 1/10/2022
0.4.0 324 12/24/2021
0.3.13 304 12/23/2021
0.3.12 622 12/4/2021
0.3.11 890 12/1/2021
0.3.8 335 11/30/2021
0.2.5 855 11/21/2021
0.2.0 464 11/19/2021
0.1.0 359 11/18/2021