locked
Problem with WinJS.xhr in background task

    Question

  • My app uses WinJS.xhr to fetch data from a web service.  The response is a document which I can parse using the usual querySelectorAll() methods.

    I also use WinJS.xhr to access the same service in a background process that is triggered by a timer.  The problem is that when used in this background process, the return value always has a null response for responseXML.  This is a change from the CP which worked OK.  I do get a responseText return and I tried to use DOMParser.parseFromString() to create a document from the text response but ran into DOMParser undefined.

    I presume this is due to some security rules but is there a way to get an XML document in a background task so I can use the querySelector to parse?

    Otherwise, I will have to resort to a custom string parser.

    Tuesday, June 12, 2012 3:32 PM

Answers

  • Hi CodePB,

    This appears to be a bug.  Please use the workaround I specified earlier!

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 20, 2013 10:55 AM
    Moderator

All replies

  • Hey JR,

    I did not repro this yet, but can you use a technique like this in your background task:

                var articleSyndication = new Windows.Data.Xml.Dom.XmlDocument();

                articleSyndication.loadXml(response.responseText);

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, June 14, 2012 2:29 PM
    Moderator
  • Hi Jeff,

    Yes, after running across this post, I did give it a try.  The loadXml method comes with its own security baggage that can be configured with options.  In theory, I should be able to parse the xml using the selectNodes methods; however, after some effort, I was not able to extract the selected data.  I tried to use the viewer in Visual Studio on the articleSyndication object but the result did not look like an xml document tree and the selectors could not find my nodes.

    Anyway, I moved on to use another port in the web service that presents the data in CSV format which I am able to parse in code.

    Thanks for the suggestion.  Maybe I'll get back to the technique and see how I can make it work.

    Thursday, June 14, 2012 3:16 PM
  • OK,

    I did not see anything that indicated that you should run into this problem from a backgroundtask.  If I get some time I will play with this more later.

    -Jeff


    Jeff Sanders (MSFT)

    Thursday, June 14, 2012 3:26 PM
    Moderator
  • Thanks.  Not critical at the moment.  PBCaK is always a possibility too!
    Thursday, June 14, 2012 3:46 PM
  • Hi,

    I just ran into the same problem myself. This is the only reference I have come across that details the problem. Was any solution ever found? I'd also be interested to know whether this is expected behaviour and I need to find a work around, or whether it is a bug, and I can expect a fix?

    Tuesday, March 19, 2013 8:46 AM
  • There is no know bug and I cannot repro the problem.

    Please post a complete repro of the issue for someone to look at (you can use SkyDrive to share your project).

    -Jeff


    Jeff Sanders (MSFT)


    Tuesday, March 19, 2013 11:40 AM
    Moderator
  • Just a guess but could it be that (according to http://msdn.microsoft.com/library/windows/apps/Hh464925) network availability to the App is switched off during App suspension and it affects the Background Task?

    "When an app is suspended, it does not receive network events that it registered to receive. These events are not queued, they are simply missed. Therefore, your app should test the network status when it is resumed."

    You might consider using Windows.Networking.BackgroundTransfer.

    Use "Transfering data in the background" as reference.

    Tuesday, March 19, 2013 12:39 PM
  • Hi Jeff,

    I have uploaded the project to SkyDrive, and the public link is here:

    https://skydrive.live.com/redir?resid=91DA9BFFB4DCBB4F!375&authkey=!AMITNWdUHao1iUQ

    It is source controlled, so you may get issues when attempting to open it.

    The way I hit the issue is by running the application, giving it permission to perform background tasks, then running the background task through the visual studio debugger. If I break it just after the xhr request in the background task, I can see there is responseText that looks like a valid xml string, but responseXML is not set
    • Edited by CodePB Tuesday, March 19, 2013 2:59 PM
    Tuesday, March 19, 2013 2:57 PM
  • Hi CodePB,

    This appears to be a bug.  Please use the workaround I specified earlier!

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, March 20, 2013 10:55 AM
    Moderator
  • Nope!

    Good guess though!


    Jeff Sanders (MSFT)

    Wednesday, March 20, 2013 10:55 AM
    Moderator