michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
409 stars 114 forks source link

"Could not load type 'Q42.HueApi.LocalHueClient'" when running with mono #75

Closed Amatsugu closed 8 years ago

Amatsugu commented 8 years ago

My application is a console application and everything works correctly when running on windows(10), however when I attempt to run the same build with mono I get the following error: Unhandled Exception: System.TypeInitializationException: The type initializer for 'com.LuminousVector.Karuta.Karuta' threw an exception. ---> System.TypeLoadException: Could not load type 'Q42.HueApi.LocalHueClient' from assembly 'Q42.HueApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. at com.LuminousVector.Karuta.Karuta.RegisterCommands () <0x40df24c0 + 0x00213> in <filename unknown>:0 at com.LuminousVector.Karuta.Karuta..cctor () <0x40dcd5f0 + 0x002ef> in <filename unknown>:0 --- End of inner exception stack trace --- at com.LuminousVector.Karuta.Program.Main () <0x40dccd50 + 0x00033> in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'com.LuminousVector.Karuta.Karuta' threw an exception. ---> System.TypeLoadException: Could not load type 'Q42.HueApi.LocalHueClient' from assembly 'Q42.HueApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. at com.LuminousVector.Karuta.Karuta.RegisterCommands () <0x40df24c0 + 0x00213> in <filename unknown>:0 at com.LuminousVector.Karuta.Karuta..cctor () <0x40dcd5f0 + 0x002ef> in <filename unknown>:0 --- End of inner exception stack trace --- at com.LuminousVector.Karuta.Program.Main () <0x40dccd50 + 0x00033> in <filename unknown>:0

The package was installed via NuGet and I've tried reinstalling, rebuilding as release/debug and nothing makes a difference, it doesn't load on mono. First I should ask if this project normally works correctly with mono and if so, is there a solution to this?

My project is .Net 4.6.1, Console Application, Any CPU

You can find my project here if there's anything you want to double check, and the class specifically that uses this project.

michielpost commented 8 years ago

I've seen a mono project that works with this library in the past. So I guess it can work. But I haven't tested it and I'm not sure everything works with mono. You can check out the source for Q42.HueApi, build it against mono and maybe figure out what's wrong?

spacepope commented 8 years ago

Hi there, i had the exact same problem and was able to solve this with the following steps:

  1. If you are using the api within a library or a subpackage, make sure that you install the Q42 nuget package in all those Projects and the main Project referencing the DLLs.
  2. I did update the Microsoft.Bcl.Build package to Version 1.0.21 (restart your IDE if requested..), but i am not sure if this step is necessary. You could first skip this step if you like and try later if it still fails..
  3. Important: you have to insert a binding redirection in your app.config file (the main app.config file for the application which is run by Mono!) I am targeting .Net 4.5, so maybe you have to adapt the following to 4.6, but try this first:
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  1. If all dependencies are correctly configured the dlls "System.Net.Http.Primitives.dll" and "System.Net.Http.Extensions.dll" should be copied in the Bin dir, but the other System.Net.Http.*.dll should NOT (These are loaded from the GAC).

I am successfully running the Api on a Raspberry 2 with mono 4.4.2.

Hope i could help... ;)

Amatsugu commented 8 years ago

Thank you so much, that worked!!! How did you figure out that this was the cause?

spacepope commented 8 years ago

Nice, glad i could help! :) I figured it out with a lot of pain and trial and error (setting MONO_LOG_LEVEL=debug and trying to understand whats going wrong), but at the end it was Visual Studio which helped me: there was a warning about different versions of System.Http.Net mapped and a hint to add a binding redirect. Luckily there was an option within the hint to automatically generate and insert this into app.config. Otherwise i wouldn't have known that.. I should have paid attention to that warning earlier on ;) Although i am still not knowing exactly WHY there is this version conflict and how i could have noticed this by myself..

Happy coding...

Amatsugu commented 8 years ago

Thanks, this might help me in the future if I encounter similar issues.