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

刚做的网站怎么网页设计制作网站模板图片

刚做的网站怎么,网页设计制作网站模板图片,海口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/212789.html

相关文章:

  • 哪个网站可以做立体字的模板石家庄新闻
  • 沈阳做网站 智域如何找推广平台
  • 深圳wap网站建设公司seo网页优化平台
  • 重庆外贸网站建设公司排名电商线上推广渠道
  • 微信公众号运营策划方案什么建站程序最利于seo
  • 网站开发需要考什么证书b站网站推广
  • 重庆网站推广平台石家庄整站优化技术
  • 怎么做支付网站360竞价推广怎么做
  • 网站开发绩效指标爱站工具下载
  • 做哪些网站比较好的电商运营推广
  • 网站建设规划结构石家庄网站关键词推广
  • 胶州网站开发app开发平台
  • 淘宝客网站素材企排排官网
  • 手机上使用wordpressseo优化一般多少钱
  • 怎么给网站做关键词招商外包
  • 媒介平台seo的目的是什么
  • 纯文本网站连接湖南正规seo优化报价
  • 自己做网站卖东西可以sem招聘
  • 做曖免费网站免费网站建设模板
  • 外国人做的网站吗百度网站提交入口网址
  • 曲阜做网站的公司如何宣传自己的网站
  • 软件定制开发公司在哪里成都seo优化推广
  • 网站url可以在自己做吗网站排名靠前方法
  • 电商网站开发系列江东seo做关键词优化
  • 举报网站建设公司近期发生的新闻
  • 国外优秀建筑设计网站营销方式方案案例
  • 今日油价92号汽油价格表网站seo视频教程
  • 海城网站建设找文网客服联系方式
  • 推进纪委网站建设aso网站
  • 网站里的动效怎么做有什么推广软件