views:

152

answers:

2

In it's very basic form I have a WebClient request for some xml in a Page.xaml code behind. Something like:

    public Page()
    {
        InitializeComponent();

        Uri uri = new Uri("Dummy.xml", UriKind.Relative);

        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(uri);
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            //Do something
        }
    }

If I setup my Silverlight project to run through an asp.net hosted page, and then put Dummy.xml in the ClientBin folder (relative to the xap) it works fine.

If I setup the project using just the automatically generated test page option, and again put the xml relative to the xap, the request doesn't work (although the completed event does fire).

My question is why? Is it a requirement that any Silverlight project that dynamically downloads has to be on a server?

Cheers J

A: 

Yes, there is no webserver for it to connect to! The autogenerated test page just opens that XAP directly without invoking Visual Studio's web server. If you want to do this you must use the other option to create a website with the silverlight project. Alternatively, you can embed the XML file in the XAP as a resource and access it as a resource.

mattmanser
This does answer the question 'why'? Does WebClient have a dependency that there is a web server?
James Hay
+1  A: 

First up, try to avoid using the auto generated test page. It requires you to understand how the silverlight security by default model works when the xap is being accessed as a file.

To answer your question, you're encountering the security designed to prevent unauthorised cross-domain access.

Graeme Bradbury
lol... nice and clear error message they throw then :). Thanks for your help. I wonder how they handle this for desktop Silverlight apps then.
James Hay
OOB apps are in the browser sandbox for all intents and purposes. So when the desktop tries to call a webservice that webservice needs a cross domain policy for the web site the silverlight app was installed from.
Graeme Bradbury