Stream closes after first reading on some XP computers

I have a desktop application that downloads some DLLs from a web server, and then loads them using Assembly.Load . On one XP machine, BadImageFormatExceptions were thrown a second time when I ran the application. On other Win 7 and XP machines, it worked fine.

It seems that only the first 65536 bytes of the dll are returned, and this only happens when the loaded dll has been cached.

What could lead to truncation?

This is how I reproduced the problem. First upload the file by adding it to the cache:

 client = new WebClient(); client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Reload); data = client.DownloadData(url); Console.WriteLine("Got " + data.Length); 

This works, in this case the print is "Got 159744".

Normally I would use RequestCacheLevel.Deafult , but to reliably re-create the problem that I read only from the cache for the second request:

 client = new WebClient(); client.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheOnly); data = client.DownloadData(url); Console.WriteLine("Got " + data.Length); 

On most machines this produces "Got 159744", however on an XP burst machine these outputs are "Got 65536". Considering the source code for WebClient , it uses a buffer size of 65,536 bytes. Suspecting a problem with WebClient , I reproduced the problem directly using WebRequest :

 request = WebRequest.Create(url); request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheOnly); responseStream = request.GetResponse().GetResponseStream(); var buffer = new byte[16384]; using (var memoryStream = new MemoryStream()) { int read; while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { Console.WriteLine("Read... " + read + " bytes"); memoryStream.Write(buffer, 0, read); } data = memoryStream.ToArray(); } Console.WriteLine("Got " + data.Length); 

In this case, only one line "Read ..." is output and only 16384 bytes are read. The end-of-stream exception is excluded, and the bytes I do look good.

It seems that the problem with the response flow ends too soon, but what could be causing this?

+4
source share
1 answer

There is platform inconsistency between your builds.

Most likely, the mix of "AnyCPU" and "x86" assemblies that will work fine on the 32-bit CLR, and not on the 64-bit CLR (since AnyCPU loads like 64 bits, which breaks the dependency on x86 builds).

Make sure all your builds have a consistent goal - all AnyCPU or all x86.

You can also try this.

  corflags myassembly.exe /32bit+ 

See http://msdn.microsoft.com/en-us/library/ms164699%28v=vs.80%29.aspx

0
source

All Articles