public Task <long> GenerateMessageId(string key) { // Slave if (!this.IsElectedMaster()) { var electedMasterPeer = this.GetElectedMaster(); var queryString = "?" + PeerToPeerHelper.RequestKeys.GenerateMessageId + "=" + HttpUtility.UrlEncode(key); var messageIdGeneratorUrl = electedMasterPeer + MessageReceiverHandler.HandlerName + queryString; return(TaskAsyncHelper.Success <HttpWebResponse, long>(HttpHelper.GetAsync(messageIdGeneratorUrl, req => { }), t => { var messageId = long.Parse(t.Result.GetResponseHeader(PeerToPeerHelper.HeaderPrefix + PeerToPeerHelper.RequestKeys.GenerateMessageId)); _messageIds[key] = messageId; return messageId; })); } // Master if (!_messageIds.ContainsKey(key)) { _messageIds[key] = 1; } return(TaskAsyncHelper.FromResult <long>(_messageIds[key]++)); }