System NotSupportedException The URI prefix is not recognized

by Trent Guidry8. January 2011 03:42
I found a work around for that "The URI prefix is not recognized" System NotSupportedException that can occur when using a WebClient after turning on then off Silverlight 4’s "enable running application out of the browser" setting.
To see this issue, create a Silverlight 4 application.
Paste the following code into the MainPage.cs file:
public MainPage()
{InitializeComponent();Loaded += (s, e) =>{Uri uri = new Uri("test.txt", UriKind.RelativeOrAbsolute);WebClient webClient = new WebClient();webClient.DownloadStringCompleted += (s2, e2) =>{if (e2.Error != null && e2.Error.InnerException != null){MessageBox.Show(e2.Error.InnerException.Message);}};webClient.DownloadStringAsync(uri);};
If this project is run, it gives an exception "The remote server returned an error: NotFound.", which is OK, since I didn’t create a file called test.txt.
If I then go to the Silverlight 4’s project settings and turn on “Enable running application out of the browser” and run the application, I get the same exception.  That is also expected and OK.
However, if I go back to the Silverlight 4’s project settings and turn off “Enable running application out of the browser”, I now get an exception of “The URI prefix is not recognized.”  That is unexpected and not OK.
After digging into it a bit, I found the following work around.  Right click the Silverlight 4 project and select Unload.  Right click on the now unavailable project file and click edit.  In that file there should be a section that looks like that shown below.
<ItemGroup><None Include="Properties\OutOfBrowserSettings.xml" /></ItemGroup>
Comment that out or replace it with the code below.
<!--<ItemGroup><None Include="Properties\OutOfBrowserSettings.xml" /></ItemGroup>-->
Save the file.  Right click on the project file and select Reload.  Click Yes to the dialog box. 
Rerun the application and it should now give the original exception again.

Tags: ,