tags:

views:

1540

answers:

5

I am having a heck of a time calling a RESTful service from within silverlight. I am encountering this error:

{System.Security.SecurityException ---> System.Security.SecurityException: Security error.
   at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
   at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)}

Which seems to be a popular error when using the webclient. I have put in place a clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?> 
  <access-policy>
    <cross-domain-access>
      <policy>
        <allow-from http-request-headers="*">
           <domain uri="*" /> 
        </allow-from>
        <grant-to>
          <resource path="/" include-subpaths="true" /> 
        </grant-to>
     </policy>
   </cross-domain-access>
  </access-policy>

and I have watched the silverlight in fiddler and it does make a request to the web site and does get a 200 status back.

public void login(string userName, string password)
        {
            WebClient client = new WebClient();
            Uri uri = new Uri(serverURI + "/clientaccesspolicy.xml");
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(login_Complete);
            client.OpenReadAsync(uri);
        }

        private void login_Complete(object sender, OpenReadCompletedEventArgs e)
        {
            byte[] buffer = new byte[e.Result.Length]; //crashes here with exception
            ...
        }

I am more or less out of ideas. Anybody know what I'm doing wrong? Is there some issue with running the silverlight directly from a file:// uri?

Update: I deleted the clientaccesspolicy.xml file and kept just the crossdomain.xml file in place and bingo everything worked. That makes me believe that the error is in the clientaccesspolicy file but I copied that directly from microsoft. What gives?

+1  A: 

You can't request content from a website if your Silverlight application is running from a file:// URL.

For more information, see URL Access Restrictions in Silverlight.

X-Cubed
That seems logical but it doesn't really explain why removing the clientaccesspolicy.xml file fixed it.
stimms
A: 

This may help you: Some tips on cross-domain calls which was posted by the Silverlight Web Services Team on their blog.

JonnyBoats
A: 

Mmm...

Try with this cross domain file

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
    <cross-domain-access>
     <policy>
      <allow-from http-request-headers="SOAPAction" >
       <domain uri="*"/>
      </allow-from>
      <grant-to>
       <resource include-subpaths="true" path="/"/>
      </grant-to>
     </policy>
    </cross-domain-access>
</access-policy>
Braulio
A: 

I just spent 3 hours looking into this very issue. The cross domain access policy and client access policy files were a dead-end for me. Nothing would work. Then finally I ran into a post on the Silverlight.net forums by a Microsoft employee that helped me fix the issue.

The answer, at least in my case was the test webpage that Visual Studio generates when you create a new Silverlight application.

Basically you get two options when you start a Silverlight project. The first option will generate an html page dynamically when you run your application. The second option will create a separate ASP.NET project that will host your Silverlight application. If you choose the first option (dynamic test page) you will not be able to do any cross domain requests, even if both your projects are on the same box it will somehow consider this a cross domain call and fail (I am not sure why)

Create another Silverlight project, choose the second option, and move your XAML files in. That should fix your issue.

Adam Berent
A: 

X-Cubed is correct, you cannot do cross-domain requests from file://, as Adam Berent pointed out, this means if you use the TestPage generated by visual studio your network requests will fail.

A workaround is to launch the TestPage using Chiron (usually used for dynamic languages) to serve it (because then the access is on http://) or off a development web server.

The catch is you actually have to attach the debugger manually to the browser in order to debug with networking (you can't just hit F5.)

Eloff