Bug 46538

Summary: System.Net.WebClient.OpenReadAsync/OpenReadTaskAsync do not work for file:// URLs
Product: [Mono] Class Libraries Reporter: Patrick Dähne <pdaehne>
Component: SystemAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: 4.6.0 (C8)   
Target Milestone: Untriaged   
Hardware: All   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test case

Description Patrick Dähne 2016-11-07 15:01:19 UTC
Created attachment 18354 [details]
Test case

System.Net.WebClient.OpenReadAsync/OpenReadTaskAsync do not work for file:// URLs. See the test case attached to this bug report. Compile the test code like this:

mcs -debug webclienttest.cs

and start it like this:

mono --debug webclienttest.exe webclienttest.cs

You'll get the following exception:

Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
  at System.Net.WebClient.OpenReadAsyncCallback (System.IAsyncResult result) [0x00000] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/System/net/System/Net/webclient.cs:1803 
  at System.Net.LazyAsyncResult.Complete (System.IntPtr userToken) [0x00047] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/System/net/System/Net/_LazyAsyncResult.cs:415 
...

The problem is the following line 1803 in the function "OpenReadAsyncCallback" in the file "mono/mcs/class/referencesource/System/net/System/Net/webclient.cs":

var lazyAsyncResult = (WebAsyncResult) result;

When using file URLs, the incoming result is not a System.Net.WebAsyncResult, instead it is a System.Net.LazyAsyncResult, therefore the type cast fails.
Comment 1 Marek Safar 2016-11-20 10:39:30 UTC
Fixed in master