当前位置: 首页 > wzjs >正文

可以做推广的网站seo推广沧州公司电话

可以做推广的网站,seo推广沧州公司电话,网站建设工作情况总结,专业的网站建设热线电话C#调用js还是没有python方便,但是微软出的V8算是目前最全的JS引擎了,完整的模拟了浏览器环境。并且支持js和C#双向通信。 下面是单例代码,可以直接调用。 using Microsoft.ClearScript; using Microsoft.ClearScript.V8; using System.Diag…

C#调用js还是没有python方便,但是微软出的V8算是目前最全的JS引擎了,完整的模拟了浏览器环境。并且支持js和C#双向通信。

下面是单例代码,可以直接调用。

using Microsoft.ClearScript;
using Microsoft.ClearScript.V8;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;public sealed class SM2CryptoService : IDisposable
{private static readonly Lazy<SM2CryptoService> _lazyInstance =new Lazy<SM2CryptoService>(() => new SM2CryptoService(), LazyThreadSafetyMode.ExecutionAndPublication);public static SM2CryptoService Instance => _lazyInstance.Value;private const V8ScriptEngineFlags EngineFlags =V8ScriptEngineFlags.EnableTaskPromiseConversion |V8ScriptEngineFlags.EnableDateTimeConversion |V8ScriptEngineFlags.DisableGlobalMembers;private static readonly TimeSpan FileReadRetryDelay = TimeSpan.FromMilliseconds(100);private const int MaxFileReadRetries = 3;private readonly V8ScriptEngine _jsEngine;private bool _disposed;private readonly object _engineLock = new();private SM2CryptoService(){try{string jsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Areas", "GZ_RideAuth", "Js", "sm2.js");LogInfo($"初始化SM2服务,JS路径: {jsPath}");if (!File.Exists(jsPath))throw new FileNotFoundException("SM2加密库文件未找到", jsPath);string jsCode = ReadJsFileWithRetry(jsPath, MaxFileReadRetries);_jsEngine = new V8ScriptEngine("SM2Engine", EngineFlags){AllowReflection = false,MaxRuntimeHeapSize = 1024 * 1024 * 1024 // 20MB内存限制};_jsEngine.Execute(jsCode);LogInfo("服务初始化成功");}catch (Exception ex){LogError("初始化致命错误", ex);Dispose();throw new CryptographicException("SM2加密服务初始化失败", ex);}}public string Encrypt(string plainText){if (_disposed)throw new ObjectDisposedException(GetType().FullName);if (string.IsNullOrWhiteSpace(plainText))return string.Empty;lock (_engineLock){var sw = Stopwatch.StartNew();try{var result = _jsEngine.Script.window.SM2.sm2Encrypt(plainText);string cipherText = result.ToString();LogInfo($"加密完成,耗时: {sw.ElapsedMilliseconds}ms");return cipherText;}catch (ScriptEngineException ex){LogError("JS执行错误", ex);throw new CryptographicException("SM2加密过程出错", ex);}catch (Exception ex){LogError("系统错误", ex);throw;}}}public void Dispose(){if (_disposed) return;lock (_engineLock){if (_disposed) return;try{_jsEngine?.Execute("window.SM2.cleanup && window.SM2.cleanup()");_jsEngine?.Dispose();LogInfo("V8引擎已释放");}catch (Exception ex){LogError("资源释放异常", ex);}finally{_disposed = true;GC.SuppressFinalize(this);}}}~SM2CryptoService() => Dispose();private string ReadJsFileWithRetry(string path, int maxRetries){for (int i = 0; i < maxRetries; i++){try{string content = File.ReadAllText(path, Encoding.UTF8);if (string.IsNullOrWhiteSpace(content))throw new InvalidDataException("JS文件内容为空");return content;}catch (IOException) when (i < maxRetries - 1){Thread.Sleep(FileReadRetryDelay * (i + 1));}}throw new IOException($"读取JS文件失败,已重试{maxRetries}次");}private void LogInfo(string message) =>Debug.WriteLine($"[{DateTime.UtcNow:u}] [SM2] [INFO] {message}");private void LogError(string message, Exception ex = null) =>Debug.WriteLine($"[{DateTime.UtcNow:u}] [SM2] [ERROR] {message}{(ex != null ? $"\n{ex}" : "")}");
}

注意:
1.先使用_jsEngine.Execute(jsCode);把JS脚本加载进入v8引擎
2.使用_jsEngine.Script.window.SM2.sm2Encrypt(plainText);进行调用,其中window.SM2.sm2Encrypt(plainText)这部分是我JS中的方法,我挂载在window上面。_jsEngine.Script这部分就是js环境,理解成浏览器的控制台,你后半部分就像你平时浏览器控制台中调用网页中的方法一样操作。

http://www.dtcms.com/wzjs/221236.html

相关文章:

  • 汕头网站建设公司免费发布广告的平台
  • 网站建设 推广找山东博达热点营销案例
  • 新网站上线怎么做seo小说推文万能关键词
  • 视频网站logo怎么做怎么让百度收录自己的网站
  • 平面设计与网页设计培训seo优化交流
  • 网站静态页seo网站优化服务合同
  • 做编程网站有哪些内容注册网站怎么注册
  • 网站开发工程师ppt引流客户的最快方法是什么
  • 可信网站认证logo网络推广计划制定步骤
  • 公司网站如何做水印免费seo免费培训
  • 天猫网站平面广告中国重大新闻
  • 小米手机商城网址seo关键词
  • python网站开发 django十大跨境电商erp排名
  • 公司网站手机版入门seo技术教程
  • 南京做机床的公司网站seo顾问能赚钱吗
  • wordpress 百度 插件seo网络推广软件
  • 网站设计 职业湖南做网站的公司
  • 山东网站建设公司电话最好的推广平台排名
  • 网站开发+自动生成缩略图建站
  • wordpress ip地址只能访问首页百度快速优化软件排名
  • 宁波网站搜索优化今日新闻头条热点
  • 企业注册资金变更流程windows7优化大师官方下载
  • 广州网站建设公需要优化的网站有哪些
  • 翻译公司网站建设多少钱百度查询关键词排名工具
  • 好的建筑设计网站惠州百度推广优化排名
  • 网站建设生产或运营bt磁力搜索
  • 申请免费网站公司最好的免费推广平台
  • 互联网站管理工作细则挖掘关键词工具
  • 鄂州网站建设价格汕头网站排名
  • wordpress古腾堡编辑器站长之家seo信息