✅ Loading types from an assembly fails because assemblies provided via project SDK cannot be loaded
I'm trying to load types from an assembly to analyze and execute them.
The project of which I want to load the assembly is an ASP.NET Core project and uses the project SDK
Microsoft.NET.Sdk.Web
.
Loading the built assembly with Assembly.LoadFile
/Assembly.LoadFrom
works fine.
But as soon as I try to get types from the assembly (e.g. with GetTypes()
) a System.Reflection.ReflectionTypeLoadException
is thrown because one of the assemblies (e.g. Microsoft.Extensions.Hosting.Abstractions
) cannot be located. It throws for multiple referenced assemblies, but all of these are included with the project SDK, yet don't appear in the output directory of the assembly unless I specifically publish as self-contained application, which is of no use here.
How do I load the types of my target assembly while successfully resolving the referenced SDK assemblies?
I could use the Types
property of the ReflectionTypeLoadException
, but I'd rather not get the exception at all.10 Replies
Thanks, that's already really helpful.
But I guess there's no other way to locate these SDK assemblies automatically?
I see, thanks for your help!
The project option still helps a lot as this is a local/private tool only for now
what happens if you delete the .deps.json file
So I tried this and the referenced SDK assemblies are still not in the output directory. To be exact, no additional assemblies are in the output directory
nothing different, the exception still occurs
just for context, I'm talking about these assemblies.
Those are missing in the project output directory, and I'd like to dynamically load the project from a different executable, but getting any type information fails as described in the original post
ok. the "from a different executable" is what makes it hard. if the host application does not reference ASP.NET Core then it will be very difficult for Assembly.Load to find those assemblies
if the thing you are trying to inspect has a runtimeconfig.json file next to it, you can use https://learn.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblydependencyresolver
then, whenever you need to load a dependent assembly, you can use the AssemblyDependencyResolver to resolve it to a path
if the host application does not reference ASP.NET Core then it will be very difficult for Assembly.Load to find those assembliesyeah that's exactly the problem. that's a greater pointer, I'll have a look at this tomorrow. The target project has a runtimeconfig, so this might work thanks for your help!
actually, it looks like AssemblyDependencyResolver specifically does not do this
hmm
i think the easiest workaroudn is to reference ASP.NET Core from the host application
you don't even have to use it but referencing it will add it to the list of frameworks that the loader will search for assemblies
the problem is that I don't really know the actual project SDK. referencing this might work for this case, but not for another
I don't really want to reference all the assemblies that are part of
Microsoft.AspNetCore.App
or Microsoft.NETCore.App
all that is needed is
<FrameworkReference Include="Microsoft.AspNetCore.App" />
then maybe deleting .deps.jsonalright, I'll give it a try tomorrow and research a bit more, thanks a lot for your help
Adding the framework reference(s) worked and is the best option for me so far, because no change to the target project needs to be done.
Thanks again for your help!