A quick but unknown hack to force referenced assembly to getting copied to a project

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

Referenced assembly not getting copied to a project in build might lead to surprising runtime errors. This post will explain one method of fixing these issues.

Problem

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 – Microsofts 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’s referenced), you’ll need to add the following piece of code. It’ll need to be somewhere where it’ll get run:

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 🙂

  • Conversation detailing the issue: https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/511 
The following two tabs change content below.
A Software Developer working on SharePoint, Azure, Office 365 and a lot of other stuff. Writing this blog to document interesting things I've found out here and there.

One thought on “A quick but unknown hack to force referenced assembly to getting copied to a project

Leave a Reply

Your email address will not be published. Required fields are marked *