在 UWP 应用程序的完全信任 win32 组件中使用 .NET 标准库时抛出 FileNotFoundException(无法加载文件或程序集)

FileNotFoundException (Could not load file or assembly) thrown when using a .NET standard library in a full trust win32 component of UWP app

我在 .NET 标准库中有我的域逻辑。我正在尝试在 UWP 应用程序中定义的完全信任的 win 表单托盘组件中使用它。 UWP 项目和 win 表单项目都引用了我的域逻辑库。 现在,如果我在系统托盘中将 win 表单应用程序作为完全信任进程启动并调用域逻辑,它会抛出一个 FileNotFoundException 说它找不到添加到我的 'domain logic' 库中的 nuget 包。但是,如果我单独启动表单应用程序,它会正确执行。根据答案,我在 post 构建事件中添加了复制任务,并且复制的 dll 也包含在项目中。

这里有一个 sample repo 来重现上述问题。自述文件包含重现问题的简要说明。如何修复抛出的 FileNotFound?

这是异常跟踪。

System.TypeInitializationException: The type initializer for 'DIServiceProviderSingleton' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at DomainLogic.DIServiceProvider..ctor()
   at DomainLogic.DIServiceProvider.DIServiceProviderSingleton..cctor()
   --- End of inner exception stack trace ---
   at DomainLogic.DIServiceProvider.get_Instance()
   at SystrayComponent.SystrayApplicationContext.PerformDomainLogic(Object sender, EventArgs e)

最初 nuget 包 dll 没有被复制到 SystrayComponent 的 bin 文件夹中。我按照变通方法 posted here 修复了它。即使在那之后我也得到了提到的异常。

由于无法将调试器附加到 win32 对应程序,我已记录在应用程序沙箱本地状态文件中抛出的所有异常 C:\Users\YourUserName\AppData\Local\Packages\UWPwithSystrayextension_brppa21vfw7f4\LocalState\Log.txt

除了包含exe文件外,还需要包含DomainLogic.dll文件。

请在UWP-Systray-master\SystrayComponent\bin\Debug中找到DomainLogic.dll文件并将其添加到Win32文件夹中。

添加后"Include in the project"在Visual Studio.

谢谢