用于确定是否继续处理销售线索的if
语句未包装在#if DEBUG...#else
指令中,因此在所有情况下都应执行。
TL; DR 在调试中运行时,我的某些代码返回的结果与在运行发行版时返回的结果不同。为什么会发生这种情况,我该如何解决?
我的应用程序跟踪客户邮件列表中的电子邮件。如何执行此操作对于这篇文章并不重要,但是它是一个WPF应用程序,可以获取所有早于2周并符合其他条件的潜在客户。
首先,将线索加载到内存中
public class MainViewModel : BaseViewModel
{
public ContactsViewModel Contacts { get; }
public EmailsViewModel Emails { get; }
public FollowUpsViewModel FollowUps { get; }
public GroupsViewModel Groups { get; }
public LeadsViewModel Leads { get; }
public LeadStatusesViewModel LeadStatuses { get; }
public MainViewModel(LoadingViewModel loadingVM)
{
Groups = new GroupsViewModel();
LeadStatuses = new LeadStatusesViewModel();
Contacts = new ContactsViewModel(Groups);
Emails = new EmailsViewModel(Campaigns, Templates);
Leads = new LeadsViewModel(Campaigns, Emails, LeadStatuses, Groups);
FollowUps = new FollowUpsViewModel(Leads);
}
public async Task Load()
{
// Contacts data is loaded ad hoc when requested.
await Groups.Load();
await Emails.Load();
await LeadStatuses.Load();
await Leads.Load();
// Delegate follow ups to a new thread so as to keep the UI responsive.
new Thread(delegate () { FollowUps.FollowUp(this); }).Start();
}
}
线索被加载到LeadsViewModel的ObservableCollection中:
public class LeadsViewModel : BaseViewModel
{
public ObservableCollection<LeadViewModel> AllLeads = new ObservableCollection<LeadViewModel>();
public LeadsViewModel(CampaignsViewModel campaigns, EmailsViewModel emails, LeadStatusesViewModel statuses, GroupsViewModel groups)
{
// Loads other ObservableCollections... These aren't really relevant to this question for the most part.
_campaigns = campaigns;
_emails = emails;
_statuses = statuses;
_groups = groups;
}
public async Task Load()
{
var contacts = await Dal.Instance.Contacts.GetDictionaryAsync();
var models = await TrackerDal.Instance.Leads.ListAsync();
foreach (var m in models)
{
var lead = new EmailStatus();
lead = m;
// Make VERY sure that the error is that the associated contact doesn't exist.
if (!contacts.TryGetValue(lead.ContactId, out var contact))
{
TrackerDal.Instance.Leads.Delete(lead, false);
}
else
{
// Add the lead.
AllLeads.Add(new LeadViewModel(this, m, _emails, _statuses, _groups, contact));
}
}
}
}
现在这很有趣...后续流程会读取线索并检查关联的电子邮件是否不为空。如果存在潜在客户,但电子邮件不为空,它将继续进行,否则将结束该过程。
这可以很好地工作并更新潜在客户,甚至只在我在调试中运行它时才向我的收件箱发送后续电子邮件。我有条件地登录以确定这一点。
如果我使用的是发布版本(#if !DEBUG
),则不会返回潜在客户数据。
public class FollowUpsViewModel : BaseViewModel
{
private readonly LeadsViewModel _leads;
public FollowUpsViewModel(LeadsViewModel leads)
{
_leads = leads;
}
public void FollowUp(MainViewModel mvm)
{
try
{
UpdateMessage(mvm, $"Follow up started.");
Logger.Instance.LogInfo("Follow ups started...");
int leadNumber = 1;
int leadsCount = 0;
#if DEBUG
Logger.Instance.LogDebug($"[Follow Up] Connection string is {TrackerDal.Instance.ConnectionString}");
#else
Logger.Instance.LogInfo($"[Follow Up] Connection string is {TrackerDal.Instance.ConnectionString}");
#endif
#if DEBUG
Logger.Instance.LogDebug("[Follow Up] Checking valid leads exist...");
#else
Logger.Instance.LogInfo("[Follow Up] Checking valid leads exist...");
#endif
if (_leads.AllLeads.Where(x => x.Email != null).Count() >= 1)
{
// At this point, the code loops through existing leads to send follow up emails and updates the database.
}
else
{
#if DEBUG
Logger.Instance.LogDebug("None found...");
#else
Logger.Instance.LogInfo("None found...");
#endif
}
}
catch(Exception ex)
{
// Do error handling stuff
}
}
以下是日志文件:
2018-07-19 16:25:14,701 [16] INFO后续行动已开始...
2018-07-19 16:25:14,745 [16]调试[跟进]连接字符串为Data Source = ortund;初始目录= ortund;用户ID = ortund; password = gwrhw4h; MultipleActiveResultSets = True
2018-07-19 16:25:14,745 [16]调试[跟进]检查是否存在有效的线索...
2018-07-19 16:25:14,747 [16]调试[跟进]找到有效的线索...
2018-07-19 16:25:14,748 [16]调试[跟进]找到2条有效线索供处理...
2018-07-19 16:25:14,749 [16]调试[跟进]开始领先#1
2018-07-19 16:25:14,798 [16]调试[跟进]发送线索1的后续电子邮件
2018-07-19 16:25:15,078 [16]数据库中的调试[跟进]线索#1已更新
2018-07-19 16:25:15,078 [16]调试[跟进] 1号线索处理已完成。
2018-07-19 16:25:15,078 [16]调试[跟进]开始了#2线索
2018-07-19 16:25:15,080 [16]调试[跟进]发送线索2的后续电子邮件
2018-07-19 16:25:15,155 [16]数据库中的调试[跟进]线索#2已更新
2018-07-19 16:25:15,157 [16]调试[跟进] 2号线索处理已完成。
2018-07-19 16:27:57,562 [16] INFO后续行动已开始...
2018-07-19 16:27:57,629 [16]信息[跟进]连接字符串为Data Source = ortund;初始目录= ortund;用户ID = ortund; password = gwrhw4h; MultipleActiveResultSets = True
2018-07-19 16:27:57,629 [16]信息[跟进]检查是否存在有效的潜在客户...
2018-07-19 16:27:57,630 [16]信息未找到...