R – Silverlight’s WebClient isn’t connecting to the server

asp.nethttpwebrequestsilverlight-2.0webclient

I've got a problem here.

I've got an ASP.net website hosting a silverlight 2 application.
I'd like the site to communicate to and fro from the silverlight app, and I'm doing this via http requests. Incidentally, if anyone knows a better way, please do tell me.

My server's got the following http listener set up. I copied this from a tutorial site somewhere, since it's mainly experimentation at the moment :

      HttpListener listener = new HttpListener (  );
      listener.Prefixes.Add("http://localhost:4531/MyApp/");  
      listener.Start(  );                                         

      // Wait for a client request:
      HttpListenerContext context = listener.GetContext(  );

      // Respond to the request:
      string msg = "You asked for: " + context.Request.RawUrl;
      context.Response.ContentLength64 = Encoding.UTF8.GetByteCount (msg);
      context.Response.StatusCode = (int) HttpStatusCode.OK;

      using (Stream s = context.Response.OutputStream)
      using (StreamWriter writer = new StreamWriter (s))
        writer.Write (msg);

      listener.Stop(  );

I'm using the following code to send a request :

 private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            b.Content = "Hello World";

            Uri serviceUri = new Uri("http://localhost:4531/MyApp/");
            WebClient downloader = new WebClient();
            downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(TestDownloadStoriesCompleted);
            downloader.DownloadStringAsync(serviceUri);

        }
        void TestDownloadStoriesCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                TextBox1.Text = e.Result;
            }
        }

My problem is that I can connect to the webserver from a console application using pretty much the same code (I tested it by setting a breakpoint in the code), however nothing happens when I click the button in silverlight. (I've added the "Hello World" to test that I am indeed connecting the delegate to the button.)

I've read that silverlight needs policies to connect via webclient, but it shouldn't be the case if I'm using the same server and the same domain for both the server and the silverlight application!

Thanks for all your replies!

EDIT : I am recieving this exception :

System.Security.SecurityException —> System.Security.SecurityException: Security error.

Also, based on what I'm reading apparently to be site-of-origin, the deployment URI of the xap and the request URI must also be of the same port.

However, when I set the properties for the server to be hosted on a specific port, and I set the listener to listen to that same port, it fails with the message that The process cannot access the file because it is being used by another process. I assume it is because the http listener can't listen to the same port being used to host it 😐
But then how can I make Silverlight perform host of origin webclient requests?

Best Solution

Since this is only a test add an "else TextBox1.Text=e.Error.ToString();" in your TestDownloadStoriesCompleted handler to see what error you get.

EDIT:

You can't host both the asp.net app and your listener on the same port - you could fix this by using a different port and serving a clientaccesspolicy.xml from your httplistener.

However I think it would make more sense for you to take a look at WCF web services (you add the svc to your asp.net app). Here's a sample.