A quick (and handy!) hack to force referenced assembly to getting copied to a project

Mock Function call to force loading an assembly - how elegant!

Visual Studio failing to copy a referenced assembly to a project in build might lead to surprising runtime errors. This post will explain one method of fixing these issues, using Microsoft.IdentityModel.Clients.ActiveDirectory as the example – as earlier versions of that assembly had this issue!


After build, you might get this kind of error:

In my case, I had referenced both of these DLLs in my “class library project”, which provided my console program a lot of functionality:

  • Microsoft.IdentityModel.Clients.ActiveDirectory.Platform
  • Microsoft.IdentityModel.Clients.ActiveDirectory

I was expecting the referenced project to be built alongside with my console program, and did not expect any issues with referenced libraries – but BOOM! Suddenly none of the calls that happened in the referencing project, and required the Platform dll, worked anymore. They’d throw errors like the one above.

After a while of googling, I found a solution – that might apply to some other situations, too!

The authors of the package apparently kind of broke this functionality in 3.13 version of the library (Microsoft.IdentityModel.Clients.ActiveDirectory), in August 2016. That’s understandable – Microsoft’s stack nowadays is quite a fast-moving beast, and hence things sometimes break. And since there’s a workaround, I guess the developer team wasn’t in a huge hurry to fix the issue…

The team will, however, fix the issue in the upcoming version 3.17.0. They’ll achieve this by combining the functionality of the Platform dll in the core dll, and hence eliminate the issue. Until then, there’s a hack we can use!

Solution: force copying the assembly during build

Let’s take a look into how we can fix the issue. It’s luckily quite easy!

In your “class library project” (the one that Visual Studio will treat as a referenced project), you need to call a method or a class from the assembly that failed to be copied earlier. In Microsoft.IdentityModel.Clients.ActiveDirectory -assembly’s case, add the following piece of code. Add it somewhere, where it execute for sure:

This “mock function” call – that actually does nothing, but creates an instance of a class defined in the referenced but originally not copied assembly – forces MSBuild to copy the assemblies even in the referenced project. Hence, the classes are suddenly accessible even during the runtime. This small hack worked at least for me 🙂

  • Conversation detailing the issue: https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/511 
The following two tabs change content below.

Antti K. Koskela

Solutions Architect / Escalations Engineer at Koskila / Norppandalotti Software / Valo Solutions
Antti Koskela is a proud digital native nomadic millenial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world. He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff.This is his personal professional (e.g. professional, but definitely personal) blog.

One thought on “A quick (and handy!) hack to force referenced assembly to getting copied to a project

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.