Skip to content
Open core software, built in Singapore Established 2018  /  Five utilities
Jering
Talk to us

Dependency Injection

Web.SyntaxHighlighters.HighlightJS Jering documentation Open source NuGet package
Documentation menu

The recommended setup registers the highlighter with Microsoft.Extensions.DependencyInjection and injects the service where it is needed.

Registration

C#
var services = new ServiceCollection();
services.AddHighlightJS();
var provider = services.BuildServiceProvider();
var highlighter = provider.GetRequiredService<IHighlightJSService>();

Lifetime

The service is a singleton and its members are thread safe. Resolve it once and share it. Creating a new instance per request starts a Node.js process per request and will not scale.

Testing

Inject IHighlightJSService rather than calling the static API. A fake implementation can return fixed markup in tests and removes the need for Node.js in the test environment.

The static alternative

StaticHighlightJSService exposes the same operations for applications without a container. Use it in a console tool or a small script. In a hosted application, the container keeps lifetime and configuration explicit.

Reference shelf