我正在使用 CSharpCodeProvider 在运行时编译 DLL。我的代码在某些机器上运行良好,但在其他机器上则失败并出现以下错误:
error CS0006: Metadata file 'EntityFramework.dll' could not be found
这是一个代码片段:
var csFile = ... // the file is in C:\Program Data\MyFolder\InnerFolder
using (var provider = new CSharpCodeProvider())
{
var parameters = new CompilerParameters
{
GenerateInMemory = false, // we want the dll saved to disk
GenerateExecutable = false,
CompilerOptions = "/target:library",
// the assembly is compiled to the same directory as the .cs file
OutputAssembly = GetNewCacheAssemblyPath(),
};
parameters.ReferencedAssemblies.AddRange(new[]
{
"System.dll",
"System.Data.dll",
"System.Data.Entity.dll",
"EntityFramework.dll",
});
var compilerResult = provider.CompileAssemblyFromFile(parameters, csFile);
}
对于为什么会发生这种情况有什么想法吗?
最佳答案
EntityFramework 不是 .NET 框架的一部分。所以一个简单的解释是,发生故障的机器没有安装它。您应该自己部署它。当您使用 Nuget 包时,您将在 bin\Release 目录中拥有该 DLL 的副本。不要忘记将其包含在您的交付二进制文件中。
下一个失败模式是您确实部署了它,但程序的工作目录不在您希望的位置。提供程序集的完整路径名以避免这种情况。例如,您可以使用 Assembly.GetEntryAssembly().Location 来查找 EXE 的路径。
关于c# - CSharpProvider 运行时编译找不到 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13235583/