title: Referencing library using Windows.winmd
body: <h2>What I'm trying to do</h2>
  
  <p>I'm trying to create a library (<code>Arduino.dll</code>) to interact with my Bluetooth device from a laptop (Windows 10). This library is intended to be used by a desktop application.
  To achieve that, I had to reference both <code>Windows.winmd</code> and <code>System.Runtime.WindowsRuntime</code>.</p>
  
  <h2>What's working</h2>
  
  <p>I did write the library and tested it on a console application which was on another project in the same solution. To make it work, I had to reference the project and <code>Windows.winmd</code> in the test project. Note that this test project is used to test all of my libraries, so it is heavily manipulated and I tinkered with the settings a lot for years.</p>
  
  <h2>What's not working</h2>
  
  <p>I finished to write my dll, and wanted to use it in my desktop application. Therefore, I referenced both <code>Arduino.dll</code> and <code>Windows.winmd</code> as in my test project, expecting to work the same way. Sadly, on running the application, I got a "dll not found" when instantiating a class for <code>Arduino.dll</code> : </p>
  
  <pre><code>An unhandled exception of type 'System.IO.FileNotFoundException' occurred in LuxAFX4.exe
  
  Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
  </code></pre>
  
  <p>Strange, but well, I tried to reference the (same) <code>System.Runtime.WindowsRuntime</code> as in <code>Arduino.dll</code> but no luck, I now got an even weirder error : </p>
  
  <pre><code>An unhandled exception of type 'System.BadImageFormatException' occurred in LuxAFX4.exe
  
  Additional information: Could not load file or assembly 'System.Runtime.WindowsRuntime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
  </code></pre>
  
  <p>During my search for a solution, I understood that I shouldn't try to load this "reference library" and if it is not found, it must be because it is not installed on the system. But then, why would it work in my test project? </p>
  
  <p>Another thing I may have understand, is that if my library doesn't expose members of referenced 'sub'-libraries, they won't be needed when referencing my library. Should I try to 'hide' all references? If so, is there a way to catch all exposed members? Source : <a href="https://stackoverflow.com/questions/23246749" title="How to to avoid referencing a dll&#39;s referenced dll">How to to avoid referencing a dll's referenced dll</a></p>
  
  <p>Side note : I use <code>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll</code> and <code>C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd</code>. </p>
  
  <p>May be related : <a href="https://stackoverflow.com/questions/32783102" title="Could not load file or assembly Windows.winmd">Could not load file or assembly Windows.winmd</a> and <a href="https://stackoverflow.com/questions/35439256" title="How to reference Windows.winmd from a .NET Core library?">How to reference Windows.winmd from a .NET Core library?</a></p>