Loading custom assemblies using CompileAssemblyFromSource

I'm not quite sure where to go with this. The overall goal is to be able to accept a user script and execute it in a .NET environment. I have written most of the code and everything works, I am not trying to load my own assemblies. However, in order to safely provide users with access to the internal parts of the system, a proxy DLL has been created. That's where the problem seems to be.

This proxy DLL currently has one thing in it, an interface.

CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.ReferencedAssemblies.Add("System.dll"); options.ReferencedAssemblies.Add("ScriptProxy.dll"); Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); CompilerResults result = provider.CompileAssemblyFromSource(options, script); // This line here throws the error: return result.CompiledAssembly; 

Running the above code causes the following error:

System.IO.FileNotFoundException: Could not load file or assembly 'file: /// C: \ Users ... \ AppData \ Local \ Temp \ scts5w5o.dll or one of its dependencies. The system cannot find the specified file.

Of course, my first thought is: "... and what is scts5w5o.dll?"

Is ScriptProxy.dll loading ScriptProxy.dll or is the Proxy.dll script trying to load dependencies located somewhere in a temporary file? Or is it completely different?

I should mention that I am executing this code from the NUnit test runner. I'm not sure if that matters.

+6
source share
2 answers

This is because the compilation step failed, and you did not check for errors ...

  static Assembly Compile(string script) { CompilerParameters options = new CompilerParameters(); options.GenerateExecutable = false; options.GenerateInMemory = true; options.ReferencedAssemblies.Add("System.dll"); options.ReferencedAssemblies.Add("ScriptProxy.dll"); Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); CompilerResults result = provider.CompileAssemblyFromSource(options, script); // Check the compiler results for errors StringWriter sw = new StringWriter(); foreach (CompilerError ce in result.Errors) { if (ce.IsWarning) continue; sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText); } // If there were errors, raise an exception... string errorText = sw.ToString(); if (errorText.Length > 0) throw new ApplicationException(errorText); return result.CompiledAssembly; } 
+7
source

I do not think that the message marked as answer is really the answer !!! ... However, I found the answer here

 parameters.ReferencedAssemblies.Add(typeof(<TYPE FROM DOMAIN.DLL>).Assembly.Location); 

This means that if you are trying to add a third-party dll link (sometimes a DLL that also provides exceptions), just copy it to the executable folder .. it will work fine .. otherwise you can also determine the full path is ..

+3
source

Source: https://habr.com/ru/post/914661/


All Articles