libzmq not found by clrzmq in Xamarin Studios/C# application - c#

I'm using Xamarin Studio on a Mac, with clrzmq included via NuGet.
clrzmq references on libzmq.dll. My app compiles fine, but when I try to run it, I get this:
Unhandled Exception:
System.DllNotFoundException: libzmq
at (wrapper managed-to-native) ZMQ.C:zmq_init (int)
at ZMQ.Context..ctor (Int32 io_threads) [0x00000] in <filename unknown>:0
at FeatureSpike.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
libzmq.dll is definitely there in the build target directory. Does anyone know why it's not being found?

you might want to try using netmq (https://github.com/zeromq/netmq), is a 100% C# port of zeromq and might be easier to use then calling pinvoke.

if you're still looking for a solution...
You can use clrzmq4 and add statically linked libzmq and libsodium as native libraries to your xamarin solution.
Ensure you use the very latest version of clrzmq4,we just got it running today...

Is libzmq.dll next to your executable in the build output directory ?
The clrzmq NuGet package includes a PowerShell install script to set the build action for the libzmq.dll so it is always copied to the output directory. If you added the NuGet package on the Mac then the PowerShell script will not be run. So you will need to set the build action manually.
libzmq.dll seems to be a native dll compiled for Windows so it will not work on the Mac. So it looks like you will have to compile clrzmq on the Mac.

Have you created a dllmap in your config?
<configuration>
<dllmap dll="libzmq.dll" target="/usr/local/Cellar/zeromq/4.0.4/lib/libzmq.dylib"/>
</configuration>

Related

C# mod dependency throws 'TypeLoadException' when instantiated?

Issue
When I try to use a 3rd-party library in my C# project, it almost always throws 'TypeLoadException' at me.
Details
The dependency I'm using in this case is a mongodb driver, though this issue replicates with other libraries such as Json.net and MySQL. I've tested with my own self-created small 'dependency' class and I was able to use this without it throwing an exception at me.
I'm able to build successfully, but when the .dll is loaded as a mod for a game, it logs out this rather unhelpful stack trace and fails to execute anything.
TypeLoadException: A type load exception has occurred.
at Test.Api.GameAwake () [0x00000] in <filename unknown>:0
at ModManager.GameAwake () [0x00000] in <filename unknown>:0
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
Logger:masterLogException(Exception)
Logger:Exception(Exception)
Log:Exception(Exception)
ModManager:GameAwake()
GameManager:Awake()
All that resides in 'Test.Api.GameAwake' is a simple connection handler taken from the mongodb documentation. There's nothing else going on in this project.
I can see that the assemblies do seem to load correctly, so I'm really out of ideas as to what to do next.
Could be a specific version dependency problem, or a missing assembly that you're not seeing.
I recommend checking a "dependency spy" program, such as http://ilspy.net/ and trying to load your assembly plugin to see what it's looking for.

HowTo: uPD with unity 5

First post. Let me know if I break any rules!
I would like to experiment with Pure Data and Unity 5. It seems uPD would be the best fit. I have followed the instructions on the github page and loaded the first sample scene. The unity console reports errors with the dll:
Failed to load 'Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll', expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386. You must recompile your plugin for 64 bit architecture.
So I cloned the main libpd repo and used the included batch file to build an x64 dll. I replaced the libpdcsharp.dll in the unity project and the console now reports:
DllNotFoundException: Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll
LibPDBinding.LibPD.ProcessArgs[Int32] (System.Int32[] args, System.String& debug) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:402)
LibPDBinding.LibPD.SendMessage[Int32] (System.String receiver, System.String message, System.Int32[] args) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:354)
(wrapper synchronized) LibPDBinding.LibPD:SendMessage (string,string,int[])
LibPDBinding.LibPD.ComputeAudio (Boolean state) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:238)
(wrapper synchronized) LibPDBinding.LibPD:ComputeAudio (bool)
LibPDBinding.LibPD.Release () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:207)
(wrapper synchronized) LibPDBinding.LibPD:Release ()
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:79)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)
I have also tried copying the libPDBinding.dll file from the main libpd repo to the unity project no avail. I lack the understanding to know where to go next so any help at all would be appreciated!
Further Progress
It was suggested to try NuGet to obtain the binding dll. On this - I get an error about a framework mismatch between my unity project/visual studio solution and the nuget package libPDBinding version 0.10.0. This is to do with unitys profiles as described in an article titled: "Using NuGet in Visual Studio Tools for Unity". I cant post the link here because I am too new.
However, I think I am sucessful in compiling my own libPDBinding.dll. Apologies for dragging this out, just making sure I am doing this right:
After using libPD\mingw64_build_csharp.bat to compile a x64 version of libpdcsharp.dll, I reference this and libpd\libs\mingw64\libwinthread-1.dll in the VS solution and get the resulting LibPDBinding.dll which was built with .NET 3.5. As mentioned libpdcsharp.dll was compiled using the batch/make file, so am I correct to assume this file has been built with the correct settings?
I proceed to copy these 3 dll's the unity assets folder
For reference it's a blank unity project with uPD as the only package, loaded to a uPD example scene.
I still get some errors at run time and some visual studio warnings as follows.
Unity run time error:
EntryPointNotFoundException: libpd_safe_init
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:81)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)
Visual studio warnings x 50 or so:
Warning CS0436 The type 'LibPD' in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs' conflicts with the imported type 'LibPD' in 'LibPDBinding, Version=0.3.0.177, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs'. libpdinunity.CSharp D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\PureDataCommunicator.cs 87 Active
Thanks again for any help. Much appreciated.
Looking at your error, libpdcsharp is currently placed at <ProjectDirectory>/Assets/Magicolo/AudioTools/PureData/Plugins/
This DLL file should be placed at <ProjectDirectory>/Asset.
Also, make sure that libpdcsharp is compiled with .NET 2.0 or 3.5. This won't work if you compile it with .NET 4.0 and above.
I am the maintainer of C# binding for LibPD.
You also need to change the link to the correct libwinpthread-1.dll version (64 bit instead of 32). In your final project you need all 3 dlls:
LibPDBinding.dll (the managed wrapper)
libpdcsharp.dll (the native dll)
libwinpthread-1.dll (for enabling POSIX threads on Windows)
Also: Have you tried the version from NuGet? No need to compile your own version of the binding.
EDIT: Your warning are from a different source: You have two classes named LibPD, and the C# compiler does not know, which one to use.
I am not sure, if these types are in the same namespace.
If they are in the same namespace, then change your namespace.
If they are in different namespaces, add a line using LibPd = LibPDBinding.LibPD; if you want to use the version from NuGet, or using LibPd = <yournamespace>.LibPD; if you want to use your version.
It's working. The uPD github code has an error as far as I can tell. So hopefully it gets fixed. In the meantime I just started the process from scratch and this is what I did to get the uPD test scene working in unity 5:
Get uPD from github. Theres a unity package in there
Get libPD from github.
Follow the instructions on the libPD github page to compile a x64
vresion of libpdcsharp.dll
replace the existing libpdcsharp.dll inside your unity project with
the x64 version you compiled
locate libpd\libs\mingw64\libwinthread-1.dll in the libpd files and
place this in the your unity project - the same place as the other dll is fine. I
don't think it matters where you put the Dll's (at least it made no
difference in my tests)
Run the project and notice you get an error in unity EntryPointNotFoundException: libpd_safe_init
double click the error to open up the problem script.
Find the following line of code: [DllImport("libpdcsharp", EntryPoint="libpd_safe_init")]
Change it to [DllImport("libpdcsharp", EntryPoint="libpd_init")] That's the real entry point for the init function in libpdcsharp. I don't know what libpd_safe_init is.
Thanks to the contributors who helped me to troubleshoot and find the issue!

Using .NET dependencies with Iron.io IronWorker

I'm brand new to Iron.io's IronWorker, and I'm attempting to interface with it via C#.NET. I have managed to get the sample on the site to work, which utilizes the System.Web.Script.Serialization library. However, I want to use Newtonsoft.Json library for serialization/deserialization. My .worker file (located in the \bin directory of a Visual Studio project) looks like this:
runtime 'mono'
exec 'Debug\hello.exe'
file 'Debug\Newtonsoft.Json.dll'
I'm fairly certain that Newtonsoft.Json is being loaded, because adding the 3rd line of file: 'Debug\Netwonsoft.Json.dll' fixed the error that said that Newtonsoft could not be found.
The error I'm receiving when attempting to run a task is the following (I removed any seemingly sensitive information):
{"code_id":"[CODE_ID]","code_name":"hello","created_at":"2014-11-
16T13:48:01Z","duration":2452,"end_time":"2014-11-16T13:48:06Z","id":"[ID]","msg":"Unhandled
Exception: System.TypeLoadException: Could not load type
'System.Runtime.CompilerServices.ExtensionAttribute' from assembly
'Newtonsoft.Json'.\n at HelloWorld.Main (System.String[] args) [0x00000] in <filename
unknown>:0 \n[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could
not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly
'Newtonsoft.Json'.\n","project_id":"[PROJECTID]","start_time":
"2014-11-16T13:48:04Z","timeout":3600,"updated_at":"2014-11-16T13:48:07Z","status"
:"error"}
In the course of tracking down the error: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'Newtonsoft.Json', I ran across a SO thread that suggested there might be a .NET framework mismatch between v4.0 and v4.5. I'm using v4.5, and I tried changing it to v4.0 with no luck (so this may not be the issue).
Can someone please point me in the right direction as to how to properly utilize a dependency, such as Newtonsoft.Json (so at least the sample on Iron.io will work)?
P.S. I've been toying with IronSharp as an API to assist with interfacing with Iron.io products (once I get past this dependency issue). But, I'm open to suggestions if there's anything better I should be checking out.
SOLUTION
#Featalion's answer below really helped, becuase he pointed me to the stack command, which I placed in like so:
runtime 'mono'
stack 'mono-3.6'
exec 'Debug\hello.exe'
file 'Debug\Netwonsoft.Json.dll'
I believe it resolved the error at hand, because the higher mono framework seems to handle the latest version of Newtwonsoft.Json.dll. I'll post here if I find otherwise.
Make note that as of 11/17/2014, it seems the highest stack version supported by IronWorker is 3.6 (even though 3.10 is available, details found here). Check the highest stable release from Iron.io at this link.
This is, possibly, related to mono version on the IronWorker service. I found related question on StackOverflow. Try to change the stack in your .worker file to mono-3.6 or mono-3.0. See all possible runtime environments on Iron.io's dev site. mono runtime is default, and it is alias for mono-2.10.

C# Mono: TypeLoadException when trying to use a DLL reference

I'm new to Mono/.NET so I'm not quite sure if I'm adding my reference right or not. I'm trying to implement Fleck WebSockets. I downloaded the source, opened the solution in MonoDevelop, and built it. I took the resulting "Fleck.dll" and "Fleck.dll.mdb" and placed them in my project's folder. Then, in MonoDevelop, I went to the reference editor in my solution and selected that Fleck.dll as a .NET Assembly reference.
It seems to work correctly, MonoDevelop finds everything in that assembly. If I type "Fleck." then the autocomplete pops up with all the members of that DLL, but if I write a bit of code trying to use it, I get a TypeLoadException. It compiles and runs but throws that exception immediately.
I'm using the code from an example I found so I don't think that's an issue. Here's my full exception output:
Could not load signature of Fleck.SocketWrapper:Accept due to:
Could not load signature of Fleck.ISocket:Accept due to:
Unhandled Exception: System.TypeLoadException: Could not load type 'Fleck.SocketWrapper' from assembly 'Fleck, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at Fleck.WebSocketServer..ctor (System.String location) [0x00000] in <filename unknown>:0
at TestServer.MainClass.Main (System.String[] args) [0x00000] in /home/danny/Projects/TestServer/Main.cs:10
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not load type 'Fleck.SocketWrapper' from assembly 'Fleck, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
at Fleck.WebSocketServer..ctor (System.String location) [0x00000] in <filename unknown>:0
at TestServer.MainClass.Main (System.String[] args) [0x00000] in /home/danny/Projects/TestServer/Main.cs:10
What am I doing wrong?
It works fine for me. I am using Mono 2.10.8 on Ubuntu. What I did was:
Cloned Fleck from the repository you have pointed.
Ran Nuget to download needed packages for the tests (this was actually not necessary as they are already in the repository).
Compiled Fleck, its tests and test console application contained in solution.
Application ran successfully.
NUnit reported that all tests ran successfully too.
So I bet it should work fine. What is your version of Mono?
Oh, I also explicitly set the framework version for all mentioned projects to 4.0 (which was essential for proper compilation).

MonoTouch 4.0 throwing ReflectionTypeLoadException using the simulator

Since updating a working MonoTouch 3.x app to MonoTouch 4.0, I have been getting the following error when attempting to start a Debug or Release build on the Simulator:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0
at MonoTouch.ObjCRuntime.Runtime.RegisterAssembly (System.Reflection.Assembly a) [0x00000] in <filename unknown>:0
The problem appears to be with System.Xml.Linq, however I have checked all projects and references, and this doesn't even appear to be a transitive reference from what I can tell. I just don't appear to be using this anywhere.
Exception Message is always "Could not load System.Xml.Linq.XNodeNavigator from System.Xml.Linq".
Anyone have any ideas?
The root cause is that the iOS simulator only update files, it never deletes them (even if you delete your app from the simulator).
This cause problems if you have some files (e.g. System.Xml.Linq) at one point and remove them later (directly or use the linker which can remove it). From there you'll have some old, stale files in your simulator app directory.
The next issue is that MonoTouch register all assemblies present in the directory. This means the stale one are registered too in the simulator. At this stage this can work (and you won't see an error) or this could fail (with the error you have above) because the stale assembly does not have everything it needs to be loaded properly.
The easiest workaround is to reset your iOS simulator, that will remove everything (all apps and all files).

Categories