Unable to connect to ZooKeeper from C# application using apache .NET library
apache-zookeeper, c#, orchestration
Solution
It turns out that the canonical version of the C# client library for ZooKeeper is located at https://github.com/ExactTargetDev/zookeeper/tree/et-develop which is different from what the ZooKeeper wiki points to.
- Build the source by getting it from github. Make sure to get the `et-develop` branch.
- running `ant` at the top level (This invokes `jute`, which generates RPC classes used in the C# project)
- Open up the `src/dotnet` folder and the solution in there and
- build it
I ran the unit tests which connected against my local ZooKeeper and they ran fine.
Problem
I'm using Zookeeper v3.3.3+dfsg2-1ubuntu1, running on ubuntu vm. (The VM is running with a NAT network connection) On my development machine (windows 7), if I run: `zkCli.cmd -server 10.10.135.19:2181` it connects fine and I can perform `create`s, `get`s etc. I have a C# 4 application with a NuGet dependency on Org.Apache.ZooKeeper v1.0.0.0. I am using it in the following way: ``` class watcher : IWatcher { private readonly ManualResetEventSlim _connected = new ManualResetEventSlim(false); private WatchedEvent _event; public void WaitUntilConnected() { _connected.Wait(); if (_event == null) throw new ApplicationException("bad state"); if (_event.State != KeeperState.SyncConnected) throw new ApplicationException("cannot connect"); } public void Process(WatchedEvent @event) { _event = @event; _connected.Set(); } } ... public void TestZooKeeper() { _countdownWatcher = new watcher(); _zk = new ZooKeeper( Settings.Default.ZookeeperConnectionString, // 10.10.135.19:2181 new TimeSpan(Settings.Default.ZookeeperConnectionTimeout), // 10000 _countdownWatcher); _countdownWatcher.WaitUntilConnected(); } ``` The problem is that this just hangs. On the zookeeper logs, I see the following: ``` 2012-04-05 08:12:21,376 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn$Factory@251] - Accepted socket connection from /10.0.2.2:51057 2012-04-05 08:12:21,379 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@777] - Client attempting to establish new session at /10.0.2.2:51057 2012-04-05 08:12:21,383 - INFO [SyncThread:0:NIOServerCnxn@1580] - Established session 0x1367c91bf580047 with negotiated timeout 4000 for client /10.0.2.2:51057 2012-04-05 08:12:22,500 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn$Factory@251] - Accepted socket connection from /10.0.2.2:51059 2012-04-05 08:12:22,502 - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@777] - Client attempting to establish new session at /10.0.2.2:51059 2012-04-05 08:12:22,505 - INFO [SyncThread:0:NIOServerCnxn@1580] - Established session 0x1367c91bf580048 with negotiated timeout 4000 for client /10.0.2.2:51059 2012-04-05 08:12:26,000 - INFO [SessionTracker:ZooKeeperServer@314] - Expiring session 0x1367c91bf580047, timeout of 4000ms exceeded 2012-04-05 08:12:26,001 - INFO [ProcessThread:-1:PrepRequestProcessor@387] - Processed session termination for sessionid: 0x1367c91bf580047 2012-04-05 08:12:26,004 - INFO [SyncThread:0:NIOServerCnxn@1435] - Closed socket connection for client /10.0.2.2:51057 which had sessionid 0x1367c91bf580047 2012-04-05 08:12:28,001 - INFO [SessionTracker:ZooKeeperServer@314] - Expiring session 0x1367c91bf580048, timeout of 4000ms exceeded 2012-04-05 08:12:28,002 - INFO [ProcessThread:-1:PrepRequestProcessor@387] - Processed session termination for sessionid: 0x1367c91bf580048 2012-04-05 08:12:28,004 - INFO [SyncThread:0:NIOServerCnxn@1435] - Closed socket connection for client /10.0.2.2:51059 which had sessionid 0x1367c91bf580048 ``` And this continues until I kill the process manually, ie, the `WaitUntilConnected()` method never returns. (verified with debugging) It seems as if the client connection reaches the server fine, but the Watcher never realises this, nothing further happens on that channel and the server kills the connection, only for the client to retry. Any ideas what I'm doing wrong here?