Skip to content

Registration of JS engines

Andrey Taritsyn edited this page Jun 13, 2018 · 12 revisions

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

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). In 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 JsEngineSwitcher class. In general, configuration of the JavaScript Engine Switcher is as follows:

JsEngineSwitcher engineSwitcher = JsEngineSwitcher.Instance;
engineSwitcher.EngineFactories.Add(new ChakraCoreJsEngineFactory());
engineSwitcher.EngineFactories.Add(new JintJsEngineFactory());
engineSwitcher.EngineFactories.Add(new JurassicJsEngineFactory());
engineSwitcher.EngineFactories.Add(new MsieJsEngineFactory(new MsieSettings
{
	UseEcmaScript5Polyfill = true,
	UseJson2Library = true
}));
engineSwitcher.EngineFactories.Add(new V8JsEngineFactory());

engineSwitcher.DefaultEngineName = "ChakraCoreJsEngine";

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

JsEngineSwitcher engineSwitcher = JsEngineSwitcher.Instance;
engineSwitcher.EngineFactories
	.AddChakraCore()
	.AddJint()
	.AddJurassic()
	.AddMsie(new MsieSettings
	{
		UseEcmaScript5Polyfill = true,
		UseJson2Library = true
	})
	.AddV8()
	;

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.V8;

namespace JavaScriptEngineSwitcher.Sample.AspNet4.Mvc4
{
	public class JsEngineSwitcherConfig
	{
		public static void Configure(JsEngineSwitcher engineSwitcher)
		{
			engineSwitcher.EngineFactories
				.AddChakraCore()
				.AddJint()
				.AddJurassic()
				.AddMsie(new MsieSettings
				{
					UseEcmaScript5Polyfill = true,
					UseJson2Library = true
				})
				.AddV8()
				;

			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.AspNet4.Mvc4
{
	public class MvcApplication : HttpApplication
	{
		protected void Application_Start()
		{RouteConfig.RegisterRoutes(RouteTable.Routes);
			JsEngineSwitcherConfig.Configure(JsEngineSwitcher.Instance);}
	}
}

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.V8

@{
	…
	#region JavaScript Engine Switcher configuration

	JsEngineSwitcher engineSwitcher = JsEngineSwitcher.Instance;
	engineSwitcher.EngineFactories
		.AddChakraCore()
		.AddJint()
		.AddJurassic()
		.AddMsie(new MsieSettings
		{
			UseEcmaScript5Polyfill = true,
			UseJson2Library = true
		})
		.AddV8()
		;

	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.AspNetCore.Mvc;using Microsoft.Extensions.DependencyInjection;using JavaScriptEngineSwitcher.ChakraCore;
using JavaScriptEngineSwitcher.Extensions.MsDependencyInjection;
using JavaScriptEngineSwitcher.Jint;
using JavaScriptEngineSwitcher.Jurassic;
using JavaScriptEngineSwitcher.Msie;
using JavaScriptEngineSwitcher.Sample.Logic.Services;
using JavaScriptEngineSwitcher.V8;namespace JavaScriptEngineSwitcher.Sample.AspNetCore1Full.Mvc1
{
	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.DefaultEngineName = ChakraCoreJsEngine.EngineName
			)
				.AddChakraCore()
				.AddJint()
				.AddJurassic()
				.AddMsie(options => {
					options.UseEcmaScript5Polyfill = true;
					options.UseJson2Library = true;
				})
				.AddV8()
				;

			// Add framework services.
			services.AddMvc();}}
}

* - This example is valid only for web application created by the “ASP.NET Core Web Application (.NET Framework)” template. If you create a web application based on the “ASP.NET Core Web Application (.NET Core)” template, then you will have only four JS engines: JavaScriptEngineSwitcher.ChakraCore, JavaScriptEngineSwitcher.Msie (only works in JsRT modes), JavaScriptEngineSwitcher.Jint and JavaScriptEngineSwitcher.Vroom.

First of all, you need to install the JavaScriptEngineSwitcher.Extensions.MsDependencyInjection package, that contains definition of the AddJsEngineSwitcher extension method. AddJsEngineSwitcher method adds a instance of the JsEngineSwitcher class to the services container in the form of singleton and returns value of the EngineFactories property. It should also be noted, that for configuration of instance of the JsEngineSwitcher class and individual JS engines do not use the Microsoft.Extensions.Options (it behavior is just simulated).