I’ve been digging through a fair number of my old Silverlight demos/projects and been bashing into a fair number of “upgrade issues” when opening my older code. The two changes that have brought this problem to light is the transition from Silverlight 2 to 3 and a shift in the Silverlight SDK.

Referenced assemblies missing Silverlight?

Consider the example on the left, which is the hosting website for a ‘older’ Silverlight project. The project would have been created (probably at the same time when you created a new Silverlight project) as a ASP.NET website with a whole bunch of files and References to aid you on your way.

Of note; In older “ASP.NET with Silverlight” websites the template would have added a reference to System.Web.Silverlight, added a Silverlight.js file, and a couple of generated page examples (one as an aspx file and the other as plain HTML). An empty Default.aspx page is a hangover of the regular ASP.NET website/project template.

So, why is there a broken reference (System.Web.Silverlight) after I ran the upgrade project tool? Well, the project upgrade tool actually ran for the Silverlight project itself and not for the hosting ASP.NET website. Therefore it is up to you to sort it out! I don’t have any of the previous Silverlight runtimes, tools or SDK installed and so the System.Web.Silverlight assembly has been moved (both physically and in version number).

Basically, the latest version of Silverlight SDK removed the ASP.NET Silverlight control in favour defining the Silverlight area via a HTML object or via JavaScript embedding functions. Therefore any aspx files that declare a element will need to be revisited because the reference to System.Web.Silverlight is indeed dead now and also needs to be removed.

Even though we have lost the ability to use the ASP.NET Silverlight element in our ASP.NET pages, it is very easy to refactor our ASP.NET code by removing the offending code blocks and adding in either the HTML object notation or via JavaScript embedded functions.

For example —

In the of the HTML markup:

<style type='text/css'>
    #silverlightControlHost { height: 100%; }
</style>
<script type='text/javascript' src='Silverlight.js'></script>
<script type='text/javascript'>
    function onSilverlightError(sender, args) {

        var appSource = '';
        if (sender != null && sender != 0) {
            appSource = sender.getHost().Source;
        }
        var errorType = args.ErrorType;
        var iErrorCode = args.ErrorCode;

        var errMsg = 'Unhandled Error in Silverlight Application ' +  appSource + 'n' ;

        errMsg += 'Code: '+ iErrorCode + '    n';
        errMsg += 'Category: ' + errorType + '       n';
        errMsg += 'Message: ' + args.ErrorMessage + '     n';

        if (errorType == 'ParserError')
        {
            errMsg += 'File: ' + args.xamlFile + '     n';
            errMsg += 'Line: ' + args.lineNumber + '     n';
            errMsg += 'Position: ' + args.charPosition + '     n';
        }
        else if (errorType == 'RuntimeError')
        {
            if (args.lineNumber != 0)
            {
                errMsg += 'Line: ' + args.lineNumber + '     n';
                errMsg += 'Position: ' +  args.charPosition + '     n';
            }
            errMsg += 'MethodName: ' + args.methodName + '     n';
        }

        throw new Error(errMsg);
    }
</script>

In the of the HTML markup:

<!-- Runtime errors from Silverlight will be displayed here.
This will contain debugging information and should be removed or hidden when debugging is completed -->
<div id='errorLocation' style='font-size: small;color: Gray;'></div> 

<div id='silverlightControlHost'>
    <object data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='100%' height='100%'>
        <param name='source' value='ClientBin/Demo.SL.xap' />
        <param name='onerror' value='onSilverlightError' />
        <param name='background' value='white' />
        <param name='minRuntimeVersion' value='3.0.40307.0' />
        <param name='autoUpgrade' value='true' />
        <a href='http://go.microsoft.com/fwlink/?LinkID=141205' style='text-decoration: none;'>
             <img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/>
        </a>
    <object>
    <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>

This is all well and good, but looking at the updated ASP.NET website, I wanted to take it one step further…. what is the bare minimum configuration required by a ASP.NET website?

Extending the solution I alluded to above – I created a new ASP.NET Web Site with the following Visual Studio template:

Adding an ASP.NET web site

This skeleton project includes a default.aspx page, but nothing else – at least it compiles! Note, that there is no References section in the Solution Explorer – that belongs to the ASP.NET Web Application template.

Barebones ASP.NET web application

Visual Studio will allow you to link your Silverlight project to the new ASP.NET website if it is part of the solution (that’s why I extended the example solution above). To do this, open the project’s Property Pages which are accessed by right-clicking the website root and select Property Pages right at the bottom of the context menu. Select the list item option named Silverlight Applications.

Silverlight Applications in the project property pages

Clicking the Add button will pop up the Add Silverlight Application popup which will hopefully default to the appropriate Silverlight project in the solution. I’ve highlighted that in the screen grab below. Also, ensure that Silverlight Debugging is enabled – which will modify the web.config and prove extremely useful when your application throws a spanner.

Add Silverlight Application dialog

A wizard will run through adding all the relevant bits into the website which to no surprise, creates a couple of test pages (one HTML, the other ASP.NET) and a corresponding Silverlight.js file. The default.aspx file existed from before and remains untouched, so I usually delete it and rename the test aspx file to become the default.aspx page instead.

The new website files

The end result is, the same as I had before though. I thought that there may have been some sort of further optimisation, but there is not. One of the main differences you might have spotted is the lack of added referenced assemblies. References are still part of the website, they just lurk in the project’s Property Pages instead. You’ll then find that there is a list item called References such as that shown below. There are fewer referenced assemblies listed here than there are in a typical ASP.NET Web Application (as shown at the beginning of this article) but this does not necessarily impact any deployment since the .NET Framework includes all these assemblies, it is just that the ASP.NET website is not making them available to code at runtime (in a sense it is giving you a deployment buffer).

References in the project property pages

To be honest, I would normally stick to using a ASP.NET Web Application project since it has nicer property pages and also works a bit neater with ClickOnce and publishing features of Visual Studio – but that’s just me 🙂

This is a cross post from my EMC blog, mainly for backup duplicity and to aggregate some of my past postings. My EMC blog used to be under the Conchango brand but was acquired by EMC so I’ve also retrospectively refreshed some of the old links and maybe a tweak a bit of content too. permalink to the original post here