编辑:
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] DEBUG [Follow Up] 连接字符串为 Data Source=ortund;初始目录=ortund;用户 ID=ortund;密码=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] INFO [Follow Up] 连接字符串为 Data Source=ortund;初始目录=ortund;用户 ID=ortund;密码=gwrhw4h;MultipleActiveResultSets=True
2018-07-19 16:27:57,629 [16] 信息 [跟进] 检查有效线索是否存在...
2018-07-19 16:27:57,630 [16] 信息未找到.. .