コード例 #1
0
        public void RetrySMS(int sendMessageHistoryId)
        {
            UniqueJob uniqueJob = null;

            try
            {
                uniqueJob = this.uniqueJobList.AddOrUpdate("RetrySMS", sendMessageHistoryId);
                if (uniqueJob == null)
                {
                    return;                    // Job 已經存在
                }
                using (var scope = this.unitOfWork.CreateTransactionScope())
                {
                    var sendMessageHistory = this.unitOfWork.Repository <SendMessageHistory>().GetById(sendMessageHistoryId);

                    decimal totalMessageCost = sendMessageHistory.MessageCost;

                    string currentProviderType = sendMessageHistory.ProviderName;

                    var providerTypesInOrder = GetSmsProviderTypes(currentProviderType);

                    // 由於使用預設的 Provider 無法發送成功,因此使用下一個 SmsProvider 發送
                    SmsProviderType userSmsProviderType = providerTypesInOrder[1];

                    ISmsProvider provider = GetProvider(userSmsProviderType, totalMessageCost); // 如果沒有找到,將會拋出例外

                    this.tradeService.RetrySMS(sendMessageHistory);

                    // 開始發送簡訊
                    provider.RetrySMS(sendMessageHistory.Id);

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                this.logService.Error(ex);
                throw;
            }
            finally
            {
                if (uniqueJob != null)
                {
                    this.uniqueJobList.Remove(uniqueJob);
                }
            }
        }