Tag Archives: Silverlight

RIAServices – Proudly using SessionState within your DomainServices




As mentioned in my previous article How to build huge dynamically loading cross platform Silverlight Business Applications, I’m experimenting with the idea of a hybrid web application that has some parts implement as webforms, others as Silverlight.

A problem I encountered was that RIAService calls did not necessarily access to the same information as my webforms did.  An example is “SessionState”.  In the case of my company’s framework, the authentication system must be invoked prior to anything else within my domain services.  In addition the authentication system needs access to SessionState.


To do this I thought I could inherit from DataServiceFactory and add what I needed.  Unfortunately the class is marked as not inheritable.  The way around this is to encapsulate the class.

In the code sample below you can see an implementation of MyDataServiceFactory that encapsulates DataServiceFactory and returns an encapsulated HttpHandler that flags to ASP.Net that sessionstate is needed.


Imports System.Web
Imports System.Web.SessionState

Public Class MyDataServiceFactory
    Implements IHttpHandlerFactory
    Private moHandlerFactory As IHttpHandlerFactory

    Public Sub New()
        moHandlerFactory = New System.Web.Ria.DataServiceFactory
    End Sub

    Public Function GetHandler(ByVal context As System.Web.HttpContext, ByVal requestType As String, ByVal url As String, ByVal pathTranslated As String) As System.Web.IHttpHandler Implements System.Web.IHttpHandlerFactory.GetHandler

        Return New MyDataServicesHandler(moHandlerFactory.GetHandler(context, requestType, url, pathTranslated))

    End Function

    Public Sub ReleaseHandler(ByVal handler As System.Web.IHttpHandler) Implements System.Web.IHttpHandlerFactory.ReleaseHandler
        moHandlerFactory.ReleaseHandler(DirectCast(handler, MyDataServicesHandler).InnerHandler)
    End Sub
End Class
Public Class MyDataServicesHandler
    Implements IHttpHandler
    Implements IRequiresSessionState ' Let ASP.Net know that you need to use the session object

    Public InnerHandler As IHttpHandler
    Public Sub New(ByVal handlerToWrap As IHttpHandler)
        InnerHandler = handlerToWrap
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
            Return InnerHandler.IsReusable
        End Get
    End Property

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
        ' You can access context.Session here if you want to...
    End Sub
End Class


Of course to use this, you need to modify the web.config and change the two references to DataServicesFactory to point to your new class instead, which should be hosted in it’s own class library.


Share this post :


Silverlight “Name ‘InitializeComponent’ is not declared”


She'd be far less frutrated if she plugged it into the network first.

It is quite common in Webforms, Winforms and other environements to copy user interface views from one project to another.

When you copy xaml files from one Silverlight project to another you may experience the error:

“Name ‘InitializeComponent’ is not declared”


This happens because the “x:Class” directive in the top of the xaml file includes the project namespace.

So, open up your xaml file, and change this:




To the new project namespace:





I know this isn’t much of a post, but hopefully it will provide a useful search engine hit for the desperate.

Estranged Siblings – Silverlight Mobile and the Compact Framework CLR


An illustration that contemplates the cojoined nature of Silverlight and the Compact Framework


At PDC 2008 in the session Microsoft Silverlight 2 for Mobile- Developing for Mobile Devices, Amit Chopra revealed some interesting details about Silverlight Mobile that I haven’t seen written elsewhere.

The presentation mainly consists of “here’s Silverlight running on Windows Mobile”.   The interesting bits are at the end.  I dear reader, present these to you here.


Compact Framework, small but mighty

The compact framework, is small yet mighty.  (Some may say mighty ugly...by default)

The Compact Framework provides a great deal of rich device integration and sheer programmability. It is a pleasure to create software for.  I even think that Compact Framework based applications have been a reason for customers, particularly in the Mobile Fieldworker areas to adopt Windows Mobile devices.

If you’re a Compact Framework developer like me, you may have been frustrated by the default “yesterday” appearance of the System.Windows.Forms forms engine.  It takes a great deal of work to make forms look good. 

Silverlight, a Framework Apart?

Two frameworks, Silverlight and Compact Framework, so alike, yet so different.

I for one was very excited last year at the thought of a Silverlight (ie WPF) graphics engine being available for Mobile Device development.  I was then saddened to learn that the Silverlight runtime would not be extending the Compact Framework, but would exist in addition to Compact Framework.

This is disappointing news. (I am now showing my disappointed face….beware!) It appears that Microsoft (at the moment) seem determined to give Silverlight minimal access to the device.  This is aligned with the idea that Silverlight is only a competitor to traditional Flash app that runs from a Web page. 

Although this may be acceptable for certain kinds of desktop oriented Silverlight applications, I doubt this is a believable use case for mobile developers.  The power of mobility is not only the ability to access data anywhere, but the ability to talk to the features of the device.

In the presentation mentioned at the outset, there was declaration of the intention to support Webcams, accelerometer and other device features in the future, depending on the support for this on other platforms (eg Windows and Mac). 

So, we are restricted to the lowest common denominator.  You may believe this is for technical reasons.  It isn’t. Because….

Silverlight Uses the Compact Framework!

Diagram of Silverlight Architecture on Windows Mobile

Amit said that the Silverlight runtime for Windows Mobile may be smaller because it may omit certain codecs and it also shares the Compact Framework Clr.    He went on to say that the Compact Framework would be a prerequisite to installing the Silverlight runtime.

A new and nobler purpose for Silverlight Mobile

I give you....Silver-Knight

It’s Microsoft’s belief that targeting Windows Mobile 6.? (Sorry about that WM2003 and 5) and Nokia phones will mean Web users will eschew DHTML and Flash websites for Silverlight ones.  

With the proliferation of device operating systems (particularly that pesky iPhone) I don’t think this is a likely scenario.  Do you?  Well do you?

I’ve I got an idea!  As it’s unlikely Microsoft will backtrack on their “one platform everywhere” philosophy for Silverlight, why not, in addition enable Silverlight as a cutdown WPF for the Compact Framework on Windows Mobile? Microsoft, I know you can!  Doing this would earn the gratitude of all Compact Developers and provide a side benefit to their current Silverlight efforts on Mobile.

Call to Action

If you agree, please add a comment or bookmark this article using the links below, blog about this yourself or email the people at Microsoft.   Amit said they wanted feedback, lets give it!

(BTW Any physical resemblance between Amit and myself is entirely coincidental, so don’t say it)



Microsoft Silverlight 2 for Mobile- Developing for Mobile Devices

Amit Chopra’s Blog (Email) – WIndows Mobile Program Mananger

Giorgio Sardo’s Blog (Email: gisardo @ microsoft  com) – Evangelist for Windows Mobile


Share this post :

Automatically stretching a Silverlight 2 control on a webpage



A couple of fun hours were spent trying to get my Silverlight content to resize when the user resized the browser. 

I mean, what’s the point of having your Silverlight content replace HTML if it wont flow and resize when the user resizes the browser? I want my Silverlight content to take up the whole browser frame.

The Html Code

You’d think this would be the default behaviour for an asp.net tag definition like this:

<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TechnologyOneCrm.xap" Version="2.0" Width="100%" Height="100%" />


Sadly this is not enough.  Your silverlight sits there in a dumb un-resizeable box.

The Xaml

When you create a Silverlight 2 control in Visual Studio 2008 or blend, the UserControl typically has Height and Width properties set, something like this:

<UserControl x:Class="Daphne.WorkplaceHome"
      Width="900" Height="400" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <Grid x:Name="LayoutRoot" Background="White" >


The Elusive Auto

Hence the reason why your content is not resizable.

One solution is to remove the Width and Height property.   Another is to set them to “Auto”, like so:

<UserControl x:Class="Daphne.WorkplaceHome"
      Width="Auto" Height="Auto" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <Grid x:Name="LayoutRoot" Background="White" >


If you run the project, it will start resizing.  Unfortunately this causes the designer in Blend and VS2008 look like rubbish.  (or more rubbish than usual anyway).

The trick is to set the Width and Height properties to “Auto” after the the screen has been rendered.


In WPF (apparently) you can use code like this:

Partial Public Class Page
    Inherits UserControl
    Public Sub New()

        Me.Height = new System.Windows.LengthConverter().ConvertFromString("Auto")
        Me.Width =  new System.Windows.LengthConverter().ConvertFromString("Auto")
    End Sub



Sadly, in another twist of fate, “LengthConverter” does not exist in Silverlight.

The Silverlight Solution – Who’s your Nan?




Fortunately, browsing the type library showed me that setting the property value of Height or Width to “System.Double.Nan” has the same effect as “Auto”.

So, in the intialise of your Silverlight control, put this:


Partial Public Class Page
    Inherits UserControl
    Public Sub New()

        Me.Height = System.Double.NaN
        Me.Width = System.Double.NaN
    End Sub


I am not sure if Microsoft intend to support LengthConverter (or TypeDescriptor.GetConverter!) in a future Silverlight version, but I’m guessing not.



Share this post :

Problem calling a Web Service from Silverlight 2



Apparently the “System.ServiceModel.ProtocolException” error is common in the fledgling world of Silverlight 2 and Web Services.


In my case I tried to reference a Web Service that my company created and was disappointed with this entertaining message:


System.ServiceModel.ProtocolException’ occurred in System.ServiceModel.dll but was not handled in user code Additional information: [UnexpectedHttpResponseCode]Arguments:Not Found
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.ServiceModel.dll&Key=UnexpectedHttpResponseCode


What this meant in my case is that I needed a “crossdomain.xml” file placed in the root folder of the server I was accessing.


For a development server, this could mean something like this:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
  <allow-access-from domain="*" />


This gives all applications (Silverlight, Flash) no matter where they’re from to access any web applicaitons on your web host.


As an ISV I find this a bit annoying, as it means updating a file that is outside of the Web Application (in the Web Root).


It would be nice to be able to configure this information within the virtual directory of the application itself.




Combining Silverlight 2 content from multiple web applications in VS2008



In a project I’m doing I need to create a web application that can consume Silverlight 2 RIA content from other web applications.

As it turns out, this is pretty easy to do.  All you need to do is reference the xap in the other web application from your page and it works exactly the same as if you were using an xap from within your own web application.

A Problem with Debugging

One problem is that Visual Studio tries to run the two web applications on different ports, which makes it impossible to reference the other web app directly.  You can’t use a relative url:

<form id="form1" runat="server" style="height:100%;">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div  style="height:100%;">
        <asp:Silverlight ID="Xaml1" runat="server" Source="../Crm_Web/ClientBin/TechnologyOneCrm.xap" Version="2.0" Width="100%" Height="100%" />

Therefore you need to set the web application you wish to point to on a static port, like this:


You can then access the web application directly:

<form id="form1" runat="server" style="height:100%;">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div  style="height:100%;">
        <asp:Silverlight ID="Xaml1" runat="server" Source="http://localhost:2000/Crm_Web/ClientBin/TechnologyOneCrm.xap" Version="2.0" Width="100%" Height="100%" />

There’s probably many better solutions to this problem, but I thought I’d post my workaround.

Silverlight 2 has built in Unit Testing



As announced by Scott Guthrie in Mix08, and now on his blog, Silverlight 2 incorporates visual unit testing.  

One of the motivations for an MVC/MVP architecture for me is better unit testing of ui logic.  Sometimes it is very difficult to created automated visual tests for some platforms, such as Winforms. 

It appears (from the demo) that Silverlight allows pretty good testing of the actual user interface.    Probably more effective than testing ui logic with mock views.

Food for thought.  Perhaps this should affect the way we think about testing frameworks.

If you’re interested, take a look at this good (and short) video demo which gives you an overview of the whole thing:

Video walkthrough of the Silverlight 2 control unit tests – Jeff Wilcox



Open Source automation frameworks for Winforms and other Win32 Apps


Share this post :

Showing a MessageBox in Silverlight 2.0 Beta 1



I noticed a post on Technorati that claimed that a Msgbox (or MessageBox) capability wasn’t available Silverlight.

Well, that’s not exactly true.  It’s just in a place that isn’t so obvious:


System.Windows.Browser.HtmlPage.Window.Alert("Hello World")


I find this interesting because it shows how integrated Silverlight is with the browser experience.

This is a good thing for creating browser based applications.  The goodies in System.Windows.Browser provide a rich programming model for web page integration.

Some hope that Silverlight will be used for creating cross platform desktop style applications.  It’s close integration with the browser seems to indicate that this is not a scenario they’re catering for, at least initially.

(Although I suppose a full screen browser window would equate to the same thing…)




Share this post :

Astonishing Silverlight 2 Facts


Shock astonishment and suprise at the revelations from Scott Gui about Silverlight 2.0

In case you’ve been under a rock, you’ll want to know that Scott Gu has posted about some specific features of Silverlight 2.0.  It’s an interesting post and well worth reading.

What’s also worth reading, that you might have missed, is Scott’s replies to questions in the very long contents section.

I think you’ll find these replies enlightening, so I reproduce them here for your perusal:

Cross Platform Support

>>>>>> Scott, so what about Linux? Silverlight could not fight with Flash now because of lack of Linux support. What are plans to make Silverlight really crossplatform?

All of the Silverlight 2 features will also be available on Linux.  We are partnering with Novell to build a compatible SL 2 implementation for Linux.

>>>>>>> Is there any information on Windows 2000 support for Beta 1?

Yes – Silverlight 2 supports Windows 2000 starting with Beta1.

>>>>>> When is silverlight coming for the mobile platform? Such as WM 6.0?

Silverlight will also be coming to mobile devices.  We’ll have more details on that in the future.

>>>>>> Great post, i’m really looking forward to Silverlight 2.  When Silverlight 2 is released will it be backwards compatible with applications built on top of Silverlight 1.0?

Yes, SL 2 is backwards compatible with SL 1 applications.

Cross Browser Support (Firefox, Opera and Safari!)

>>>>>> Any news on opera and safari windows support? I’m a diehard opera user and can’t view any silverlight apps yet. This also stops me from using silverlight yet, although I like it like 1000 times more than flash.

We are planning to support Opera and Safari on Windows in the future (they kinda work now – but we haven’t done a full test pass on them for beta1).  It is definitely something we are looking to-do in the future though.

>>>>>> Will this upcoming Silverlight Beta have support for Firefox 3.0 Beta 3?

FireFox 3.0 will be supported by SL 2 when it ships.  They recently changed how rendering works for plugins with their recent beta, and we know we have some issues with that with Beta1.  We’ll be fixing them in Beta2 though.

>>>>>> Thanks Scott for the quick response. One last question 🙂 Is UIAutomation going to work inside Firefox though? I mean can you access the UI element from within the Firefox parent window…

Yes, I believe so.  I think we support the standard Windows automation apis (which enable screen readers and testing automation for both IE and FireFox).

Unit Testing

>>>>>>>1a. How is unittesting supported?

We’ll be supporting a unit testing framework with Silverlight 2.  I’ll blog more details on this in the future.

>>>>>>>2a. How can we do coverage tests on client side?

We will have tools to capture code coverage.

Model View Controller

>>>>>>> 3. Will it be possible to use the MVC framework in any way along with SL code?

You could have your ASP.NET MVC views be SL based pages.  Silverlight’s WPF UI framework also supports pretty nice model/view separation, which enables you to decouple UI from state/data/actions.  This combined with the unit testing support enables a pretty clean way to-do TDD.

Designer Support

>>>>>> From the tutorials, it looks like there is no designer support (yet). Everything needs to be typed in.

We’ll be shipping designer support for Silverlight with Beta1 as well.  I’ll blog about this in the near future (I think you’ll like it <g>).


>>>>>>> Does it support simply chinese character? Silverlight doesn’t support.

Yes – SL 2 supports asian fonts and IME support.  It can also use the system fonts and doesn’t require font packages to be downloaded.


>>>>>> what happened to the DLR support from 1.1 that never really shipped.

The DLR is available for download today (and has been for several months), and can be used with full .NET Framework applications.  We’ll also have DLR support in Silverlight 2.0 Beta1.

Miscellaneous Cool Things

>>>>>>> You mention in the tutorial that some controls will need to be shipped in the ‘XAP’ file.  Will there be any provision to cache these files using some form of URL in the assembly resolver?  

Yes – you can partition your application into multiple .xap files.  They can then be cached at different levels of granularity if you want.

>>>>>> Scott, please confirm Silverlight 2.0 will support cross-domain access. Also, when is it going to be released?

Yes – SL 2 supports cross domain network access.  Tutorial #3 of my series above shows an example of it in action.

>>>>>> What will Silverlight 2 offer in terms of accessibility, e.g. AAA or Section 508 compatibility?

Silverlight 2.0 supports accessibility and will work with screen-readers.  You’ll be able to build Section 508 and AAA sites with it.



The original post : First Look at Silverlight 2 – ScottGu’s Blog

Share this post :