views:

220

answers:

2

My application has a scripting feature where I compile an in-memory assembly from the user's script using CodeDomProvider.CompileAssemblyFromSource. It's similar to what's described in this answer.

It works very well, but any exceptions that get thrown from the script code don't have line numbers in the stack trace. I tried setting the compilerParameters.IncludeDebugInformation = true, but it still doesn't include line numbers.

Is it possible to get line numbers on exceptions from an in-memory assembly?

Here are the key pieces of code I'm using to compile the assembly:

        CompilerParameters compilerParameters =
            compilerInfo.CreateDefaultCompilerParameters();
        compilerParameters.GenerateInMemory = true;
        compilerParameters.GenerateExecutable = false;
        compilerParameters.IncludeDebugInformation = true;

        ...

        CodeDomProvider codeProvider = compilerInfo.CreateProvider();
        CompilerResults compilerResults =
            codeProvider.CompileAssemblyFromSource(
                compilerParameters,
                new string[] { sourceCode });
+1  A: 

From comments I found here and here, it looks like the PDB file for the assembly has to be in the same directory as the assembly before the line numbers will be attached. It seems like this rules out debugging info for in-memory assemblies.

Don Kirkby
+2  A: 

We worked around this by writing the source out to a temporary file and then using that file to compile the code rather than an in-memory string. This provided us with meaningful debug information that we otherwise couldn't get.

Jeff Yates
I guess it's a trade off between the hassle of managing and cleaning up the temp files with the benefit of debug info.
Don Kirkby
@Don: Exactly. We tried to do it without temp files, but we tried in vane. However, the tidy-up was not working for us depending on when the files were released so, as we create the files in the user's temp directory, we left it predominantly for the user to clean-up.
Jeff Yates
Yep - that's what we did too.
ShuggyCoUk