Reputation: 8104
Is there a way to persist CS-Script internal assembly cache between subsequent application's runs?
Used component: http://www.csscript.net/
The desired behavior is: when I compile an assembly form a script string and I close the application, the next time I run the application the compiled assembly with matching script string is found and no recompilation is needed.
This question is follow-up of another question: Is there a way to call C# script files with better performance results?
Here is my code, but every script string requires recompilation with every restart of parent .NET application.
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);
}
Related problem: The folder of temp scripts created by Cache in CS Script C:\Users\vdohnal\AppData\Local\Temp\CSSCRIPT\Cache\2015108000 has 41 MB and growing with files few months old.
In the output window of WPF App there are first chance exceptions: A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll 'ESClient.vshost.exe' (Managed (v4.0.30319)): Loaded '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
Upvotes: 2
Views: 1558
Reputation: 8104
I got an answer fom Oleg Shilo which pointed me in the right direction:
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);
I used following code:
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
Thanks to this I have complete control over cached assembly name and location. If cached assembly with appropriate script already exists, I can simply load it instead of compiling a new one:
Assembly assembly = Assembly.LoadFrom(assemblyFileName);
AsmHelper asmHelper = new AsmHelper(assembly)
The speed of initial loading is better and there is no uncontrollably growing cache.
Upvotes: 3