DEV Community

Dheeraj Awale
Dheeraj Awale

Posted on

Load assembly at runtime with .Net 6*

Back in the days of .Net framework it was straight forward to load assemblies at runtime using ‘System.Reflection’ library.
When platform independent .Net Core was introduced, it came with lot of new approaches and designs that broke conventional implementations. Loading runtime assemblies was one of them.

I spent good amount of time on figuring out a generic way of achieving this and implemented a reusable & simple open-source library. This library is tested on .Net 6, .Net 7 & .Net 8.
Library location: https://github.com/dheerajawale-svg/dynamic-dependency-loader
Nuget package: https://www.nuget.org/packages/DynamicDependencyLoader

Here is the explanation and reasoning of the library.

If you are using .Net core (3.1, 6, 8 etc.) for development, you must have come across *.deps.json.

There were reasons to get rid of the old ways to introduce *.deps.json.

  • Platform Independent deployment: New Self-contained deployment does not rely on any shared components to be present on the target system whether it is Linux or Windows or others.
  • Efficient resource management: This file not only describes your dependencies packages but also helps to solve the dependency hell.
  • Easier to manage: the deps.json file defines a list of dependencies that can be dynamically linked. Normally, this file is machine-generated, and can get really big and complicated for a real-world app. But it’s plaintext, so we can craft it with just an editor.

As mentioned above, .NET Core can be configured to dynamically load assemblies from multiple locations.
These locations include:

  • App base directory (in the same folder as the entry point application, no config required)
  • Package cache folders (NuGet restore cache or NuGet fallback folders)
  • An optimized package cache or runtime packages store
  • The servicing index (rarely used. For Windows Update purposes.)
  • Shared framework (configured via runtimeconfig.json)

Top comments (0)