zhaopeiym / quartzui

基于Quartz.NET3.0的定时任务Web可视化管理。docker打包开箱即用、内置SQLite持久化、语言无关、业务代码零污染、支持 RESTful风格接口、傻瓜式配置
MIT License
1.52k stars 418 forks source link

支持运行c#脚本任务 #5

Closed autukill closed 6 years ago

autukill commented 6 years ago

执行用户上传的C#脚本。

可选方案我目前找到 dotnet-script。

但需要考虑产生的问题,比如隔离,安全。

autukill commented 6 years ago

找到另一个可行方案,当然也是基于 Roslyn,即时编译 c# 脚本成 dll, 输出 dll 到内存中,并执行.: http://www.tugberkugurlu.com/archive/compiling-c-sharp-code-into-memory-and-executing-it-with-roslyn

下面的代码片段基于教程进行了修改,我这边(.Net Core 2.1 Console Application)能正常运行: https://gist.github.com/autukill/21dceb9e84b2383c4317c2d467ce0909

autukill commented 6 years ago

.net core 没实现 AppDomain 中的几个重要方法,比如 CreateInstanceFromAndUnwrap 导致无法卸载因动态脚本产生的内存占用,个人测试发现编译并加载一次占用 35 mb

对应的解决方法: 个人使用 hangfire, 利用其分布式的特性,启动多个 console application 专门处理c#脚本作业, 并使用 PM2 这类软件的 max_memory_restart 实现自重启