WebClient usage from Silverlight not working fo me (Security?)
-
Hi, I have a strange problem I cannot seem to solve. I'm using, in Silverlight, and WebClient to invoke a REST service that returns an XML document. (the example below shows this document as being accessible via the url: "http://localhost/persons.xml" This is the callback code from a button defined in my XAML : Private Sub getdata_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Dim wcProxy As New WebClient() Dim Uri As New System.Uri("http://localhost/persons.xml") 'setup delegate AddHandler wcProxy.DownloadStringCompleted, AddressOf wcProxy_DownloadStringCompleted ' invoke service wcProxy.DownloadStringAsync(Uri) End Sub Private Sub wcProxy_DownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs) Dim doc As New XDocument doc = XDocument.Parse(e.Result) <<< ERRORS HERE <<<< ... ... End Sub The error on the line marked above says: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. and: e.Error.InnerException.Message says: "Security Error" Not sure why this is. In at atempt to fix this, I have defined a file called 'clientaccesspolicy.xml' in my ASP.NET Web Application, with te following contents: However - I'm not 100% sure if I need this at all, and even if I did, if I implemented it correctly. I've made no other changes to the default .WEB project that the Silverlight 'New Project' creates for me. Thanks - S.
-
Hi, I have a strange problem I cannot seem to solve. I'm using, in Silverlight, and WebClient to invoke a REST service that returns an XML document. (the example below shows this document as being accessible via the url: "http://localhost/persons.xml" This is the callback code from a button defined in my XAML : Private Sub getdata_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Dim wcProxy As New WebClient() Dim Uri As New System.Uri("http://localhost/persons.xml") 'setup delegate AddHandler wcProxy.DownloadStringCompleted, AddressOf wcProxy_DownloadStringCompleted ' invoke service wcProxy.DownloadStringAsync(Uri) End Sub Private Sub wcProxy_DownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs) Dim doc As New XDocument doc = XDocument.Parse(e.Result) <<< ERRORS HERE <<<< ... ... End Sub The error on the line marked above says: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. and: e.Error.InnerException.Message says: "Security Error" Not sure why this is. In at atempt to fix this, I have defined a file called 'clientaccesspolicy.xml' in my ASP.NET Web Application, with te following contents: However - I'm not 100% sure if I need this at all, and even if I did, if I implemented it correctly. I've made no other changes to the default .WEB project that the Silverlight 'New Project' creates for me. Thanks - S.
You should probably make sure e.Error is Nothing before trying to use e.Result in your DownloadStringCompleted handler. Are you showing the entire error strings returned in the e.Error exceptions? If this is a cross-domain issue... Your clientaccesspolicy.xml needs to be published/copied to the root directory of the website at the domain you are accessing. What's in your clientaccesspolicy.xml file? Here's one that should allow any Silverlight cross domain service calls:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*"/>
<domain uri="https://*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You should probably make sure e.Error is Nothing before trying to use e.Result in your DownloadStringCompleted handler. Are you showing the entire error strings returned in the e.Error exceptions? If this is a cross-domain issue... Your clientaccesspolicy.xml needs to be published/copied to the root directory of the website at the domain you are accessing. What's in your clientaccesspolicy.xml file? Here's one that should allow any Silverlight cross domain service calls:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*"/>
<domain uri="https://*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>Mark Salsbery Microsoft MVP - Visual C++ :java: