/// <summary> /// Sends a message to the SMS gateway /// </summary> /// <param name="provider"></param> /// <param name="smsHistory"></param> /// <returns></returns> private async Task SendSMSToProvider(ISMSClientProxy smsClient, EFModel.Provider provider, EFModel.SMSHistory smsHistory) { // send the sms to the provider and return the control to the var result = await smsClient.SendSMSAsync(smsHistory.Id, smsHistory.MobileNumber, smsHistory.Message, null, null).ConfigureAwait(false); // when a task finish the history record is updated smsHistory.ProviderFeedback = result.ReturnedMessage; smsHistory.ProviderFeedBackDateTime = result.TimeStamp; smsHistory.ProviderMsgId = result.ProviderId; if (result.MessageStatus == Common.MessageStatus.Delivered) { smsHistory.Status = Common.MessageStatus.Delivered; } else { smsHistory.Status = Common.MessageStatus.Error; } // _smsHistoryRepository.Update(smsHistory); return; }
/// <summary> /// Sends messages to the provider, creates a session in the history table and saves the messages as history /// under this session /// </summary> /// <param name="messagesToSent"></param> /// <param name="providerId"></param> /// <returns>Returns the session ID</returns> public Guid SendBulkSMS(IEnumerable <DTO.SMSMessageDTO> messagesToSent, string providerName) { // get the provider data EFModel.Provider provider = _providerRepository.GetById(providerName, true); Guid sessionId = Guid.NewGuid(); // First job is to save the messages into the history table in order to populate the id foreach (var msg in messagesToSent) { EFModel.SMSHistory smsHistory = new EFModel.SMSHistory(); smsHistory.ContractId = msg.ContractId; smsHistory.Message = msg.Message; smsHistory.MobileNumber = msg.MobileNumber; smsHistory.PersonId = msg.PersonId; smsHistory.ProviderName = providerName; smsHistory.TemplateId = msg.TemplateId; smsHistory.Status = Common.MessageStatus.Pending; smsHistory.SessionId = sessionId; smsHistory.SessionName = providerName + "|" + DateTime.Now; smsHistory.SendDateTime = DateTime.Now; if (string.IsNullOrWhiteSpace(msg.MobileNumber)) { smsHistory.ProviderFeedback = "The mobile number is not present!"; smsHistory.Status = Common.MessageStatus.Skipped; } _smsHistoryRepository.Add(smsHistory); } UoW.Commit(); List <Task> serverRequests = new List <Task>(); // TODO:Need to abstract the ClientProviderFactory var smsProviderProxy = ClientProviderFactory.CreateClient(providerName, provider.UserName, provider.PassWord); //LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' //http://stackoverflow.com/questions/9606979/string-isnullorwhitespace-in-linq-expression foreach (var historysms in DbContext.SMSHistoryRecords.Where(m => (m.SessionId == sessionId) && (m.MobileNumber != null) && m.MobileNumber.Trim() != string.Empty).ToArray()) { // Collect all tasks in an array serverRequests.Add(SendSMSToProvider(smsProviderProxy, provider, historysms)); } //foreach (var task in await Task.WhenAll(tasks)) //{ // if (task.Item2) // { // Console.WriteLine("Ending Process {0}", task.Item1); // } //} Task.WaitAll(serverRequests.ToArray()); UoW.Commit(); return(sessionId); }