c# - CS-Script 持久缓存可在加载脚本时提供更好的性能

标签 c# caching scripting persistence

有没有办法在后续应用程序运行之间保留 CS-Script 内部程序集缓存?

使用的组件:http://www.csscript.net/

期望的行为是: 当我从脚本字符串编译程序集并关闭应用程序时,下次运行应用程序时,会找到具有匹配脚本字符串的已编译程序集,并且不需要重新编译。

这个问题是另一个问题的后续问题: Is there a way to call C# script files with better performance results?

这是我的代码,但每次重新启动父 .NET 应用程序时,每个脚本字符串都需要重新编译。

public interface ICalculateScript
{
    Exception Calculate(QSift qsift, QSExamParams exam);
}

...
void Calculate(string script)
{
    CSScript.CacheEnabled = true; 
    //Can following command use built-in cache to load assembly, compiled by this line of code, but by another instance of this app which run in the past and has been meanwhile closed? 
    Assembly assembly = = CSScript.LoadCode(script, null);

    AsmHelper asmHelper = new AsmHelper(assembly);
    ICalculateScript calcScript = (ICalculateScript)asmHelper.CreateObject("Script");
    calcScript.Calculate(this, exam);
}

相关问题: CS 脚本 C:\Users\vdohnal\AppData\Local\Temp\CSSCRIPT\Cache\2015108000 中的缓存创建的临时脚本文件夹有 41 MB,并且随着几个月前的文件而不断增长。

在WPF应用程序的输出窗口中,有第一次机会异常: mscorlib.dll 中发生了“System.IO.FileLoadException”类型的第一次机会异常 mscorlib.dll 中发生了“System.IO.FileLoadException”类型的第一次机会异常 mscorlib.dll 中发生了“System.IO.FileLoadException”类型的第一次机会异常 mscorlib.dll 中发生了“System.IO.FileLoadException”类型的第一次机会异常 mscorlib.dll 中发生了“System.IO.FileLoadException”类型的第一次机会异常 'ESClient.vshost.exe'(托管(v4.0.30319)):已加载'C:\Users\vdohnal\AppData\Local\Temp\CSSCRIPT\Cache\2015108000\af621e10-d711-40d7-9b77-0a8e7de28831.tmp.compiled ' C:\Users\vdohnal\AppData\Local\Temp\CSSCRIPT\Cache\2015108000

最佳答案

我从 Oleg Shilo 那里得到了答案,它为我指明了正确的方向:

The cache folder indeed grows as new scripts are compiled/loaded. This is the nature of the caching. It seems that it "grows without control" though it is not. Once cache is created for a given script file it is never duplicated and the new cache update is always written over the existing one.

The problem in your case is that every time you load the file you give it a unique name thus you are creating a new unique cache. To fix it you need to start using the same name for the script file every time you load/execute it.

Alternatively you can completely take over the caching location and specify what ever cache name you want. It is that second parameter that you pass null for:

 Assembly assembly = CSScript.LoadCode(script, null);

我使用了以下代码:

if (assemblyFileName == null)
    assembly = CSScript.LoadCode(script, null); //In case there is no name specified - when my custom temp folder cannot be created etc.
else
    assembly = CSScript.LoadCode(script, assemblyFileName, false, null);  //Specify full path and file name with extension 

因此,我可以完全控制缓存的程序集名称和位置。 如果带有适当脚本的缓存程序集已经存在,我可以简单地加载它,而不是编译一个新的:

Assembly assembly = Assembly.LoadFrom(assemblyFileName);
AsmHelper asmHelper = new AsmHelper(assembly)

初始加载速度较好,且不存在缓存不可控制增长的情况。

关于c# - CS-Script 持久缓存可在加载脚本时提供更好的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189170/

相关文章:

ios - 为 com.apple.dt.instruments 设置缓存目录

c# - @RenderBody() 与 Canvas

c# - 如何将 IHttpActionResult(内部带有 JSON)转换为我可以使用 LINQ 处理的对象

c# - 在 Entity Framework 中设置

javascript - GZIP 压缩和缓存我的 JS 和 CSS 文件

python - memcached 中的 Scrapy http 缓存存储

linux - shell 脚本 :How to pass script's command-line arguments through to commands that it invokes?

bash - 递归复制文件夹,排除部分文件夹

shell - 列出所有不包含特定字符串的文件

c# - 如果 Powershell 条件结果为真,如何将阶段标记为不稳定?