概述linux
一般JavaScript在Web瀏覽器執行展示特定效果,C#也能夠後臺執行JavaScript裏面方法;C#一般調用第三方DLL方式:MsieJavaScriptEngine或者JavaScriptEngineSwitcher。具體使用列舉以下;瀏覽器
MsieJavaScriptEngine使用spa
Nuget裏面,添加MsieJavaScriptEngine的類庫;因爲MsieJavaScriptEngine不支持.NET Core下Linux的部署運行,代碼使用方式以下:code
public static string Ck3(string cert, string ts, string nullPara, string page, string token) { var basePath = AppDomain.CurrentDomain.BaseDirectory; using (var jsEngine = new MsieJsEngine()) { jsEngine.ExecuteFile(string.Format(@"{0}/Scripts/myscript.js", basePath)); string[] arr = new string[] { cert, ts, nullPara, page, token }; jsEngine.Execute("var $CFMethod=$.ck;"); var publickey = jsEngine.CallFunction("$CFMethod", arr); return publickey.ToString(); } }
JavaScriptEngineSwitcher的使用orm
Nuget裏面,添加JavaScriptEngineSwitcher的類庫;因爲JavaScriptEngineSwitcher支持.NET Core下Linux和Win的部署運行,另外還須要安裝的類庫blog
JavaScriptEngineSwitcher.ChakraCore.Native.linux-x64 ///針對Linux-x64環境下組件;token
JavaScriptEngineSwitcher.ChakraCore.Native.win-x64 //針對Wind-x64環境下組件;ip
JavaScriptEngineSwitcher.ChakraCore.Native.win-x32 //針對Wind-x32環境下組件;ci
代碼使用方式以下:部署
public static string Ck(string cert, string ts, string nullPara, string page, string token) { var basePath = AppDomain.CurrentDomain.BaseDirectory; IJsEngineSwitcher engineSwitcher = JsEngineSwitcher.Current; engineSwitcher.EngineFactories.Add(new ChakraCoreJsEngineFactory()); engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName; using (IJsEngine engine = JsEngineSwitcher.Current.CreateDefaultEngine()) { engine.ExecuteFile(string.Format(@"{0}/Scripts/myscipt.js", basePath)); string[] arr = new string[] { cert, ts, nullPara, page, token }; engine.Execute("var $CFMethod=$.ck;"); var publickey = engine.CallFunction("$CFMethod", arr); return publickey.ToString(); } }