C# Binding (.NET & Mono)

NetMQ

Native C# port of ZeroMQ. Stable and actively maintained.

Binaries

Available on Nuget.

Source Code

http://github.com/zeromq/netmq

Sample code

using (NetMQContext ctx = NetMQContext.Create())
{
    using (var server = ctx.CreateResponseSocket())
    {
        server.Bind("tcp://127.0.0.1:5556");
        using (var client = ctx.CreateRequestSocket())
        {
            client.Connect("tcp://127.0.0.1:5556");
            client.Send("Hello"); 
 
            string m1 = server.ReceiveString();
            Console.WriteLine("From Client: {0}", m1);
            server.Send("Hi Back");
 
            string m2 = client.ReceiveString();
            Console.WriteLine("From Server: {0}", m2);
            Console.ReadLine();
        }
    }
}

Castle.Zmq

Modern .Net binding targeting ØMQ v4.0.5. Released under the LGPL license.

Common ZMQ patterns also available such as proxy/device, shared queue, forwarders, workerpool using idiomatic C#.

Binaries

Will soon be available on nuget

Source code

https://github.com/castleproject/castlezmq

Sample code

const string MsgReq = "Hello";
const string MsgReply = "World";
 
using (var ctx = new Context())
using (var reqSocket = ctx.CreateSocket(SocketType.Req))
using (var repSocket = ctx.CreateSocket(SocketType.Rep))
{
    repSocket.Bind("tcp://0.0.0.0:90002");
 
    reqSocket.Connect("tcp://127.0.0.1:90002");
 
    reqSocket.Send(MsgReq);
 
    var msg = repSocket.Recv();
    var msgStr = Encoding.UTF8.GetString(msg);
 
    repSocket.Send(MsgReply);
 
    msg = reqSocket.Recv();
    msgStr = Encoding.UTF8.GetString(msg);
}

clrzmq

Aims to provide the full functionality of the underlying ZeroMQ API

You can install clrzmq from binaries or source code, the binding currently targets ØMQ v2.1.

Binaries

If you are using .NET on Windows you can install the clrzmq NuGet package (it includes the binaries of the ØMQ and clrzmq libraries).

Source Code

http://github.com/zeromq/clrzmq

Build

Microsoft Visual C# 2010 Express or MonoDevelop 2.2 or later is recommend.

Four build profiles have been included with the aim of provide a cross platform binding.

Binaries will be placed in /bin/debug or /bin/release.

  • WIN_DEBUG Target CPU: x86/x64
  • WIN_RELEASE Target CPU: x86/x64
  • POSIX_DEBUG Target CPU: x86/x64
  • POSIX_RELEASE Target CPU: x86/x64

Make sure to select the configuration appropriate for your platform.

N.B. The ZeroMQ library file will need to be made available for the binding to work.

Usage/Examples

The binding is available within the ZMQ namespace.

The guide examples should be used to gain a better understanding of the API.

Bug Reporting

http://github.com/zeromq/clrzmq/issues

Mailing list

gro.qmorez.stsil|ved-qmorez#gro.qmorez.stsil|ved-qmorez


ZeroMQ Interop

There is an MIT-licensed binding available at CodePlex.

Source & Binaries

Current release (May 22nd, 2011)
ZeroMQ Interop v0.8.304.11142 (beta)

Features & Goals

  • First-class support for .NET events programming, implemented transparently using zmq_poll()
  • Messages are simply Byte[]s, with overloads throughout the API that accept Strings
  • Context management is taken care of automatically on a per-AppDomain basis using refcounting
  • Compile with any C# 3.5 compiler, run on any ECMA-334 (.NET 2.0) compatible CLI
  • Support for both 32 and 64 bit versions of the library on any platform that has an official ZeroMQ port

Example

// Set up a publisher.
 
var publisher = new ZmqPublishSocket {
    Identity = Guid.NewGuid().ToByteArray(),
    RecoverySeconds = 10
};
 
publisher.Connect( address: "tcp://127.0.0.1:9292" );
 
// Set up a subscriber.
 
var subscriber = new ZmqSubscribeSocket();
 
subscriber.Bind( address: "tcp://127.0.0.1:9292" );
 
subscriber.Subscribe( prefix: "" ); // subscribe to all messages
 
// Add a handler to the subscriber's OnReceive event
 
subscriber.OnReceive += () => {
 
    String message;
    subscriber.Receive( out message, nonblocking: true );
 
    Console.WriteLine( message );
};
 
// Publish a message to all subscribers.
 
publisher.Send( "Hello world!" );