当 Net Framework 引用 Net Standard Library 时,无法加载文件或程序集 [英] Could not load file or assembly When Net Framework reference a Net Standard Library

查看:172
本文介绍了当 Net Framework 引用 Net Standard Library 时,无法加载文件或程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 netstandard 非常陌生,当我想运行具有引用 netstandard 库的 .Net Framework(控制台)的调试模式时,我刚刚遇到异常.

所以我后来发现如果我将 System.IO.Ports 从 nuget 安装到 .Net Framework 项目上,异常就会消失.但是这个库已经安装到 netstandard 项目中.

所以我非常怀疑,如果我使用另一个 .net 框架项目中的 netstandard 项目,我是否需要安装 netstandard 项目所需的所有库.

我一定遗漏了一些东西,任何熟悉 netstandard 的人都可以给我一些有关它的见解.

非常感谢您的意见!

解决方案

打开托管 .Net 标准库的 .Net Framework 项目的 .csproj 文件,并在第一个 中添加这一行; 像这样改变还原风格:

PackageReference

查看 Scott Hanselman 撰写的关于问题原因和修复的文章.

来自文章:

<块引用>

完整"框架项目使用旧的 .csproj 格式,默认情况下,它们使用 package.config 来管理依赖项.较新的项目可以引用包作为一流的引用.所以我们需要告诉这个解决方案中的所有项目将它们的包管理和恢复为PackageReferences".

在一个新项目中,这个修复对我来说非常有效.当我在恢复该新项目中的包之前将其应用于 csproj 时,nuget 会获得正确的引用.

将此应用于现有项目时,它不起作用..net 标准类库中引用的库不是由 nuget 拉取的,因此,它仍然会抛出相同的错误.

要修复它,试试这个:

  1. 打开 csproj.你会注意到你的图书馆看起来像那样

<块引用>

<HintPath>..packagesyournetstandardlibrarylib
etstandard2.0.dll</HintPath></参考>

  1. 删除该引用
  2. 改为添加此引用

<块引用>

 <PackageReference Include="yournetstandardlibrary"><版本>1.0.1</版本></PackageReference></项目组>

I am very new to netstandard and I just encountered exception when I want to run a debug mode a .Net Framework (console) which has reference to a netstandard library.

So I later figured out that the exception would be gone if I installed the System.IO.Ports from nuget onto the .Net Framework Project. But this library has already installed onto netstandard project.

So I highly doubt that if I need to install all the libraries which netstandard project required if I uses the netstandard project from another .net framework project.

I must be missing something, can anyone who is familiar with netstandard give me some insights about it.

your inputs are much appreciated !

解决方案

Open up the .csproj file for the .Net Framework project that is hosting the .Net standard library and add this line within the first <PropertyGroup> like this to change the restore style:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

Check out this article by Scott Hanselman about the cause of the issue and the fix.

From the article:

The "full" Framework projects are using the older .csproj format and by default, they use package.config to manage dependencies. The newer projects can reference Packages as first-class references. So we need to tell ALL projects in this solution to manage and restore their packages as "PackageReferences."

EDIT:

This fix worked perfectly for me on a new project. When I apply it to the csproj before I restore packages in that new project, nuget gets the correct references.

When applying this to an existing project, it doesn't work. The libraries that are referenced inside the .net standard class library are not pulled by nuget and therefore, it would still throw the same error.

To fix it try this:

  1. Open the csproj. You will notice your library looking like that

<Reference Include="yournetstandardlibrary, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..packagesyournetstandardlibrarylib
etstandard2.0.dll</HintPath>
    </Reference>

  1. Delete that reference
  2. Add this reference instead

 <ItemGroup>
     <PackageReference Include="yournetstandardlibrary">
       <Version>1.0.1</Version>
     </PackageReference>   
  </ItemGroup>

这篇关于当 Net Framework 引用 Net Standard Library 时,无法加载文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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