0

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

Adam Wojnar
  • 473
  • 6
  • 19
  • Never ever add dll's manually anymore - that's the reason we have package managers – riffnl Nov 04 '21 at 23:36
  • @riffnl I'm not sure what do you mean. This whole ticket was created because I don't want to add these dlls manually. I only pointed out that *When I copied that file manually it worked*. Unfortunately, when I add this package via nuget, dll is still not included in the output folder, that's my problem. – Adam Wojnar Nov 04 '21 at 23:44

2 Answers2

0

I've had similar issues sporadically with various projects in the past. The most reliable solution for me has been to install any problematic libraries through the nuget packet manager. Remove any dependency files you manually added before doing this.

  • I tried adding `Microsoft.AspNetCore.Mvc.Core` from nuget explicitly, but that didn't work. `Microsoft.AspNetCore.Mvc.Core.dll` wasn't added to the output – Adam Wojnar Nov 04 '21 at 22:16
0

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.

markkidduk
  • 13
  • 7
  • I would rather find a way too get these Controller methods from the binaries deployed in the standard (FDD) way then do SCD deployment. It's for a custom diagnostic tool I'm building for my API – Adam Wojnar Nov 05 '21 at 01:45
  • I understand but I’m guessing that either needs a Framework re-write, you try a support ticket for that, or a custom post build task that copies the missing assemblies over. I’m not a fan of either tbh. – markkidduk Nov 11 '21 at 19:01