有没有办法在后续应用程序运行之间保留 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/