I have a .NET 8 Core C# MVC project to provide the content as website and a class library to host the logic.
I'm trying to run ua-parser-js through JavaScriptEngineSwitcher as I couldnt find a C# based project like this which was recently updated.
I installed the nuget packages to my class library and added the JsEngineSwitcher to the available services in my MVC projects Program.cs file.
builder.Services.AddJsEngineSwitcher(options =>
{
options.AllowCurrentProperty = false;
options.DefaultEngineName = JintJsEngine.EngineName;
}).AddJint();
In my Controller I load the injected Service as below
var jsEngineSwitcher = _serviceProvider.GetService<IJsEngineSwitcher>();
var engine = jsEngineSwitcher.CreateDefaultEngine();
I'm passing the UA and the engine to my class library method where I am trying to load the JS file. The JS file is added as a embedded ressource in the root folder of the class library.
engine.ExecuteResource("<class library name>.ua-parser.min.js", typeof(Program).Assembly);
Unfortunately this fails with
System.NullReferenceException: 'Resource with name '<class library name>.ua-parser.min.js' is null.'
I checked if the JS file is really exiting as a ressource by below code, and it does have ".ua-parser.min.js"
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
The sample projects in the github repo seem not to load any JS and I wasn't able to find a recent example except this.
Anyone having an idea what I am doing wrong?
Figured it out, instead of
engine.ExecuteResource("<class library name>.ua-parser.min.js", typeof(Program).Assembly);
I had to use
engine.ExecuteResource("<class library name>.ua-parser.min.js", typeof(<class library name here>).Assembly);