c# - 有没有办法强制使用 dll 版本?

标签 c# .net

有没有办法强制使用特定的 dll 版本?来自 app.config?

(背景故事)我们正在使用 SQL Compact 3.5,出于业务原因(是的,我知道)暂时无法迁移到 SQL Compact 3.5 SP1。我们的构建目录中有 System.Data.SqlServerCe 和所有非托管 dll,但如果安装了 SP1,应用程序将加载并使用 SP1 托管 dll(并且推而广之,我假设也是非托管 dll)。

pre-sp1 dll的版本号是3.5.0.0,sp1版本是3.5.1.0

我已经将对 System.Data.SqlServerCe 的引用设置为 CopyLocal = true 和 Specific Version = true,但它仍然使用 SP1 版本,即使 sp1 之前的版本在我们的构建目录中(假设它使用来自 GAC 的那个)。我已经尝试添加来自 GAC 的引用,以及手动进入文件系统并直接引用 dll。

(说明一下,客户正在为其他需要它的软件安装服务包,但即使安装了服务包,我们仍然需要运行 pre-sp1 版本)

有没有办法强制 .net 使用我们构建目录中的那个?

更新:

我在应用程序配置中放置了一个重写,例如 this question ,但运行程序集绑定(bind)日志查看器给我这个:

LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Projects\ConsoleApplication11\bin\Debug\ConsoleApplication11.exe.Config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Redirect found in application configuration file: 3.5.0.0 redirected to 3.5.0.0.
LOG: Publisher policy file is found at C:\Windows\assembly\GAC_MSIL\policy.3.5.System.Data.SqlServerCe\3.5.0.0__89845dcd8080cc91\publisher.config.
LOG: Publisher policy file redirect is found: 3.5.0.0 redirected to 3.5.1.0.
LOG: ProcessorArchitecture is locked to MSIL.
LOG: Post-policy reference: System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL
LOG: Found assembly by looking in the GAC.
LOG: Binding succeeds. Returns assembly from C:\Windows\assembly\GAC_MSIL\System.Data.SqlServerCe\3.5.1.0__89845dcd8080cc91\System.Data.SqlServerCe.dll.
LOG: Assembly is loaded in default load context.

看起来 GAC 中 3.5.1 程序集的配置覆盖了它。有什么方法可以从我的 app.config 强制解决这个问题?

最佳答案

我找到了解决方案。查看 Henk 的回答(here)中的链接以及融合日志的输出,似乎 3.5 SP1 安装程序安装了一个发布者策略文件,该文件强制加载 sp1 dll,即使在请求 pre-sp1 dll 时也是如此.

将此放在 app.config 中会告诉 .net 忽略发布者策略,并最终使用 3.5.0.0 版本:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" />
                <publisherPolicy apply="no"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

关于c# - 有没有办法强制使用 dll 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1165190/

相关文章:

c# - 确定一个方法是否调用另一个包含新语句的程序集中的方法,反之亦然

c# - WCF 发送大文件

c# - LINQ OrderBy 使用真正的 ASCII 排序忽略大小写

c# - 在.Net Winforms应用程序中共享图像列表

c# - 使用 jQuery 从 ASP.Net JSON 服务获取数据

javascript - 使用 Razor 设置 javascript 变量

c# - 是否可以使用 VS2008 在 C# 中自动连接事件?

c# - Console.Write 语法 : what does the format string "{0, -25}" mean

.net - `x <> Nothing` 与 `x IsNot Nothing`

.net - Unity 'GetAllInstances' 没有返回任何内容