Skip to content

Registration of JS engines

Taritsyn edited this page Feb 7, 2024 · 12 revisions

After installing NuGet packages, you need to register JS engines. For registration of JS engines uses the following properties of IJsEngineSwitcher interface:

Property name Data type Default value Description
DefaultEngineName String Empty string Name of default JS engine
EngineFactories JsEngineFactoryCollection Empty сollection Collection of JS engine factories

The table shows, that the EngineFactories property is empty by default, i.e. is not registered any JS engine. In version 1.X JS engines were registered automatically during installation of corresponding NuGet packages (by using transformations of configuration files). Since version 2.X registration of JS engines you need to make manually.

Registration of JS engines should be done only once, and before to call of the CreateEngine and CreateDefaultEngine methods of IJsEngineSwitcher interface. In general, configuration of the JavaScript Engine Switcher is as follows:

IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
engineSwitcher.EngineFactories.Add(new ChakraCoreJsEngineFactory());
engineSwitcher.EngineFactories.Add(new JintJsEngineFactory());
engineSwitcher.EngineFactories.Add(new JurassicJsEngineFactory());
engineSwitcher.EngineFactories.Add(new MsieJsEngineFactory(new MsieSettings
{
    EngineMode = JsEngineMode.ChakraIeJsRt
}));
engineSwitcher.EngineFactories.Add(new NiLJsEngineFactory());
engineSwitcher.EngineFactories.Add(new NodeJsEngineFactory());
engineSwitcher.EngineFactories.Add(new V8JsEngineFactory());
engineSwitcher.EngineFactories.Add(new VroomJsEngineFactory());
engineSwitcher.EngineFactories.Add(new YantraJsEngineFactory());

engineSwitcher.DefaultEngineName = "ChakraCoreJsEngine";

This code can be simplified by using extension methods and constants:

IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
engineSwitcher.EngineFactories
    .AddChakraCore()
    .AddJint()
    .AddJurassic()
    .AddMsie(new MsieSettings
    {
        EngineMode = JsEngineMode.ChakraIeJsRt
    })
    .AddNiL()
    .AddNode()
    .AddV8()
    .AddVroom()
    .AddYantra()
    ;

engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName;

Next, consider examples of the JavaScript Engine Switcher configuration in ASP.NET applications.

ASP.NET 4.X

Configuring of the JavaScript Engine Switcher in many ways resembles configuration of the Microsoft ASP.NET Web Optimization Framework.

In ASP.NET MVC and Web Forms applications configuring of the JavaScript Engine Switcher maked in App_Start/JsEngineSwitcherConfig.cs file:

using JavaScriptEngineSwitcher.ChakraCore;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.Jint;
using JavaScriptEngineSwitcher.Jurassic;
using JavaScriptEngineSwitcher.Msie;
using JavaScriptEngineSwitcher.NiL;
using JavaScriptEngineSwitcher.Node;
using JavaScriptEngineSwitcher.V8;
using JavaScriptEngineSwitcher.Vroom;
using JavaScriptEngineSwitcher.Yantra;

namespace JavaScriptEngineSwitcher.Sample.AspNet461.Mvc5
{
    public class JsEngineSwitcherConfig
    {
        public static void Configure(IJsEngineSwitcher engineSwitcher)
        {
            engineSwitcher.EngineFactories
                .AddChakraCore()
                .AddJint()
                .AddJurassic()
                .AddMsie(new MsieSettings
                {
                    EngineMode = JsEngineMode.ChakraIeJsRt
                })
                .AddNiL()
                .AddNode()
                .AddV8()
                .AddVroom()
                .AddYantra()
                ;

            engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName;
        }
    }
}

In order for these configuration settings to take effect, you must also add the JsEngineSwitcherConfig.Configure method call in the Global.asax file:

using System.Web;using System.Web.Routing;

using JavaScriptEngineSwitcher.Core;

namespace JavaScriptEngineSwitcher.Sample.AspNet461.Mvc5
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            …
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            JsEngineSwitcherConfig.Configure(JsEngineSwitcher.Current);}
    }
}

In ASP.NET Web Pages sites instead of the App_Start/JsEngineSwitcherConfig.cs and Global.asax files is used only one file - _AppStart.cshtml:

@using JavaScriptEngineSwitcher.ChakraCore
@using JavaScriptEngineSwitcher.Core
@using JavaScriptEngineSwitcher.Jint
@using JavaScriptEngineSwitcher.Jurassic
@using JavaScriptEngineSwitcher.Msie
@using JavaScriptEngineSwitcher.NiL
@using JavaScriptEngineSwitcher.Node
@using JavaScriptEngineSwitcher.V8
@using JavaScriptEngineSwitcher.Vroom
@using JavaScriptEngineSwitcher.Yantra

@{
    …
    #region JavaScript Engine Switcher configuration

    IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current;
    engineSwitcher.EngineFactories
        .AddChakraCore()
        .AddJint()
        .AddJurassic()
        .AddMsie(new MsieSettings
        {
            EngineMode = JsEngineMode.ChakraIeJsRt
        })
        .AddNiL()
        .AddNode()
        .AddV8()
        .AddVroom()
        .AddYantra()
        ;

    engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName;

    #endregion
    …
}

ASP.NET Core

In ASP.NET Core applications configuring of the JavaScript Engine Switcher maked in the Startup.cs file:

using Microsoft.Extensions.DependencyInjection;using JavaScriptEngineSwitcher.ChakraCore;
using JavaScriptEngineSwitcher.Extensions.MsDependencyInjection;
using JavaScriptEngineSwitcher.Jint;
using JavaScriptEngineSwitcher.Jurassic;
using JavaScriptEngineSwitcher.Msie;
using JavaScriptEngineSwitcher.NiL;
using JavaScriptEngineSwitcher.Node;using JavaScriptEngineSwitcher.V8;
using JavaScriptEngineSwitcher.Vroom;
using JavaScriptEngineSwitcher.Yantra;namespace JavaScriptEngineSwitcher.Sample.AspNetCore.Mvc
{
    public class Startup
    {// This method gets called by the runtime.
        // Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {// Add JavaScriptEngineSwitcher services to the services container.
            services.AddJsEngineSwitcher(options =>
            {
                options.AllowCurrentProperty = false;
                options.DefaultEngineName = ChakraCoreJsEngine.EngineName;
            })
                .AddChakraCore()
                .AddJint()
                .AddJurassic()
                .AddMsie(options => {
                    options.EngineMode = JsEngineMode.ChakraIeJsRt;
                })
                .AddNiL()
                .AddNode()
                .AddV8()
                .AddVroom()
                .AddYantra()
                ;

            // Add framework services.}}
}

* - This example is valid only for ASP.NET Core applications that supports .NET Framework 4.6.1, .NET Core App 3.1 and .NET 5.0.

First of all, you need to install the JavaScriptEngineSwitcher.Extensions.MsDependencyInjection package, that contains definition of the AddJsEngineSwitcher extension method. AddJsEngineSwitcher method adds an instance of a class that implements the IJsEngineSwitcher interface to the services container in the form of singleton and returns value of the EngineFactories property. It should also be noted, that for configuration the IJsEngineSwitcher interface implementation and individual JS engines do not use the Microsoft.Extensions.Options (it behavior is just simulated). AllowCurrentProperty configuration property allows to deny access to the JsEngineSwitcher.Current property in order to ensure that the web application will receive a instance of the JS engine switcher only through a built-in service container (required in some testing scenarios using a WebApplicationFactory).