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.
After build, you might get this kind of error:
Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=126.96.36.1996, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
System.IO.FileNotFoundException at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.Assembly.Load(AssemblyName assemblyRef) at Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformPlugin.LoadPlatformSpecificAssembly()
In my case, I had referenced both of these DLLs in my “class library project”, which provided my console program a lot of functionality:
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 should not be required, but alas.
private static void MethodCallToForceLoadingOfPlatformDlls()
var creds = new UserPasswordCredential("MockAccount", "ImportantPassword");
// hack to include the dll in referencing projects
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
Latest posts by Antti K. Koskela (see all)
- 4 ways to fix error AADSTS65001 (The user or administrator has not consented to use the application) - November 20, 2017
- How to use the Azure AD associated with your SharePoint Online - November 3, 2017
- New version of Microsoft.IdentityModel.Clients.ActiveDirectory (ADAL.NET) is out – good time to update! - October 27, 2017