2

I have some problem with dynamic compile C# code. I have some provider property in my class:

 static CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });

and I have some code to compile my code

 var compile = provider.CompileAssemblyFromFile(compilerParameters, sourceFile);

Most offen this code just compile my code. But on one machine, when app run as Windows Service this method return exception:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
  at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
  at System.IO.File.InternalReadAllBytes(String path, Boolean checkHost)
  at System.IO.File.ReadAllBytes(String path)
  at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
  at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
  at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromFile(CompilerParameters options, String[] fileNames)

I Want to debug CompileAssemblyFromFile. This project has .net 4.6 targer so I download source form here https://referencesource.microsoft.com/download.html. Next I check Debug->Options->General->Enable .NET Framework source stepping and Suppress JIT optimization on module load (Managed only), next check in Debug->Options->Symbols->Microsoft Symbol Server, next click Load all symbols, and reboot VS.

When I debug my code and clikc F11 a go to enter image description here

next click F10

enter image description here

when I want to see compiler variable I type in Immediate Window

enter image description here

next when type F10 debug go out from CodeDOMProvider.cs to invoke of CompileAssemblyFromFile. I Want to debug all CompileAssemblyFromFile. enter image description here

How can I debug this code? This look like pdb from MS Symbol Server not match. Can you help me? Thanks for Yours time.

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
lgabryel
  • 109
  • 1
  • 9
  • No. I read this page and issue before I work. I apply every step from "How to debugg into .NET framework source code" I debug some lines from .NET but i cant'w watch variables and after few step next debug go outside debuging code. – lgabryel Sep 15 '20 at 10:02
  • Does this answer your question? [How to debug/break in codedom compiled code](https://stackoverflow.com/questions/875723/how-to-debug-break-in-codedom-compiled-code) – Sinatr Sep 15 '20 at 10:24
  • @Sinatr - no I want to debug CodeDOM - I can step into by F10, byt when I debug CodeDOM, debug go out from CodeDOM like I push continue. – lgabryel Sep 15 '20 at 10:31
  • 3
    You're trying to debug code that was compiled with Release build settings. Yes, that's not a pleasant experience. You can't generally inspect local variables, stepping behaves oddly with strange jumps and no stepping into methods. No workaround for that, there is no Debug build for the framework. Just keep in mind that it is *never* a bug in the framework, always a bug in your code. – Hans Passant Sep 16 '20 at 08:37
  • Instead of trying to debug the issue, use a try-catch block and log it. – Ian Kemp Sep 16 '20 at 09:36
  • Hans Passant - thank you. Now I uderstand why debuging framework is odd. But this is not true that, it is never bug in framework... In my life I see lot of bugs in frameworks and libraries. In 99% my code work perfect, but on one machines crash. I read source code of CSharpCodeProvicer. Provider check that source exists, then run in commandline csc.exe and pass args. When csc.exe not return error provider want to read dll, but it is no dll and wxception throw from provider, from Framework code! I think in this one machine csc.exe not work propertly in windows service session. – lgabryel Sep 16 '20 at 10:18
  • After reboot this machine, all works fine, and after few weaks something broken and csc.exe not work again in Windows Service session. – lgabryel Sep 16 '20 at 10:19
  • @HansPassant can I build .NET Framework in Debug mode, and overwrrite System.dll with debug build? – lgabryel Sep 16 '20 at 12:03
  • If you download .net framework from [this link](https://referencesource.microsoft.com/download.html) and want to build .net framework, you may need to notice this “This solution will not build because it is missing crucial components such as resources, XAML files, etc.” – LoLance Sep 17 '20 at 09:59

0 Answers0