Bug 46538 - System.Net.WebClient.OpenReadAsync/OpenReadTaskAsync do not work for file:// URLs
Summary: System.Net.WebClient.OpenReadAsync/OpenReadTaskAsync do not work for file:// ...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.6.0 (C8)
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2016-11-07 15:01 UTC by Patrick Dähne
Modified: 2016-11-20 10:39 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Test case (543 bytes, text/plain)
2016-11-07 15:01 UTC, Patrick Dähne

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

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