简体   繁体   English

DLL 参考仅在 NuGet 在服务器上还原后有效

[英]DLL Reference Only Works After NuGet Restore on Server

The title could probably be better, but I can't really come up with something better right now.标题可能会更好,但我现在真的想不出更好的东西。 I'm running into a very strange problem between two of my servers: Server1 and Server2.我在两台服务器之间遇到了一个非常奇怪的问题:Server1 和 Server2。

Both are Windows 2012 R2/IIS 8.5.两者都是 Windows 2012 R2/IIS 8.5。 Server2 was cloned from Server1 when they were built and have been on the same patch cycle since. Server2 在构建时从 Server1 克隆而来,并且从那以后一直处于相同的补丁周期。

I recently deployed a major code change to Server1 which included the addition of a new project to my solution.我最近对 Server1 部署了一个主要的代码更改,其中包括向我的解决方案添加一个新项目。 The majority of the solution targets .NET Framework 4.6 (with some older bits here and there, but nothing all that important) but the new project targets .NET Standard 1.3 in the properties.大多数解决方案的目标是 .NET Framework 4.6(这里和那里有一些较旧的位,但没有什么那么重要),但新项目的目标是 .NET 属性中的标准 1.3。 This new project has all of its references listed as NuGet packages.这个新项目的所有引用都列为 NuGet 包。

When I compile this entire solution on my local computer and run it, everything works fine.当我在本地计算机上编译并运行整个解决方案时,一切正常。 When I deployed it to Server1, however, I began to get the following error any time the code got to a point of referencing the .NET Standard 1.3 project's code.但是,当我将它部署到 Server1 时,只要代码到达引用 .NET Standard 1.3 项目代码的位置,我就会开始收到以下错误。

Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

After trying everything I could think of and find online to resolve this, I eventually was able to get this to run properly by installing MSBuild onto the machine and running the msbuild -t:restore command.在尝试了我能想到的所有方法并在网上找到解决此问题的方法后,我最终能够通过在机器上安装 MSBuild 并运行msbuild -t:restore命令使其正常运行。 Once that was run directly on the server, everything began working.一旦它直接在服务器上运行,一切就开始工作了。

I thought maybe the deployment process was missing a step, so when I moved all the code over to Server2, I literally moved the entire codebase as-is from Server1 to Server2.我认为部署过程可能缺少一个步骤,所以当我将所有代码移至 Server2 时,我实际上将整个代码库按原样从 Server1 移至 Server2。 I didn't even rely on my own deployment process in fear that it was the cause of the problem.我什至没有依赖自己的部署过程,因为担心这是问题的原因。

Sure enough, once running the code on Server2, I saw the exact same error again.果然,一旦在 Server2 上运行代码,我又看到了完全相同的错误。 At this point the only thing I haven't done on Server2 is run that command and while it may fix it for these dev machines, I'm not particularly eager to install a program like MSBuild onto our production servers to fix this there.在这一点上,我在 Server2 上唯一没有做的事情就是运行该命令,虽然它可能会为这些开发机器修复它,但我并不是特别渴望在我们的生产服务器上安装像 MSBuild 这样的程序来修复这个问题。

So now my question: Does anyone know if there's something I'm missing here?所以现在我的问题是:有谁知道我在这里是否遗漏了什么? I admit, I don't wholly understand NuGet so I'm not sure why the msbuild -t:restore command fixes this.我承认,我并不完全理解 NuGet,所以我不确定为什么msbuild -t:restore命令可以解决这个问题。 Is there some way to replicate what this command does on a file-structure or config level that can be incorporated into a deployment process?有没有什么方法可以复制此命令在可以合并到部署过程中的文件结构或配置级别上执行的操作?

Or... should I just stop worrying about putting MSBuild on a production server and make the on-server build activity part of our regular deployment process moving forward?或者...我是否应该不再担心将 MSBuild 放在生产服务器上,并让服务器上的构建活动成为我们常规部署过程的一部分?

PLEASE NOTE: I do not recommend the solution below to anyone who runs into this problem with an application that is meant to exist for a long time with continuous enhancements.请注意:我不向任何遇到此问题的人推荐以下解决方案,该应用程序本应长期存在并不断增强。 The only reason this was a viable option for us was because the main application will be reaching EOL soon and the codebase is not expecting to go through any more major enhancements or integrations before it is decommed completely.这对我们来说是一个可行的选择的唯一原因是因为主要应用程序将很快达到 EOL,并且代码库不希望在完全取消之前通过任何更多的主要增强或集成来实现 go。

It turns out the solution was to add NuGet to our core application (which never actually used NuGet and only relied on manual references) and then force the core application to downgrade its own System.Net.Http and System.Diagnostics.DiagnosticSource packages to the packages used by the .NET Standard 1.3 targeted project.事实证明,解决方案是将 NuGet 添加到我们的核心应用程序(它从未真正使用过 NuGet 并且仅依赖于手动引用),然后强制核心应用程序将其自己的 System.Net.Http 和 System.Diagnostics.DiagnosticSource 包降级为.NET 标准 1.3 目标项目使用的包。 I wasn't particularly happy about this approach, because it spells quite a bit of issues for us moving forward if we ever need to upgrade anything, but for the purposes of the current project, it'll work.我对这种方法不是特别满意,因为如果我们需要升级任何东西,它会为我们前进带来很多问题,但就当前项目而言,它会起作用。

I'm still a little confused about why using a Web.Config binding never worked for this.我仍然对为什么使用 Web.Config 绑定对此不起作用感到有点困惑。 It was my understanding that if I used the code below, I wouldn't have to worry about downgrading or upgrading library references, and that .NET would handle the pointing of refence calls based on the binding settings shown.据我了解,如果我使用下面的代码,我就不必担心降级或升级库引用,并且 .NET 将根据显示的绑定设置处理引用调用的指向。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
                <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

We even tried using a newVersion of 4.1.1.3 and 4.1.1.0 (with the appropriate DLL changes), but neither of them worked.我们甚至尝试使用 4.1.1.3 和 4.1.1.0 的新版本(具有适当的 DLL 更改),但它们都不起作用。 The potential reality that the application was just completely ignoring this line in the config is a whole other concern for us, but luckily, as the note above states, we won't have to worry about this application much longer.应用程序完全忽略配置中的这一行的潜在现实是我们关心的另一个问题,但幸运的是,正如上面的注释所述,我们不必再担心这个应用程序了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM