3

I'm doing an Universal Windows Platform app with Visual Studio and i'm facing a very strange problem. It's a Background Task on a Windows Runtime Component. The Background task executes without problem if the problematic part of code is removed.

When the code is reached the first sentence (WebRequest request = WebRequest.Create(_finalURI);) is executed but WebResponse response = await request.GetResponseAsync(); stops immediately the background task, without exception, without anything. Pretty weird.

     WebRequest request = WebRequest.Create(_finalURI);
     WebResponse response = await request.GetResponseAsync(); // Here it closes
     Stream dataStream = response.GetResponseStream();
     StreamReader reader = new StreamReader(dataStream);
     textResponse = reader.ReadToEnd();

This is a very rare situation for me. I uploaded a video to YouTube so you can see it as it happens. I've been through this more than two hours :( and keep trying.

EDIT:

I've changed to the class HttpClient and it's the same behavior.

var httpClient = new HttpClient();
textResponse = await httpClient.GetStringAsync(_finalURI); // Here it closes
httpClient.Dispose();

EDIT 2:

Method signature, as requested by Scott Chamberlain:

public async Task<ArticleList> GetArticleList(ArticleAttributes attributes,
ImageResFromAPI imageres, string singleArticle)
Adelaiglesia
  • 365
  • 1
  • 4
  • 16

1 Answers1

1

I've got the same problem. Application closes without any exception. Even in AppDomain.CurrentDomain.UnhandledException

Its pretty weird, but instead of using await request.GetResponseAsync();, if you use,

var webResponseTask = request.GetResponseAsync();

await Task.WhenAll(webResponseTask);

if (webResponseTask.IsFaulted)
  throw webResponseTask.Exception;

using (var webResponse = webResponseTask.Result)
{
}

you can bypass this issue.

Sency
  • 2,818
  • 8
  • 42
  • 59