0

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!

Dean Rich
  • 11
  • 2
  • Is your `task` of type Task>? You are awaiting `task` and also using ContinueWith. This is not a good practice. You should not mix the two. If the task is of type Task> (this makes absolutely no logical sense), then await both `var task2 = await task;` var result = await task2; – Shawn Kendrot Mar 27 '14 at 15:36
  • My task is not of type Task>. I'm calling out to an WCF OData service so the return type is Task>. So basically what I'm doing is asking for the service to return the DTO so I can populate my business objects. Does that change your opinion using "await task.ContinueWith". Now that you mention it I agree I could have just awaited that task instead of calling ContinueWith. Unless I'm missing something not so obvious I plan on leaving it with ContinueWith, and in the future I will just call await task to get my results as I agree its cleaner and easier to read. – Dean Rich Mar 27 '14 at 15:49
  • 1
    OK, remove the ContinueWith and just await the task itself – Shawn Kendrot Mar 27 '14 at 15:51
  • I've taken the above code and hacked it straight into my ViewModel code to see if that could isolate the issue and it has helped. At this point I'm pinning the problem on Caliburn.Micro (CM). I'm using CM in WinRT without any issues. I was using CM EventAggregator to pipe messages to separate classes. Apparently in WinRT this is OK, but in Win Phone 8 there are some issues. In this case I was able to throw away the event messaging in CM and just call a method on my ViewModel myself. This seems to solve this issue. Luckily I think I only used the EventAggregator in 1 or 2 other places. – Dean Rich Mar 27 '14 at 16:01
  • Oh, and big THANKS for your help and suggestions Shawn. I guess I had a few more things to try out but assumed (wrongly) that Caliburn.Micro is pretty stable and I could assume common behavior in both platforms. – Dean Rich Mar 27 '14 at 16:02
  • sounds like you've got it working? If you solved your own problem, add an answer below so others can find it – Shawn Kendrot Mar 27 '14 at 16:18

0 Answers0