Dynamic compilation fails

J

J.A.

Hi,

I'm trying to use the CSharpCompilerProvider to dynamically compile some
code. The test console app works fine but I get this error when using the
same code in an ASP.NET app:
error CS1619: Cannot create temporary file
'c:\WINDOWS\system32\CSCCB.tmp' -- Access is denied.

I attached the code I use to compile the dynamic code at the bottom. I tried
with and without specifying the TempFileCollection, I also tried to compile
from source and from file.

It seems there isn't much information available so I figured I would ask
here.

Any suggestions ?

J.
--

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();

string fn = Path.GetTempFileName();
using(StreamWriter sw = File.CreateText(fn))
{
sw.WriteLine(
@"namespace Foo
{
public class Bar
{
public int Test() { return 1; }
}
}");
}

parameters.OutputAssembly = "testAssembly";
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.IncludeDebugInformation = false;
parameters.ReferencedAssemblies.Add("System.dll");
parameters.TempFiles = new TempFileCollection(Path.GetTempPath());

CompilerResults cr = provider.CompileAssemblyFromFile(parameters, fn);

if(cr.Errors.Count > 0)
{
// Display compilation errors.
System.Diagnostics.Debug.WriteLine("Errors during compilation");
foreach(CompilerError ce in cr.Errors)
{
System.Diagnostics.Debug.WriteLine(ce.ToString());
}
}
 
G

Guest

I was having the same problems as you. I broke out Reflector to see what was
happening underneath. CompileAssemblyFromSource() doesn't use much of the
parameters, etc. CompileAssemblyFromFile() however does. So, what worked for
me was writing my to a temporary file, specify a TempFileCollection(tempPath,
false) for the parameters and setting the outputAssembly to point to the temp
area.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top