DotJS 8.0.0-rc.4-alpha

This is a prerelease version of DotJS.
dotnet add package DotJS --version 8.0.0-rc.4-alpha
                    
NuGet\Install-Package DotJS -Version 8.0.0-rc.4-alpha
                    
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="DotJS" Version="8.0.0-rc.4-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotJS" Version="8.0.0-rc.4-alpha" />
                    
Directory.Packages.props
<PackageReference Include="DotJS" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotJS --version 8.0.0-rc.4-alpha
                    
#r "nuget: DotJS, 8.0.0-rc.4-alpha"
                    
#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.
#:package DotJS@8.0.0-rc.4-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotJS&version=8.0.0-rc.4-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DotJS&version=8.0.0-rc.4-alpha&prerelease
                    
Install as a Cake Tool

DotJS

DotJS is a lightweight C# library for seamless communication between the browser and the server, making it easy to call JavaScript methods from C# and vice versa. This README will guide you through the basics of setting up and using DotJS.


Features

  • JavaScript Integration: Inject JavaScript code directly into the browser using the Script property.
  • Async Method Invocation: Call JavaScript methods from C# and handle the responses asynchronously.
  • Object References: Manage JavaScript references for efficient interaction with browser-side objects.

Installation

To install DotJS, add the library to your project via NuGet:

dotnet add package DotJS

Usage

Setting Up

To get started, initialize a new instance of the JS class. Here’s an example:

using DotJS;
using Constellations;

// Initialize the Constellation instance
var constellation = new Constellation("MyServer");

// Create a JS instance
var jsInterop = new JS(
    id: "my-session-id",
    constellation: constellation,
    key: "secure-key"
);

// Access the JavaScript injection script
string script = jsInterop.Script;

// Inject the script into your HTML page
// Example: Serve it to the browser or append it to the DOM dynamically
Console.WriteLine(script);

Injecting the JavaScript Script

The Script property contains a JavaScript snippet that establishes communication between the browser and the server. You can inject this script into your webpage via:

  1. Including it as a script tag in your HTML.
  2. Sending it dynamically to the client via your server framework.

Calling JavaScript Methods from C#

Use the InvokeAsync method to call JavaScript methods from C#. Here’s how:

// Example: Call a JavaScript method named 'showAlert'
var result = await jsInterop.InvokeAsync(
    methodName: "showAlert",
    args: new object[] { "Hello from C#!" }
);

Console.WriteLine(result); // Output from the JavaScript method

Cleanup

When you no longer need the JS instance, ensure proper cleanup to release resources:

jsInterop.Dispose();

This will:

  • Remove any user keys associated with the session.
  • Unsubscribe from event handlers.

Example Workflow

Here’s a complete example that demonstrates DotJS in action:

using DotJS;
using Constellations;

class Program
{
    static async Task Main(string[] args)
    {
        var constellation = new Constellation("MyServer").AllowOrigins("yoursite.com").Run();

        var jsInterop = new JS("session-123", constellation, "secure-key");

        

        // Inject the script
        Console.WriteLine("Inject this script into your browser:");
        Console.WriteLine(jsInterop.Script);

        // Call a JavaScript function
        string result = await jsInterop.InvokeAsync("alert", new object[] { "Hello from DotJS!" });
        Console.WriteLine($"Result: {result}");

        // Cleanup
        jsInterop.Dispose();
    }
}

Contributing

Feel free to open issues or submit pull requests to improve DotJS. Contributions are welcome!


Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 DotJS:

Package Downloads
Vibe

Vibe allows you to use .csx files like .jsx files but in c#.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.0.0-rc.4-alpha 93 2/8/2025
8.0.0-rc.3-alpha 72 1/29/2025
8.0.0-rc.2-alpha 155 1/25/2025
8.0.0-rc.1-alpha 79 1/24/2025