Precompiling to static assembly in .NET 4.0

17 views
Skip to first unread message

Kyle Beyer

unread,
Sep 9, 2010, 2:11:53 PM9/9/10
to Spark View Engine Dev
I've recently run into an issue where the first time my app loads it
dynamically compiles correctly ... but after the app pool recycles
isn't able to resolve all referenced assemblies. Quite similar to the
problem discussed here: http://groups.google.com/group/spark-dev/browse_thread/thread/dbee06a0d1b2766f.
However, none of the recommended solutions worked for me.

So, I decided to change my approach to precompile to a static assembly
using the approach demonstrated in the PrecompiledViews sample
project. This resulted in some different issues. The first one was
that when the installer ran it said it already loaded mscorlib. I was
able to resolve these issues with two steps:

1) Re-compiled spark targeting .net 4 and added the following code to
BatchCompiler.cs line 76
if (assembly.FullName.StartsWith("mscorlib")){ continue; }

2) Moved all assembly and namespace declarations out of web.config and
global.asax setup steps and into _global.spark

The good news is that a static assembly is being generated and my
application works very well when pre-loading it.

The bad news is that the installer is giving an error even though the
views assembly is created:
"Spark.Compiler.CompilerException: Dynamic view compilation failed.
EXEC : error CS0016: Could not write to output file
'.......SparkViews.dll' -- 'The process cannot access the file because
it is being used by another process. '"

I've tried turning off the app pool to ensure the assembly isn't being
used by the web app during compilation and still no luck.

Has anyone ran into this issue? Suggestions for resolution?

Asbjørn Ulsberg

unread,
Oct 13, 2010, 3:41:12 PM10/13/10
to Spark View Engine Dev, Kyle Beyer
Hi. As you can see by the following thread:

http://groups.google.com/group/spark-dev/browse_thread/thread/aa1f41b3f9973057

Getting precompilation to work is quite a bit of work, as you probably
have discovered. I've done the same changes to Spark as you have, which I
hope could be implemented in the core, so everyone that wanted to
precompile Spark views with .NET 4.0 wouldn't have to do it themselves. :)

I haven't gotten the error you're having, though, so I'm wondering if it
might be due to an error in one of the views, as the problem I ran into.
It might be something completely different, but it's worth looking into.
Do all the views render successfully when not precompiled?

The error says that the "SparkViews.dll" is locked. May it be locked by
another precompilation task trying to compile another set of views to the
same DLL file? How have you wired up this precompilation installer? As a
post-build process? How have you set up your installer file? Could you
provide us with some of the code?

As a sidenote, I would appreciate a much better precompilation process
than having to run an installer from the command line, as it's very
limiting in terms of dealing with error messages. Not being able to double
click in the error list to go to the line causing the problem is a great
source of frustration.

Being able to precompile Spark views into a DLL should be with less magic
and trouble, but I have too little knowledge in this area to device the
perfect solution. I do hope that someone with enough Spark Mojo can pull
out some magic dust and sprinkle it over this area of Spark, though, as I
think it's in dire need of some hardcore expert attention.


-Asbjørn

On Thu, 09 Sep 2010 20:11:53 +0200, Kyle Beyer <kyle....@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages