2

Currently, I'm trying to upload files from a server using web services in the portable library. For each file, I make this :

WebRequest request = WebRequest.Create("http://localhost:49364/" + url);

 request.BeginGetResponse((aResult) =>
 {
      var retour = aResult.AsyncState as WebRequest;
      WebResponse reponse = retour.EndGetResponse(aResult);
      callback(reponse);
 }, request);

On my callback method, I make this :

byte[] bytes;
string currentFileName = fileName;
string categorie = currentFileName.Split('/').ElementAt(0);
string dir = currentFileName.Split('/').ElementAt(1);

using (var reader = new BinaryReader(reponse2.GetResponseStream()))
{
    bytes = new byte[reponse2.ContentLength];
    reader.Read(bytes, 0, (int)reponse2.ContentLength);
}
fileService.EnsureFolderExists(categorie);
fileService.EnsureFolderExists(fileService.PathCombine(categorie, dir));
fileService.WriteFile(currentFileName, bytes);

I get the whole file as a byte array. But, with winRT, writing the file stops quickly and my local file is not complete. If I try to upload just one file, the writing also stops. But, if I try with Silverlight (I extended MvvmCross to Silverlight), the writing is complete. I have not yet tested for MonoDroid et MonoTouch.

So, my question is: Why the writing stops?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Titecarma
  • 31
  • 4

2 Answers2

2

I have looked through the code for WriteFile in MvxBlockingWinRTFileStoreService.cs and I can't see any obvious bug.

To test this, I've just written a quick standalone WinRT test app using https://gist.github.com/4016898.

This saves the 37kB Bing home page file perfectly. Does it also work on your server?

After this test, my guess is that maybe there is some bug in your web transfer code - perhaps even in the localhost service. However, it's still possible that the bug is in the StorageFile saving.

Some questions:

  • Can you add additional trace to find out the data buffer length reported at each stage in your download?

  • Can you adapt the simple test harness above so that it shows the same results?


One possible candidate is:

You are using ContentLength as the stream length? Are you sure that this is the correct length to use?

e.g. if you have GZip compression enabled, then ContentLength will give you the length of the transmitted compressed data, not the length of the data itself - see content-length when using http compression

The more I think about it the more this makes sense to me - Silverlight will be using the browser stack which will have different HTTP accept headers compared to the WinRT stack.


Some good news is that async/await are coming to MonoTouch and MonoDroid soon - and when they do then I will try to make the file APIs all available as async and await.

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
1

Stuart,

First, thank you for your answer !

I tried your example and adapt to my case (upload files from a server via web service), and at first everything worked. All files are uploaded correctly. But when I added images to upload, I had the same problem. Image files AND text files are not complete.

But looking at the code of your example, I found a solution, thought I don't really understand where the problem is.

For writing works, I replaced this (in my callback method) :

...

using (var reader = new BinaryReader(reponse2.GetResponseStream()))
{
    bytes = new byte[reponse2.ContentLength];
    reader.Read(bytes, 0, (int)reponse2.ContentLength);
}
...
fileService.WriteFile(currentFileName, bytes);

by this :

...

var mem = new MemoryStream();
using (var stream = reponse2.GetResponseStream())
{
        stream.CopyTo(mem);
}
mem.Seek(0L, SeekOrigin.Begin);
...
fileService.WriteFile(currentFileName, mem.ToArray());

I don't know why, but it works ! (If you know why this works, I'm interested)

So, thank you for your help !

Titecarma
  • 31
  • 4
  • see my answer - it's in the bold comment - ContentLength is not unzipped stream length. PS We want your silverlight port in public :) – Stuart Nov 05 '12 at 16:13
  • Stuart, I come to you on the adaptation of Mvvmcross Silverlight. Can I have your email address to send you the code? I do not use GitHub ... And when you have the code, I have a question for you regarding a memory leak;) Thank you ! – Titecarma Nov 28 '12 at 10:48
  • best contact details are on http://slodge.blogspot.co.uk/p/if-youve-got-questions.html – Stuart Nov 28 '12 at 15:44
  • although if you click on my name in StackOverflow then that also has some hints in it ;) – Stuart Nov 28 '12 at 15:45