[zeromq-dev] I've written a completely new C# API for ZeroMQ

Alex Forster alex at alexforster.com
Tue Dec 21 07:47:46 CET 2010


I have quite a few gripes with clrzmq and clrzmq2, and NZMQ is incomplete in several areas, so over a few weekends this past month I've written a new C# API for ZeroMQ based on the 2.0.10 release of zmq.h.

ZeroMQ Interop v0.8.190.10354 (beta)
http://zeromq.codeplex.com

* Feature-complete
* MIT licensed
* Targeted at both Microsoft and Mono .NET 2.0 CLRs (though it does require a 3.5 compatible compiler, basically for lambda syntax)
* Includes binaries for both 32 and 64bit platforms (without any #ifdefs)

Here's an example using Pub/Sub sockets-

> // Set up a publisher.
> 
> var publisher = new ZmqPublishSocket {
> 	Identity = Guid.NewGuid().ToByteArray(),
> 	RecoverySeconds = 10
> };
> 
> publisher.Bind( address: "tcp://127.0.0.1:9292" );
> 
> // Set up a subscriber.
> 
> var subscriber = new ZmqSubscribeSocket();
> 
> subscriber.Connect( 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!" );


A few things that make this sample stand out from the other two ZeroMQ C# libraries-

* There's no need to manage your ZeroMQ context; it's taken care of on a per-AppDomain basis using refcounting.
* There is no "message" object because it provides no added benefit in C#. Messages are simply Byte[]s, with overloads throughout the API that accept Strings.
* First-class support for .NET events programming, implemented using zmq_poll() (but that's completely transparent to the user).

I'd love some feedback. I'm just starting to use it in a project I'm working on, and so far it all seems to be working smoothly and at high throughput, but I'm releasing it as "beta" because I don't feel that it has enough real-world experience yet.

Alex Forster
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20101221/0762cd98/attachment.htm>


More information about the zeromq-dev mailing list