Well, I'll have to top post because Google doesn't want to scroll all
the way down to the bottom. :o)
Cheers Oleg for your timely replies.
I don't want to store the scripts in the database at this point, but
I'll keep that in mind. There may be a lot of scripts :o).
But, I think I've worked this out, and it's not too complex. Here's
what I've done:
1) My Task class now stores
a) Script text
b) Assembly name, named after Task's TaskId (e.g., "Scripts\Task_<some
guid>.dll")
c) Sha1 hash of compiled assembly.
d) There is also a public property to get a Sha1 hash of the script
text, though this is not stored with the object.
2) When someone goes to edit a script in the GUI, they are presented
with the script text and three buttons:
a) Compile script (calls CSScript.CompileCode if needed, passed an
assembly name as above, checks hashes on script text from GUI vs
stored text and also checks hash on assembly if it exists; only
compiles if something doesn't match up, stores new assembly hash in in-
memory Task object on successful compile)
b) Execute script (compiles script if necessary, then executes using
"new AsmHelper(Assembly.LoadFile(asmFileName))"
c) Execute and Unload script (compiles if necessary, then executes via
"using (AsmHelper helper = new AsmHelper(asmFileName,
TaskId.ToString(), true))" )
For a project that I'm trying to organize and to some extent automate,
there may be upwards of 1K different scripts. I don't want to be
creating new assemblies every time a script is recompiled, and I don't
want to recompile scripts unnecessarily; rather I want to reuse a
specified filename for the script's assembly based on the TaskId and
only recompile if something changed.
This seems to be working well. I've only come across one real problem
at this point: If a user does "Execute script" (in the same app
domain), then they can't recompile the script until they close and
reopen the GUI app because the assembly filename is in effect hard
coded. Doing "Execute and Unload script" solves that of course. I'll
probably take the plain "Execute script" button out of the GUI.
One goal is to allow collaborative editing of the project that these
scripts will be used for; so I will need to revisit adding some change
counters or locking flags in the database to make sure no one steps on
anyone else.