Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Custom Assemblies with CompileAssemblyFromSource

Tags:

c#

I'm not quite sure where to go with this. The overall objective is to be able to take a user script, and execute it within a .NET environment. I have most of the code written and things work provided I don't attempt to load my own assemblies. However, to safely give users access to internal portions of the system, a proxy DLL has been created. This is where the problem seems to be.

Right now this proxy DLL 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, it throws 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 file specified.

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

Is this the ScriptProxy.dll not loading properly, or is the ScriptProxy.dll itself trying to load up dependencies, which are in a temp file somewhere? Or is it something completely different?

I should mention, I'm executing this code from the NUnit test runner. I'm not sure if that makes a difference.

like image 323
T. Stone Avatar asked May 01 '12 19:05

T. Stone


2 Answers

This is because the compilation step failed and you have not check it 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;
    }
like image 149
csharptest.net Avatar answered Nov 11 '22 03:11

csharptest.net


I don't think the post marked as answer is really an answer !!! ... However i found the answer here

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

It mean if you trying to add dll reference that are third party(sometime .net dlls also giving exceptions) then simply copy it in executable folder .. it will work fine .. else you can also define full path there..

like image 4
Moumit Avatar answered Nov 11 '22 02:11

Moumit