For testing I used a Console application with the following source code:
public string CODEInString = @"namespace MyNamespace.Generator
{
public class Calculator
{
public int Sum(int a, int b)
{
return a + b;
}
}
}";
public void Create()
{
var provider = new CSharpCodeProvider();
var cp = new CompilerParameters
{
GenerateInMemory = false,
OutputAssembly = "AutoGen.dll"
};
provider.CompileAssemblyFromSource(cp, CODEInString);
}
With this code inside a console application I can make it work and the AutoGen.dll file is created, from that point I can invoque the calculator’s methods.
My problem happens when I do the same code but inside a MVC 3 application. I can catch the exception if I use the following variable.
var compileResult1 = provider.CompileAssemblyFromSource(cp, CODEInString);
‘compileResult1.CompiledAssembly’ threw an exception of type System.IO.FileNotFoundException’
I also tried to use Server.MapPath(“~/bin/”) to tell the output directory.
Someone could help me here?
Thank you
UPDATE 1
I gave folder’s permissions to the correct user in order to write, so that is not the problem.
How exactly did you try that because the following works for me:
And here’s my full test code:
Just wanted to point out that before doing this I hope you are fully aware that by writing to the
binfolder you are basically killing and unloading the AppDomain of your web application every time you run this code. So if you really want to execute some dynamic code you might consider compiling the assembly in-memory.