Skip to main content
Enterprise Agile Planning Image

This post is from the CollabNet VersionOne blog and has not been updated since the original publish date.

Last Updated Apr 11, 2014 — Enterprise Agile Planning expert

Detecting and loading a non-redistributable dll at run time

Enterprise Agile Planning

Scenario: We have a solution that depends on different versions of a non-redistributable dll, so until now we have created building rules that we use when we want to build against this dll. We have a folder per dll  and we have to pick the correct one for the environment that we want to setup. Question: Can we figure out what is the correct version of the dll installed in the environment and load it programmatically? Yes, we can do that! It’s a solution that isn't pretty but it gets the job done!

  • First of all, we've to declare explicitly what are the assemblies we support for the solution

[code lang="csharp"]supportedDlls = new Dictionary();
supportedDlls.Add(
"Microsoft.TeamFoundation.Client",
new string[]{
"Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}
);
[/code]

  • In the code where the dlls are used we subscribe to the event that is raised when an assembly cannot be found or loaded.

[code lang="csharp"]AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;[/code]

  • In the handler we try to load another instance of the assembly supported and if it fails we try with the other.

Note: in the handler we first have to unsubscribe from the event to prevent a loop and after try to load the dll subscribe again.

[code lang="csharp"]AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
Assembly assembly = resolveSupportedAssembly(supportedDlls[args.Name.Split(',').First()]);
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
return assembly;

static Assembly resolveSupportedAssembly(string[] versions)
{
Assembly assembly = null;
foreach (string version in versions){
try {
assembly = Assembly.Load(version);
return assembly;
}
catch{}
}
return assembly;
}
[/code]

Recap: So if the environment where you want to run the application has the same version of the dll that the solution compiled has it will work without needing to do anything else. In the case that the environment has another version of the dll installed (e.g a major version) , when the application runs, it will make use of the loop in the code trying to load the correct dll by itself.

Here is the link to the repository with this sample code: https://github.com/lremedi/ResolveDlls

More from the Blog

View more
Feb 14, 2021

Reflecting on the 20th anniversary of the Agile Manifesto

Enterprise Agile Planning
Over the past 20 years, it’s been amazing to watch an idea from ...
Read More
Feb 08, 2021

How does agile apply to an entire organization?

Enterprise Agile Planning
Before we dive into the main subject of this blog post, it is importan ...
Read More
Feb 03, 2021

It took a pandemic to realize why digital transformation actually matters

Enterprise Agile Planning
Before anyone had ever heard of COVID-19, businesses across the globe ...
Read More
Jan 27, 2021

Improve visibility, reduce costs, and take back control of your scaled out container and cloud deployments with the latest releases of Digital.ai Agility and DevOps solutions

Enterprise Agile Planning
We’re thrilled to announce the latest releases of our Digital.ai Agili ...
Read More
Contact Us