none
CompileAssemblyFromFile not creating the OutputAssembly DLL file, why????

    Question

  • Hi,

    When I run the last line in the code below, I get this error:
    Could not load file or assembly 'file:///C:\Temp\WebReferences\MySrv.dll' or one of its dependencies. The system cannot find the file specified.

    Can anyone see what is wrong here? 

    Thanks in advance!!


             // Create the C# provider
             CodeDomProvider aCSProvider=CodeDomProvider.CreateProvider("CSharp");
                     
             //input params for the compiler
             CompilerParameters compilerParams = new CompilerParameters();
             compilerParams.OutputAssembly = @"C:\Temp\WebReferences\MySrv.dll";
             compilerParams.ReferencedAssemblies.Add("System.dll");
             compilerParams.ReferencedAssemblies.Add("System.Diagnostics.dll");
             compilerParams.ReferencedAssemblies.Add("System.Web.Services.dll");
             compilerParams.ReferencedAssemblies.Add("System.ComponentModel.dll");
             compilerParams.ReferencedAssemblies.Add("System.Web.Services.Protocols.dll");
             compilerParams.ReferencedAssemblies.Add("System.Xml.Serialization.dll");
             compilerParams.GenerateExecutable = false;
             compilerParams.GenerateInMemory = false;
             compilerParams.CompilerOptions = "/optimize";

             // Run the compiler and build the assembly        
             CompilerResults aCompRes= aCSProvider.CompileAssemblyFromFile(compilerParams,                                                         @"C:\Temp\WebReferences\Reference.cs");

             Assembly aAssm= aCompRes.CompiledAssembly;


    Friday, April 04, 2008 9:15 PM

Answers

  • OK, here is the answer:

    Instead of using this:
     Assembly aAssm= aCompRes.CompiledAssembly;

    I used this, along with the above Ref changes also:
             Assembly asm = Assembly.LoadFrom(@"C:\Temp\WebReferences\MySrv.dll");

    And now it works great!!

    I don't know why CompiledAssembly is throwing a File error. But I'll just use LoadFrom which does the job nicely!

    Thanks for your replies regardless!
    Friday, April 04, 2008 10:27 PM

All replies

  • OK, the issue is that you have place the full path of those referenced assemblies. I thought the system would dynamically know that info.
    Friday, April 04, 2008 9:28 PM
  • Did you make sure that the CompilerResults does not have any errors in its Errors collection?

     

    Friday, April 04, 2008 9:34 PM
  • Yes, and the errors are these:
    {error CS0006: Metadata file 'System.Diagnostics.dll' could not be found}
    {error CS0006: Metadata file 'System.ComponentModel.dll' could not be found}
    {error CS0006: Metadata file 'System.Web.Services.Protocols.dll' could not be found}
    {error CS0006: Metadata file 'System.Xml.Serialization.dll' could not be found}


    But I don't think I should have to explicitly give the paths. This is a web app and this code will run on a server which will not necessarily have those dll's in the paths set here.


    Actually, when I change Ref's to this:
     compilerParams.ReferencedAssemblies.Add("System.dll");      
             compilerParams.ReferencedAssemblies.Add("System.Web.dll");       
             compilerParams.ReferencedAssemblies.Add("System.Web.Services.dll");
             compilerParams.ReferencedAssemblies.Add("System.Xml.dll");

    I get no errors in the error collection (as in CS0006) but still get the same File error as above: Could not load file or assembly 'file:///C:\Temp\WebReferences\MySrv.dll'

    Friday, April 04, 2008 10:04 PM
  • OK, here is the answer:

    Instead of using this:
     Assembly aAssm= aCompRes.CompiledAssembly;

    I used this, along with the above Ref changes also:
             Assembly asm = Assembly.LoadFrom(@"C:\Temp\WebReferences\MySrv.dll");

    And now it works great!!

    I don't know why CompiledAssembly is throwing a File error. But I'll just use LoadFrom which does the job nicely!

    Thanks for your replies regardless!
    Friday, April 04, 2008 10:27 PM
  • i got the same error

                // Create the compiler object
                JScriptCodeProvider provider = new JScriptCodeProvider();
                ICodeCompiler compiler = provider.CreateCompiler();

                CompilerParameters options = new CompilerParameters();
                options.GenerateInMemory = true;
               
                // Generating an "executable" just means that the code will have an entry point, rather than simply
                // being a collection of classes.
                options.GenerateExecutable = true;
                // Adding references is optional - mscorlib and Microsoft.JScript are automatically referenced
                options.ReferencedAssemblies.Add("System.Windows.Forms");
                CompilerResults results = compiler.CompileAssemblyFromSource(options, textBox1.Text);

                // Run the newly generated assembly's Main method
                Assembly assembly = results.CompiledAssembly; -> error message appears here
                MethodInfo entryPoint = assembly.EntryPoint;
                entryPoint.Invoke(null, new object[] { null } );

    it gives that error

    Could not load file or assembly 'file:///C:\Documents and Settings\Mehmet\Local Settings\Temp\yjxx3d_p.dll' or one of its dependencies. The system cannot find the file specified.

    Thanks in advance

    asp,asp.net,vb.net,c#,MsSql,Oracle
    Wednesday, October 01, 2008 8:41 AM