public async Task StartExecuteReturnsOkAndAddsHandlerIfPayloadIsValid()
        {
            var message = new ServerRegistration
                          {
                              Hostname = "testServer",
                              Port = 867,
                          };
            ByteArrayContent payload;
            using (var ms = new MemoryStream())
            using (var writerStream = new WriterStream(ms))
            {
                writerStream.CreateCompactBinaryWriter().Write(message);
                payload = new ByteArrayContent(ms.GetBuffer());
            }

            var response = await
                           this.httpClient.PostAsync(TestUtils.GetUri(this.server,
                                                                      RegistrationClient.RegistrationEndpoint,
                                                                      string.Empty),
                                                     payload);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.IsNotNull(this.server.ServerList["testServer"]);
        }
 private static ByteArrayContent GetRequestPayload(BatchQueryRequest data)
 {
     using (var ms = new MemoryStream())
     using (var writerStream = new WriterStream(ms))
     {
         writerStream.CreateCompactBinaryWriter().Write(data);
         return new ByteArrayContent(ms.GetBuffer());
     }
 }
        private async void StartRegistration()
        {
            IPAddress[] addresses;
            try
            {
                addresses =
                    await Task<IPAddress[]>.Factory.FromAsync(Dns.BeginGetHostAddresses, Dns.EndGetHostAddresses,
                                                              this.destinationHostname, null);
            }
            catch (SocketException e)
            {
                Events.Write.RegistrationDestinationResolutionFailed(this.destinationHostname, e.Message);
                return;
            }

            var serverRegistration = new ServerRegistration
                                     {
                                         Hostname = this.sourceHostname,
                                         Port = this.sourcePort,
                                         MachineFunction = this.sourceMachineFunction,
                                         Datacenter = this.sourceDatacenter,
                                     };

            foreach (var counter in this.dataManager.Counters)
            {
                serverRegistration.Counters.Add(
                                                new CounterInfo
                                                {
                                                    Name = counter.Name,
                                                    Type = counter.Type,
                                                    Dimensions = counter.Dimensions.ToList(),
                                                    StartTime = counter.StartTime.ToMillisecondTimestamp(),
                                                    EndTime = counter.EndTime.ToMillisecondTimestamp(),
                                                });
            }

            byte[] payload;
            using (var ms = new MemoryStream())
            using (var writerStream = new WriterStream(ms, this.dataManager.MemoryStreamManager))
            {
                var writer = writerStream.CreateCompactBinaryWriter();
                writer.Write(serverRegistration);
                payload = ms.ToArray();
            }

            foreach (var address in addresses)
            {
                this.RegisterWithAddress(address, payload);
            }

            lock (this)
            {
                if (this.registrationTimer != null)
                {
                    this.registrationTimer.Change(this.registrationInterval, TimeSpan.Zero);
                }
            }
        }