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)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay