无法在引用 .Net 标准库的已发布 .Net 4.6.1 项目中加载文件或程序集“System.ComponentModel.Annotations" [英] Could not load file or assembly 'System.ComponentModel.Annotations' in published .Net 4.6.1 project referencing .Net Standard library

查看:39
本文介绍了无法在引用 .Net 标准库的已发布 .Net 4.6.1 项目中加载文件或程序集“System.ComponentModel.Annotations"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:这不是重复

我尝试了很多方法来解决这个问题,包括:

将以下行添加到 WPF 项目的 .csproj:

PackageReference<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects><GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

在 App.config 中添加以下绑定重定向:

<从属程序集><assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a"culture="neutral"/><bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/></dependentAssembly></运行时>

在 WPF 项目中添加对 System.ComponentModel.Annotations NuGet 包的引用

这些都没有奏效.

请注意:问题不是在 Visual Studio 中构建和运行.我能够做到这一点而不会出错.尝试发布应用程序并随后运行已发布的应用程序时会出现此问题.

解决方案

Visual Studio 中存在一个与 ClickOnce 发布相关的错误,它似乎发布逻辑试图变得聪明并且有帮助地"排除了它认为是其中一部分的 DLL框架的.当然,在混合替换完整框架功能的 .NET Standard 包时,这会导致各种问题.

参考:

我没有亲自尝试过 GitHub 问题中建议的解决方法,但是 System.Net.Http.dll 有一个类似的问题,通过显式地将指向 NuGet 包中的 dll 的链接添加到项目文件(添加现有 > 显示所有文件 > 添加为链接)并设置始终复制".需要明确的是:我的意思是将 dll 添加为内容"——而不是添加对 dll 的引用.链接的 DLL 将始终复制到发布输出.

PLEASE NOTE: This is not a duplicate of Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.1.0.0. Unlike the issue linked, this issue occurs only after publishing the application. As you will see below, I have tried every solution proposed in that post with no success.

This issue occurs in a WPF .Net Framework 4.6.1 project which references a .Net Standard 2.0 library which itself references the System.ComponentModel.Annotations NuGet package.

The issue is reproduced in the following project: https://github.com/kaitlynbrown/DataAnnotationsError

To reproduce the error:

  1. Clone the repo linked above
  2. Clone the repo linked above
  3. Open the solution in visual studio
  4. In Visual Studio, Build -> Publish
  5. Click Finish
  6. Install and run the published application

You will see the following error:

I have tried a number of things to resolve this issue, including:

Adding the following lines to the WPF project's .csproj:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

Adding the following binding redirects in App.config:

<runtime>
  <dependentAssembly>
    <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
  </dependentAssembly>
</runtime>

Adding a reference to the System.ComponentModel.Annotations NuGet package in the WPF project

None of these things has worked.

PLEASE NOTE: The problem is not building and running within Visual Studio. I am able to do that without errors. The problem occurs when attempting to publish the application and subsequently running the published app.

解决方案

There's a bug in Visual Studio related to ClickOnce publishing where it seems like the publishing logic is trying to be clever and "helpfully" excludes DLLs that it thinks are part of the framework. Of course, this causes all sorts of problems when mixing .NET Standard packages that replace full framework functionality.

Reference:

I haven't personally tried the workaround suggested in the GitHub issue, but had a similar issue with System.Net.Http.dll that was resolved by explicitly adding a link to the dll from the NuGet package to the project file (Add Existing > show all files > Add As Link) and setting "Copy Always." To be clear: I mean adding the dll as "content" - not adding a reference to the dll. The linked DLL will always be copied to the publish output.

这篇关于无法在引用 .Net 标准库的已发布 .Net 4.6.1 项目中加载文件或程序集“System.ComponentModel.Annotations"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆