CodeDomProvider failed to resolve my assemblies in website Bin folder when dynamically compiling code

StackOverflow https://stackoverflow.com/questions/17221356

  •  01-06-2022
  •  | 
  •  

Question

All,

I have asp.net website and in my bin folder I have all neccessary assemblies that my dynamically compiled code references. I compile my code at run time using CodeDomProvider.

When I compile my expression with the following line

compiler.CompileAssemblyFromSource(expr);

I get an error: Metadata file 'NHibernate.dll' could not be found, although that assembly is in the bin folder.

NOTE: I have similar code that works in a windows app, if not identical.

Here's my code:

        CodeDomProvider compiler = _GetCompiler();
        CompilerParameters compilerParams = _GetCompilerParams();
        CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, expr);
        if (results.Errors.Count > 0)
            throw new InvalidOperationException("Failed to compile criteria expression for the following reason: " + _GetAllCompilerErrors(results.Errors));

private CodeDomProvider _GetCompiler()
    {
        Dictionary<string, string> providerOptions = new Dictionary<string, string>();
        providerOptions.Add("CompilerVersion", "v2.0");

        CodeDomProvider compiler = new CSharpCodeProvider(providerOptions);
        return compiler;
    }

    private CompilerParameters _GetCompilerParams()
    {
        CompilerParameters compilerParams = new CompilerParameters();
        compilerParams.GenerateInMemory = true;
        compilerParams.GenerateExecutable = false;
                    compilerParams.ReferencedAssemblies.AddRange(new string[] {
            "System.dll",


            "NHibernate.dll",
        });
        return compilerParams;
    }
Was it helpful?

Solution

I was also having the same problem just solve it by adding the explicitly the bin folder in path

instead of "NHibernate.dll" use like below :

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+ "\\NHibernate.dll")

it will now look for the "NHibernate.dll" in the bin directory the same worked for me

i Hope it will work for you also

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top