Yoni Ziv
Yoni Ziv

Reputation: 176

How to load assemblies and invoke their methods without compiling in .NET Core

I have a .NET Core project that needs to receive a string which determines which authentication logic to use and then use it.

The authentication logic needs to be a DLL that sits in a directory and implements an interface. We should have the option to add more DLLs that implement the interface to the directory, without compiling the project.

.NET Framework has a perfect solution for that - MEF (Managed Extensibility Framework). Unfortunately, .NET Core only partially supports MEF (Doesn't support DirectoryCatalog, which is what allows scanning a directory and loading assemblies)

Is there a similar/better way to achieve this in .NET Core?

Upvotes: -1

Views: 661

Answers (2)

Vlad Radu
Vlad Radu

Reputation: 367

If anyone is stumbling on this, there is another way to add plugin capability to a .NET 5+ application.

This means using AssemblyLoadContext, which is better documented than MEF for .NET Core/.NET5+

Upvotes: 0

Yoni Ziv
Yoni Ziv

Reputation: 176

Since in .NET Framework I added the reference through the Assemblies tab, when I tried the same in .NET Core it wasn't there. Also I read a false/outdated article that claims it's not supported in .NET Core - I got the impression that it's not possible.

I figured I better look for a NuGet package that gives an alternative and found out you just had to install the NuGet for System.DirectoryCatalog.Composition and it works.

Here's the article:

https://www.tutorialspoint.com/dotnet_core/dotnet_core_managed_extensibility_framework.htm#:~:text=MEF%20is%20a%20library%20for,and%20is%20available%20wherever%20the%20.

Only System.Composition is ported, and System.ComponentModel.Composition is not available yet. This means, we don’t have the catalogs which can load types from assemblies in a directory.

Upvotes: 0

Related Questions