I recently was working on a Silverlight 2 project and needed to "phone home" from the .xap back to the server of origin to get a text file from the server. It wasn't completely obvious how to do this at first, so I figured I'd share the code I wrote if others need to do something similar. Note a couple things:

  1. You'll need to add a reference to System.Net.dll and using statements for System.Net and System.IO.
  2. The ASP.NET WebDev server used for development will run the project on a random port. You can change this behavior so you have a consistent port number. Click on the project node in Solution Explorer and open the properties window (press F4). Change the “Use dynamic ports” property from True to False and edit the port number.
  3. If you try to go to a server other than the server of origin, you'll most likely run into cross site scripting issues.

 

    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //you may need to change this path depending on your web server
            WebRequest request = WebRequest.Create(new Uri("http://localhost:4386/io_web/textfile.txt"));
            request.Method = "GET";
            request.ContentType = "text/plain";
            request.BeginGetResponse(new AsyncCallback(ResponseReady), request);
            
        }

        void ResponseReady(IAsyncResult asyncResult)
        {
            WebRequest request = asyncResult.AsyncState as WebRequest;
            using (WebResponse response = request.EndGetResponse(asyncResult))
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream);
                    string s = reader.ReadLine();
                    System.Diagnostics.Debug.WriteLine(s);
                }
            }
        }        

    }
Posted on March 31, 2008 09:39
Actions: E-mail | Comments (5)

Comments


John Stockton

John Stockton

April 1, 2008 20:38

I did the same thing recently and another solution for item 2 in your list above is to use a virtual directory in IIS.  This way you always know the full virtual path to the file you need.
          


Tran Thanh Luong

Tran Thanh Luong

April 18, 2008 10:23

+ Read file:
I have a binary file (content: float number) in the same web directory on the webserver. I want to read this file into the array for drawing some shapes using Silverlight 2.0. Have you give me a sample.
+ Write file:
I want to write information into a binary file on webserver using Silverlight 2.0. How to do?

Please help me! Thanks.
          


Parimal

Parimal

April 22, 2008 09:00

Hi,

I used to above code to read the Text file in the Silverlight APplication and the Text File is part of the Silverlight Hosting Web Project.

It runs fine in IE but doesnot work in Fire Fox 2.0.

Any solution,
regards
Parimal
          


Markul

Markul

April 29, 2008 23:10

Got the same problem with FireFox 2.0 (
          


Ole

Ole

October 24, 2008 14:59

I would suggest using webservices for this. this way you can do waaay more powerfull stuff.
          


Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.