title: Using web client in Silverlight
body: <p>I am having a heck of a time calling a RESTful service from within silverlight.  I am encountering this error:</p>
  
  <pre><code>{System.Security.SecurityException ---&gt; System.Security.SecurityException: Security error.
     at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
     at System.Net.BrowserHttpWebRequest.&lt;&gt;c__DisplayClass5.&lt;EndGetResponse&gt;b__4(Object sendState)
     at System.Net.AsyncHelper.&lt;&gt;c__DisplayClass2.&lt;BeginOnUI&gt;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)}
  </code></pre>
  
  <p>Which seems to be a popular error when using the webclient.  I have put in place a clientaccesspolicy.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
    &lt;access-policy&gt;
      &lt;cross-domain-access&gt;
        &lt;policy&gt;
          &lt;allow-from http-request-headers="*"&gt;
             &lt;domain uri="*" /&gt; 
          &lt;/allow-from&gt;
          &lt;grant-to&gt;
            &lt;resource path="/" include-subpaths="true" /&gt; 
          &lt;/grant-to&gt;
       &lt;/policy&gt;
     &lt;/cross-domain-access&gt;
    &lt;/access-policy&gt;
  </code></pre>
  
  <p>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.  </p>
  
  <pre><code>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
              ...
          }
  </code></pre>
  
  <p>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?  </p>
  
  <p><strong>Update:</strong>  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 <a href="http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx" rel="nofollow noreferrer">microsoft</a>.  What gives?  </p>