c# - CSharpProvider 运行时编译找不到 DLL

标签 c# entity-framework exception assembly-references csharpcodeprovider

我正在使用 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/

相关文章:

c# - C#/。NET-记录正在播放的当前音频

c# - 使用 Entity Framework 5 将特定 id 插入 MySQL 中的自动递增字段

java - 同时抛出两个异常

.net - 使用 Entity Framework 4.3 迁移而不依赖于实际数据库

c# - Linq 中的计算字段按总和分组

exception - kotlin协程,launch如何处理异常

C#卡路里计数器

c# - LINQ 根据对象行中的整数获取 10 个对象

c# - machine.config appSettings 为空

c# - 为什么我从 Clipboard 类收到 OutOfMemoryException 异常?