The Setup: I have a code base that includes a few PCLs one of those PCL is a group of data access classes that call out to my WCF Data service. I've leveraged the Task.Factory.FromAsync to manage the beginExecute/endExecute of calling out to the WCF Data service. I then await for the results and send the data to the application. I started out by first creating a Win Store app and everything worked great. Now I'm in the process of creating a win phone 8 UI. Now I'm running into a snag. The snag seems to be an exception on the line that awaits the task created by the Task.Factory.FromAsync method. To further complicate matters the exception does not happen if I install and run the application for the first time. The second time I open the application the error is thrown. So I'm really confused as to why the second run causes the error since all of the same logic is being followed. The exception I get isn't from anything in my code but seems to be from the .NET library itself. I've tried a few other things to try to solve this mysterious issue. I created a Task.Delay() task and that line has no problem, but when I call await on its task I get the exact same error. I tried to just get the Task.Result instead of awaiting it and the result is never returned so the application just hangs (and an exception was never thrown either).
I didn't run into any issues in my WinRT application so I'm guessing all of my logic is sound and there shouldn't be any odd threading issues, etc.
The Exception:
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll When I get this exception I also get a tab in visual studio that says "Source Code Not Available". That makes sense to me, but this exception doesn't help me out all and I can't get more detail. I can't seem to find the inner exception (if there is one) to get additional detail.
The code:
var retMasterDataEntryInfos = new List<Model.MasterData.MasterDataEntryInfo>();
try
{
var task = Task.Factory.FromAsync(_entities.MasterDataEntryInfoes.BeginExecute,
(result) => _entities.MasterDataEntryInfoes.EndExecute(result),
null);
return await task.ContinueWith((result) =>
{
string a = string.Empty;
if (result != null && result.Result != null && result.Exception == null)
{
foreach (var r in result.Result)
{
var newMDEI = new Model.MasterData.MasterDataEntryInfo()
{
Id = r.Id,
KeyName = r.KeyName,
Name = r.Name,
LastUpdated = r.LastUpdated
};
newMDEI.DatabaseCreationCleanup();
retMasterDataEntryInfos.Add(newMDEI);
}
}
return retMasterDataEntryInfos;
});
}
catch (Exception ex)
{
string a = string.Empty;
}
return retMasterDataEntryInfos;
The error happens on the line "return await task.ContinueWith((result) =>". As you can see I've added a try/catch and that doesn't even help to contain the issue as the catch block is never called.
Any suggestions would be appreciated including solutions to the issue or debugging help to get better exception detail. I've just pounded my brain to figure this out and am coming short. Its really frustrating as I was hoping all the hard work was done with the WinRT app and now all I need to do was create a quick simple UI on the windows phone 8. I was hoping to deliver a sweet surprise to my boss to say you wanted a WinRT app, but we were able to reuse all of our code and easily got two platforms with minimal extra work. Your help is greatly appreciated, Thanks!