编译 .NET5 项目时缺少 Microsoft 库 [英] Microsoft libraries missing when compiling .NET5 project

查看:26
本文介绍了编译 .NET5 项目时缺少 Microsoft 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反编译 ASP.NET Core WebApi 项目并从所有控制器收集所有方法.当我的项目针对 .NETCore3.1 时,它通过运行以下代码工作:

Assembly assembly = Assembly.LoadFrom(assemblyPath);//assemblyPath 指向 .dllvar types = assembly.GetExportedTypes();

但是在更新到 .NET5 之后,上面的第二行 (assembly.GetExportedTypes()) 会引发文件 Microsoft.AspNetCore.Mvc 的异常.Core.dll 丢失.当我从旧项目(编译为 .NETCore3.1)手动复制该文件时,它起作用了!

最重要的是,当项目更复杂时,有 EFCore 依赖等等......在 .NET5 下编译项目时会丢失更多文件.它们是:

  • Microsoft.Extensions.Hosting.Abstractions.dll
  • Microsoft.AspNetCore.Mvc.Abstractions.dll
  • Microsoft.AspNetCore.Authentication.Abstractions.dll

我有两个问题:

  • 为什么这些文件没有复制到输出文件夹?
  • 如何正确读取已编译 ASP.NET WebApi 二进制文件的控制器中的所有端点/方法?我做错了什么吗?

重现步骤:

  • 创建面向 .NET5ASP.NET Core WebApi 项目.
  • 创建其他面向 .NET5 的项目并实现这两行:
<块引用>

var assemblyPath = "C:\Projects\Other\DotNet5Test\DotNet5Test\DotNet5Test.WebApi\bin\Debug\net5.0\DotNet5Test.WebApi.dll";装配装配 = Assembly.LoadFrom(assemblyPath);var types = assembly.GetExportedTypes();

  • 运行它

我尝试从 nuget 添加 Microsoft.AspNetCore.Mvc.Core,但是 Microsoft.AspNetCore.Mvc.Core.dll 没有被添加到输出文件夹>

解决方案

我认为问题在于 Microsoft.AspNetCore.Mvc.Core.dll 作为依赖于框架的部署 (FDD) 而不是独立部署的一部分提供(SCD) 查看已接受的答案 是否有任何 GAC 等效于 .NET Core?.

鉴于我之前看到你的这个问题,如何检索来自 WebApi 二进制文件的控制器方法? 我有一种感觉,您正在尝试从面向 .net 5 的 wpf 应用程序执行代码,但该应用程序无法使用 FDD 来解析程序集.

答案可能是您需要更改您的 webapi 应用程序以使用 SCD 部署模型,但没有将 SCD 用于网络应用程序也许 google 会有所帮助.

I'm trying to decompile ASP.NET Core WebApi project and gather all methods from all controllers. When my project targeted .NETCore3.1 it worked by running this code:

Assembly assembly = Assembly.LoadFrom(assemblyPath); // assemblyPath pointed to .dll
var types = assembly.GetExportedTypes();

But after updating to .NET5, the second line (assembly.GetExportedTypes()) from above throws an exception that file Microsoft.AspNetCore.Mvc.Core.dll is missing. When I copied that file manually from an old project (compiled as .NETCore3.1), it worked!

On the top of that, when project is more complicated, has EFCore dependency and more... more files are missing when compiling the project under .NET5. These are:

  • Microsoft.Extensions.Hosting.Abstractions.dll
  • Microsoft.AspNetCore.Mvc.Abstractions.dll
  • Microsoft.AspNetCore.Authentication.Abstractions.dll

I have two questions:

  • Why these files are not copied to the output folder?
  • How can I properly read all endpoints/methods in Controllers having complied binaries of the ASP.NET WebApi? Am I doing something wrong?

Steps to reproduce:

  • Create ASP.NET Core WebApi project targeting .NET5.
  • Create other project that targets .NET5 and implement these two lines:

var assemblyPath = "C:\Projects\Other\DotNet5Test\DotNet5Test\DotNet5Test.WebApi\bin\Debug\net5.0\DotNet5Test.WebApi.dll";
Assembly assembly = Assembly.LoadFrom(assemblyPath); 
var types = assembly.GetExportedTypes();

  • Run it

EDIT:

I tried adding Microsoft.AspNetCore.Mvc.Core from nuget, but Microsoft.AspNetCore.Mvc.Core.dll is not being added to the output folder

解决方案

I think the issue is that Microsoft.AspNetCore.Mvc.Core.dll ships as part of the framework-dependent deployment (FDD) rather than self-contained deployment (SCD) see the accepted answer Is there any GAC equivalent for .NET Core?.

Given I saw this question of yours posted earlier, How to retrieve Controller methods from WebApi binaries? I have a feeling you are trying to execute the code from a wpf application targeting .net 5 but the app is not able to use FDD to resolve the assembly.

The answer may be that you need to change your webapi app to use an SCD deployment model, but not having used SCD for web apps perhaps google will help.

这篇关于编译 .NET5 项目时缺少 Microsoft 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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