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

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

问题描述

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

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.

因此,我后来发现,如果我将nuget中的System.IO.Ports安装到.Net Framework Project中,该异常将消失.但是该库已经安装到netstandard项目上.

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.

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

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.

我必须缺少一些东西,熟悉netstandard的任何人都可以给我一些见解.

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

非常感谢您的投入!

推荐答案

为托管.Net标准库的.Net Framework项目打开.csproj文件,并将此行添加到第一个<PropertyGroup>内,如下所示:更改还原样式:

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>

请参阅Scott Hanselman撰写的文章修复.

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

摘自文章:

完整"框架项目使用的是较旧的.csproj格式,默认情况下,它们使用package.config来管理依赖项.较新的项目可以将Packages作为一流的参考.因此,我们需要告诉该解决方案中的所有项目,以"PackageReferences"的身份管理和还原其软件包.

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."

此修复程序对我在新项目中的工作非常理想.当我在该新项目中还原软件包之前将其应用于csproj时,nuget将获得正确的引用.

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.

将其应用于现有项目时,将无法使用. .net标准类库中引用的库不会被nuget拉取,因此,它仍然会引发相同的错误.

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. 打开csproj.您会发现您的图书馆看起来像这样

<Reference Include="yournetstandardlibrary, Version=1.0.1.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\yournetstandardlibrary\lib\netstandard2.0\.dll</HintPath>
    </Reference>

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

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

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

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