public string GetAvailableCredits(string smsGateWayName, string userName, string password) { // TODO:Need to abstract the ClientProviderFactory var smsProviderProxy = ClientProviderFactory.CreateClient(smsGateWayName, userName, userName); return(smsProviderProxy.GetAvailableCreditsAsync().Result); }
/// <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); }