Reputation: 8245
The if
statement that determines whether to continue processing leads is not wrapped in an #if DEBUG...#else
directive so it should execute in all cases.
TL;DR Some part of my code is returning different results when I run in debug to what it returns when I'm running a release build. Why would this happen and how do I fix it?
My app processes follow up emails on the client's mailing list. How it does this is not important for this post, but its a WPF app that gets all leads that are older than 2 weeks and meet some other conditions.
First, leads are loaded into memory:
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();
}
}
The leads are loaded into an ObservableCollection on the LeadsViewModel:
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));
}
}
}
}
Now here's the fun bit... The follow ups process reads the leads and checks that the associated email is not null. If there are leads where the email is not null, it'll proceed, else it'll just end the process.
This works fine and updates leads, even sending follow up emails to my inbox, ONLY when I run it in debug. I logged conditionally to determine this.
If I'm using a release build (#if !DEBUG
), it returns no leads data.
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
}
}
Here's log files:
2018-07-19 16:25:14,701 [16] INFO Follow ups started...
2018-07-19 16:25:14,745 [16] DEBUG [Follow Up] Connection string is Data Source=ortund; Initial Catalog=ortund; user id=ortund; password=gwrhw4h;MultipleActiveResultSets=True
2018-07-19 16:25:14,745 [16] DEBUG [Follow Up] Checking valid leads exist...
2018-07-19 16:25:14,747 [16] DEBUG [Follow Up] Valid leads found...
2018-07-19 16:25:14,748 [16] DEBUG [Follow Up] 2 valid leads found for processing...
2018-07-19 16:25:14,749 [16] DEBUG [Follow Up] Started lead #1
2018-07-19 16:25:14,798 [16] DEBUG [Follow Up] Sending follow up email for lead #1
2018-07-19 16:25:15,078 [16] DEBUG [Follow Up] lead #1 updated in database
2018-07-19 16:25:15,078 [16] DEBUG [Follow Up] lead #1 processing complete.
2018-07-19 16:25:15,078 [16] DEBUG [Follow Up] Started lead #2
2018-07-19 16:25:15,080 [16] DEBUG [Follow Up] Sending follow up email for lead #2
2018-07-19 16:25:15,155 [16] DEBUG [Follow Up] lead #2 updated in database
2018-07-19 16:25:15,157 [16] DEBUG [Follow Up] lead #2 processing complete.
2018-07-19 16:27:57,562 [16] INFO Follow ups started...
2018-07-19 16:27:57,629 [16] INFO [Follow Up] Connection string is Data Source=ortund; Initial Catalog=ortund; user id=ortund; password=gwrhw4h;MultipleActiveResultSets=True
2018-07-19 16:27:57,629 [16] INFO [Follow Up] Checking valid leads exist...
2018-07-19 16:27:57,630 [16] INFO None found...
Upvotes: 0
Views: 197