我有一个应用程序配置为在控制台上运行或作为服务运行(取决于我是手动运行 .exe 还是安装它)。
从命令行运行它效果很好......但是当我将其安装为服务时,我收到文件加载错误:
System.IO.FileLoadException: Could not load file or assembly 'Microsoft.EntityFrameworkCore.Relational, Version=2.2.0.0' ... the located assembly's manifest definition does not match the assembly reference.
需要这个包是因为我正在使用 EntityFrameworkCore 和 Pomelo 库来查询 MySQL 数据库。我已确保所有必需的 NuGet 包都包含在内并且是最新的。
我已经做了一些事情来纠正这个错误,老实说,我看不出我错过了什么......
首先,我的程序从一开始就运行此行,以确保服务从我期望的目录(而不是 system32)运行:
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
接下来,我尝试将特定包恢复到 v2.0.0.0 以匹配错误描述...但是,一旦我使用恢复的包构建它,其他包就会抛出错误,表明它们正在寻找更新版本 v2 .2.6.0
我还尝试删除生产计算机上对 v2.0.0.0 的所有引用...所以它甚至不会寻找它
我的 .csproj 文件仅引用最新版本:
<Reference Include="Microsoft.EntityFrameworkCore.Relational, Version=2.2.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.EntityFrameworkCore.Relational.2.2.6\lib\netstandard2.0\Microsoft.EntityFrameworkCore.Relational.dll</HintPath>
</Reference>
我的 app.config 文件中甚至有一个绑定(bind)重定向,它似乎被完全忽略了:
<dependentAssembly>
<assemblyIdentity name="Microsoft.EntityFrameworkCore.Relational" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.6.0" newVersion="2.2.6.0" />
</dependentAssembly>
现在我对可能丢失的内容感到非常困惑...如上所述,我仅在作为服务运行时出现文件丢失错误...
为什么我的应用程序在作为服务运行时找不到正确的程序集版本?我该如何解决这个问题?
最佳答案
关于c# - 将应用程序作为 Windows 服务运行时程序集文件加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57564997/