views:

959

answers:

1

Im trying to write to a form from an asynchronous call. The examples I seen on line show that this should work but I keep getting an error.

First the call that is made though the Disbatcher never calls p. Second i get a System.Security.SecurityException on the call req.EndGetResponse(a);

What could be causing the problem?

public partial class Page : UserControl
{
    Uri url = new Uri("http://www.google.com", UriKind.Absolute);
    HttpWebRequest req;

    private delegate void PrintToUIThread(string text);
    PrintToUIThread p;

    public Page()
    {
        InitializeComponent();
        req = (HttpWebRequest)WebRequest.Create(url);
        p = new PrintToUIThread(print);
        req.BeginGetResponse(new AsyncCallback(WebComplete), req);
    }

    void WebComplete(IAsyncResult a)
    {
        try
        {
            Dispatcher.BeginInvoke(p);
            HttpWebRequest req = (HttpWebRequest)a.AsyncState;
            HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
            Dispatcher.BeginInvoke(p);
        }
        catch (Exception ex)
        {
            print(ex.ToString());
        }
    }

    private void print(string text)
    {
        PageTextBox.Text = text;
    }

    private void print()
    {
        PageTextBox.Text = "Call From Invoke";
    }
}
+1  A: 

There's nothing inherently wrong with your request/response code from what I can see. You're probably running into the security exception because of the cross-domain restrictions implied by google's crossdomain.xml file.

This is Google's crossdomain.xml:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"&gt;
<cross-domain-policy>
  <site-control permitted-cross-domain-policies="by-content-type" />
</cross-domain-policy>

All this is really saying is "Hey, RIA app, use a proxy". Basically, set up a WCF or ASMX web service call with your "friendly" server with a clientaccesspolicy.xml file in place, use your server to make the http request, then deliver the results back. I'm not not sure if google supports JSONP, but you could try that as well.

Daniel Crenna