我有这个代码向订阅者发送短信通知(大约60,000个订阅者)我一直收到错误 System.InvalidOperationException 没有数据时读取的尝试无效。我做错了什么代码在
之下var sw = new Stopwatch();
sw.Start();
var networkSettings = ops.GetNetworkSettings().ToDictionary(x => x.NetworkID, x => x.BillingURL);
var serviceIds = serviceRepository.All.Where(x=>x.Status);
//var blastMsgs = new Dictionary<int, Blast>();
var currTime = DateTime.Now.TimeOfDay;
foreach (var item in serviceIds)
{
var termTime = item.TermTime;
var termTimeCeiling = currTime.Add(TimeSpan.FromMinutes(15));
var termTimeFloor = currTime.Add(TimeSpan.FromMinutes(-15));
if (termTime < termTimeFloor || termTime > termTimeCeiling) continue;
var activeEntry = entryRepository.GetMessage(item.ServiceId);
if (activeEntry == null) continue;
var accounts = accountRepository.SubscribersByServ(item.ServiceId);
log.Debug("entry is: "+activeEntry.Msg);
log.Debug("num of accounts: "+accounts.Count());
log.Debug("service id is: "+item.ServiceId);
var options = new ParallelOptions {MaxDegreeOfParallelism = 200};
var count = 0;
Parallel.ForEach(accounts, options, x =>
{
Thread.CurrentThread.IsBackground = false;
lock (log)
{
var msg = new OutboundMessage
{
Message = activeEntry.Msg,
PhoneNumber = x.PhoneNumber,
SenderId = item.Sender,
NetworkId = x.NetworkId
};
if (!ops.DoBilling(msg, item.CampaignId.ToString(), networkSettings)) return;
accountRepository.UpdateAccount(x.SubscriberId);
Interlocked.Increment(ref count);
log.Debug("current bill count is: "+count);
}
});
entryRepository.UpdateMessage(activeEntry.MessageId);
log.Debug("Service Id: " + item.CampaignId);
log.Debug("Bill count is " + count);
}
sw.Stop();
log.Debug("total bill time: "+sw.Elapsed.TotalMinutes+" minutes");