Bundle Transformer: Autoprefixer/JavaScript Engine Switcher for .Net: V8安装指南

2016年6月9日

公司有项目使用Bundle Transformer: Autoprefixer,它内部执行JavaScript以添加CSS私有前缀。JavaScript Engine Switcher目前支持三种JavaScript引擎:JavaScriptEngineSwitcher.Msie、JavaScriptEngineSwitcher.V8、JavaScriptEngineSwitcher.ChakraCore。

一开始以为这三种引擎差不多,于是选择了赫赫有名的V8引擎,结果发现它需要安装好几个动态链接库。

Theory

非托管类库的加载方式与托管类库不同。非托管类库并不会被复制到Temporary ASP.NET Files文件夹,而且需要存放在当前目录或环境变量PATH指定的目录。详见Dynamic-Link Library Search Order

JavaScript Engine Switcher类库调用ClearScript类库,如果选择加载V8引擎,则根据操作系统32位或64位,还需要ClearScriptV8-32.dll或ClearScriptV8-64.dll、v8-ia32.dll或v8-x64.dll。以32位系统为例,v8-ia32.dll是非托管的动态链接库,ClearScriptV8-32.dll是它的.NET包装类,内部调用v8-ia32.dll。考虑到上述的Windows动态链接库加载逻辑,v8-ia32.dll应被放在类似C:\Windows和C:\Windows\system32的目录,或者与被加载的ClearScriptV8-32.dll在相同目录.

对于ASP.NET项目,JavaScript Engine Switcher的Nuget包的默认安装是把ClearScriptV8-32.dll、ClearScriptV8-64.dll、v8-ia32.dll、v8-x64.dll放在bin\ClearScript.V8文件夹中。公司要研究一下能否直接放在bin文件夹里,不要创建子文件夹。

如果不把v8-ia32.dll安装到C:\Windows或PATH环境变量指定的目录,则不能加载在Temporary ASP.NET Files目录里的ClearScriptV8-32.dll,除非有某种机制把v8-ia32.dll也复制过去。

其实ClearScript类库已经有解决方案,V8Proxy.cs会从以下位置先加载v8-ia32.dll,然后加载ClearScriptV8-32.dll:Type.Assembly.Location,即Temporary ASP.NET Files文件夹、AppDomain.CurrentDomainappDomain.RelativeSearchPath,即bin文件夹。如果在一个位置找不到DLL,.NET会抛出异常,但是V8Proxy会掩盖异常。如果在以上位置都找不到v8-ia32.dll,V8Proxy才会自行抛出异常。

ClearScriptV8-32.dll is a .NET wrapper for v8-ia32.dll. But as ClearScript.dll already loaded v8-ia32.dll into memory, ClearScriptV8-32.dll doesn’t have to search for it, and will not have loading issues.

We used to see the loading error saying the ClearScript.V8 folder is not found. It’s thrown by JavaScriptEngineSwitcher.V8.AssemblyResolver and the loading is not triggered by V8Proxy.

The web.config files for our main sites have in so ClearScriptV8-32.dll isn’t actively loaded by .NET runtime. WebControlTest site doesn’t have such directive.

Conclusion

It’s fine to place ClearScriptV8-32.dll, v8-ia32.dll, and the 64 bit ones in the bin folder. The ClearScript.V8 subfolder is not necessary.

The web.config files should have at least if we don’t remove all.

After the change, you may see an error saying the ClearScript.V8 folder is not found, it’s related to page output cache set in CStaticResourceHandler. Restarting IIS apparently doesn’t clear the cache.

作者:爱让一切都对了
本文以知识共享-署名-相同方式共享3.0协议释出
2016年6月10日