I have a data class that return some objects from a wcf dataservice to a silverlight app:
void ExecuteWipReportQuery(DataServiceQuery qry)
{
context = new StaffKpiServices.HwfStaffKpiEntities(theServiceRoot);
qry.BeginExecute(new AsyncCallback(a =>
{
try
{
IEnumerable results = qry.EndExecute(a);
OnDataLoadingComplete(new WipReportByMonthEventArgs(results));
}
catch (Exception ex)
{
OnDataLoadingError(ex);
}
}), null);
}
The view model then get these results and adds them to an observable collection:
void wipReportDataContainer_DataLoadingComplete(object sender, Domain.WipReportByMonthEventArgs e)
{
Application.Current.RootVisual.Dispatcher.BeginInvoke(() =>
{
this.wipReport.Clear();
string s = “”;
foreach (StaffKpiServices.WipReportByMonth r in e.Results)
{
//this.wipReport.Add(r);
//s += r.ClientCode;
this.wipReport.Add(new StaffKpiServices.WipReportByMonth
{
ClientCode = r.ClientCode,
ClientGroup = r.ClientGroup,
ClientName = r.ClientName,
ClientType = r.ClientType,
FinancialYear = r.FinancialYear,
Month = r.Month,
OSDebt = r.OSDebt,
OSDisb = r.OSDisb,
OSOther = r.OSOther,
OSTime = r.OSTime,
OSTotal = r.OSTotal,
PartnerUserName = r.PartnerUserName,
PracName = r.PracName,
Recov = r.Recov,
RecovFees = r.RecovFees,
RecPerc = r.RecPerc,
SicCode = r.SicCode,
SicParentName = r.SicParentName,
StaffName = r.StaffName,
YTDFees = r.YTDFees,
YTDTime = r.YTDTime
});
s += r.ClientCode + " ";
}
string s2 = "";
foreach (var p in this.wipReport)
{
s2 += p.ClientCode + " ";
}
OnPropertyChanged("WipReport");
if (null != LoadComplete)
{
LoadComplete(this, EventArgs.Empty);
}
});
}
Everything works ok, but if the data is refreshed two or three times, then the collections retrun contains the right number of objects, but all with duplicate properties. There seems to be no reason why, it is as if the foreach is not working on the collection, but at the same time not returning any errors. Any ideas?
Ok so this was odd.. but by recreating the object that fetches the data (the one that creates the context), all was ok, but if the viewmodel kept alive the object responsible for running the dataservice query, the problem occured…..
I have no idea why this should have been, but the problem has gone away…..