protected Message ReadMessage(string tableName, SoapMessageTableEntity soapMessage)
        {
            byte[] data = null;
            int bytesTotal;
            try
            {

                bytesTotal = Encoding.UTF8.GetByteCount(soapMessage.Message);
                if (bytesTotal > int.MaxValue)
                {
                    throw new CommunicationException(
                       String.Format("Message of size {0} bytes is too large to buffer. Use a streamed transfer instead.", bytesTotal)
                    );
                }

                if (bytesTotal > this.maxReceivedMessageSize)
                {
                    throw new CommunicationException(String.Format("Message exceeds maximum size: {0} > {1}.", bytesTotal, this.maxReceivedMessageSize));
                }

                data = this.bufferManager.TakeBuffer(bytesTotal);
                Encoding.UTF8.GetBytes(soapMessage.Message, 0, soapMessage.Message.Length, data, 0);

                ArraySegment<byte> buffer = new ArraySegment<byte>(data, 0, (int)bytesTotal);
                return this.encoder.ReadMessage(buffer, this.bufferManager);
            }
            catch (StorageException exception)
            {
                throw new CommunicationException(exception.Message, exception);
            }
            finally
            {
                if (data != null)
                {
                    this.bufferManager.ReturnBuffer(data);
                    CloudTable cloudTable = this.cloudTableClient.GetTableReference(tableName);
                    cloudTable.Execute(TableOperation.Delete(soapMessage));
                }
            }
        }
 private RequestContext ReceiveRequestContext(SoapMessageTableEntity soapMessage)
 {
     ThrowIfDisposedOrNotOpen();
     Message message = this.ReadMessage(string.Format(ConfigurationConstants.RequestTable, this.tableName), soapMessage);
     return new AzureTableRequestContext(message, this);
 }
        private bool WaitForMessage(string tableName, TimeSpan timeout, out SoapMessageTableEntity soapMessage)
        {
            soapMessage = null;
            if (this.channelClosed)
            {
                return false;
            }

            ThrowIfDisposedOrNotOpen();
            try
            {
                CloudTable cloudTable = this.cloudTableClient.GetTableReference(tableName);
                bool foundRecords = false;
                DateTime endTime = timeout == TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.Now + timeout;
                bool active = true;
                DateTime lastMsgRecvd = DateTime.Now;
                while (!foundRecords && DateTime.Now < endTime && !this.channelClosed)
                {
                    IEnumerable<SoapMessageTableEntity> queryResults = cloudTable.ExecuteQuery<SoapMessageTableEntity>(
                        new TableQuery<SoapMessageTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", "eq", this.partitionKey)).Take(1));
                    if (queryResults.Count() > 0)
                    {
                        foundRecords = true;
                        lastMsgRecvd = DateTime.Now;
                        soapMessage = queryResults.First();
                    }
                    else
                    {
                        if (active && DateTime.Now - lastMsgRecvd > TimeSpan.FromSeconds(30))
                            active = false;
                        Thread.Sleep(active ? this.activeSleep : this.idleSleep);
                    }
                }

                return foundRecords;
            }
            catch (StorageException exception)
            {
                throw new CommunicationException(exception.Message, exception);
            }
        }
        private void WriteMessage(string tableName, Message message)
        {
            // Create a new customer entity
            SoapMessageTableEntity soapMessage = new SoapMessageTableEntity(partitionKey);
            ArraySegment<byte> buffer;
            using (message)
            {
                this.address.ApplyTo(message);
                buffer = this.encoder.WriteMessage(message, MaxBufferSize, this.bufferManager);
            }
            soapMessage.Message = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, buffer.Count);
            this.bufferManager.ReturnBuffer(buffer.Array);

            CloudTable cloudTable = this.cloudTableClient.GetTableReference(tableName);
            cloudTable.Execute(TableOperation.Insert(soapMessage));
        }
 protected bool WaitForRequestMessage(TimeSpan timeout, out SoapMessageTableEntity soapMessage)
 {
     return this.WaitForMessage(string.Format(ConfigurationConstants.RequestTable, this.tableName), timeout, out soapMessage);
 }