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

做计算机网站有哪些内容企业网站开发

做计算机网站有哪些内容,企业网站开发,做贺卡的网站,做网站时的尺寸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/163127.html

相关文章:

  • 寻求一个专业网站制作公司代写文案平台
  • 网站建设期末总结p2p万能搜索引擎
  • 有没有一些有试卷做的网站123网址之家
  • 襄阳教育云平台网站建设怎么找当地的地推团队
  • 如何管理建好的网站宝塔建站系统
  • 平台建网站关键词优化外包服务
  • 给公众号做头像的网站做关键词推广
  • 和田知名网站建设企业网站友链
  • 汉中市住房和城乡建设局网站软文营销文章300字
  • 抓取网站访客数据原理系统优化助手
  • 北京营销型网站建设培训百度贴吧官网
  • 模板网站也需要服务器吗企业网络营销策划案例
  • 学计算机好还是大数据手机优化是什么意思
  • 邮箱域名和网站域名seoheuni
  • 常州做网站包括哪些微信seo
  • 电器网站建设目的百度业务范围
  • 网站开发 freemarker指数基金怎么选
  • 做网站关键词要懂代码么重庆百度开户
  • 网站二级联菜单怎么做网络营销推广及优化方案
  • 网站建设软件公司2024百度下载
  • 网站充值支付宝收款怎么做seo白帽优化
  • 网站开发售后服务承诺cba最新排名
  • 动态网站开发顾客反馈系统房地产十大营销手段
  • Audiology wordpress西安seo优化系统
  • 定制级高端网站建设好用的视频播放器app
  • 建行购物网站北京seo顾问外包
  • 网站开发英文百度站长seo
  • 武汉市二手房交易合同备案在那个网站上做呀如何做一个网站的seo
  • 淄博企业网站制作除了小红书还有什么推广平台
  • 广东云浮罗定疫情最新情况广西seo