Cross-scheme Silverlight development

First of all, welcome to my brand new blog. I'll be posting bits of information on our products, my experiences with those products and general information which I find interesting to know and aren't necessarily easy to find on the web.

That said, let's get right into it with some basic Silverlight 2.0 development.

I've recently started dabbling in some Silverlight 2.0 development, to familiarize myself with the product and to explore the possibilities of Silverlight on the web and Microsoft Surface. One of the first things I wanted to try was to create an image rotator, that fetched images from the web and displayed them sequentially with some sort of cross fading mechanism. Sounds pretty simple, right? So I started out with a new Silverlight 2.0 project in Expression Blend 2 SP1 that contained a simple image with the following basic XAML:

<Canvas>

<Image Name="someImg"/>

</Canvas>

Next, I moved to the Page.xaml.cs to provide some code to actually display an image:

BitmapImage bImg = new BitmapImage();

bImg.UriSource =

new Uri("https://silverlight.net/Themes/silverlight/images/logo.jpg");

someImg.Source = bImg;

So, all was set to run the code to see the Silverlight logo appear in front of my eyes. I pushed F5 and...

The complete error message reads:

Line: 53
Error: Unhandled Error in Silverlight 2 Application SilverlightApplication1.xapCode 4001
Category: ImageError
Message: AG_E_NETWORK_ERROR

My first reaction, as you might have guessed, was "What?!". Seeing as how the error message didn't really provide any insight as to what the problem was, I started hunting around on the internet and came to the following article on MSDN: https://msdn.microsoft.com/en-us/library/cc189008(VS.95).aspx.

What's said here is that due to the security model of Silverlight, it's not possible to do cross-scheme communication with Silverlight. So in my case this means that I cannot reach resources on a HTTP type of URL from my local application, which is running in the FILE scheme. So, what to do? Well, the most obvious solution would be to run my Silverlight application on HTTP somehow, as this is going to be the case anyway once the application is deployed. I could just move the files to a web server every time I wanted to see the application in action, but that would hardly be the most effective solution. Since I'm using Visual Studio 2008 as my development environment, there's a better solution.

I came across the following workaround on Connect: https://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=293772. So the steps to follow are:

 

  • Add a new ASP.NET Web Application project to the solution
  • Right-click the Web Application project and select Properties
  • Select the Silverlight Applications tab and click Add
  • Select the Silverlight project in the solution and click Add (see screenshot below)

 

  • Two TestPage files will be created (.html and .aspx), I find it handy to rename the .aspx file to Default.aspx (removing the initial Default.aspx page first)
  • Set the Web Application as the StartUp project

This will ensure that when you run the application, the Web Application project will start up using the output of the Silverlight project as the actual item that is displayed. This way you run the application on the HTTP scheme and you shouldn't have any more problems with cross-scheme calls… until you want to access HTTPS resources.