Rob Smyth

Wednesday, 28 December 2011

Migrating from Silverlight to WPF

I've just completed migrating a Silverlight application to WPF. All up it took a few days. Not difficult, mostly tedious, but it become evident that some preparation and knowledge helps. So here are my notes.

The application's vital statistics:
  • Silverlight 4
  • Visual Studio 2010 with Resharper 6
  • Several VS Silverlight projects with multiple common library assemblies
  • MVVM
  • No Silverlight IoC used
  • Automated UATs using White (Silverlight) framework
  • Telerik RadControls using charting, ribbon bar, data grid views, etc
  • Highly interactive UI
  • Multiple WCF services
  • Entity framework & SQL server back end


Changes and research needed prior to changing references to the WPF framework ...

Do a spike
I found a spike run useful. I converted a few base assemblies until I got to one that had a Silverlight control (e.g. a user control or page). This took a few hours and help greatly in planning the real attack. Discard the spike, you will do it better the second time.
Assembly Dependencies
Understand your application's Silverlight assembly dependencies, you need to work up from the bottom.
WPF reference assemblies
Do some research into Microsoft's WPF/Winforms reference assemblies. Referencing the wrong library is just too easy and can be time consuming to correct. Both WPF and Winforms have TextBox controls and Resharper is great but not a mind reader (yet). Take care when adding references.

e.g. PresentationFramework.dll, WindowsBase.dll, PresentationCore.dll
Clean-up orphaned files
Sometimes files are removed from an assembly without being deleted or removed from the repository. You need to make sure these 'orphaned' files are removed before migrating. Select each project in turn and click on the show all files button in Visual Studio. Delete the orphans from your disk and the repository.
Remove all web page navigation before you start the migration proper. This may be the biggest part of the job, the application needs to behave as one application rather than multiple web pages.

Search your code for 'href', 'Uri', 'MappedUri', and 'NavigationService'. These must go.
Web specific controls
In our case we had a RadControls HtmlPlaceholder which we used to display PDF files. This was removed prior to migrating the code and a PDF reader added post code migration. I could not see a refactoring option here.

Code Migration

Getting the code to compile as a WPF application ...

Telerik RadControls
Possibly the easiest part. Got the WPF version and it compiled with very few differences. Actually, I do not remember any compile time changes. Nice one Telerik.

Note: Mitigated the cost of buying new WPF licenses by timing the change close to our Silverlight licenses renewal. Tell the bean counters the new license costs are X but this is offset by dropping the cost of license Y making our cost ....

However there were a few run-time behaviour differences which were probably base WPF framework differences. I list these later.
Migrating assemblies
The procedure to migrate an assembly, working up the reference hierarchy was:
  1. Add a new dot net assembly (not Silverlight) with the same name as the one being replaced but with a clear suffix like "_X". Important: Tell VS to create it in the same folder as the existing assembly.
  2. Remove the newly created assembly from the solution.
  3. VS has created a folder for the new project in the existing assemblies folder. Navigate to that folder and move the project file down into the same folder as the existing project.
  4. Add the moved project to the solution. As VS sorts projects alphabetically you will find in just below the one your replacing.
  5. Copy the project's name space from the original project (do not change it until the entire migration is finished). Set the assembly name to be the same but with a suffix like the project name.
  6. Add a reference to, as a minimum, PresentationFramework.dll, WindowsBase.dll, PresentationCore.dll.
  7. Select the new project and click on "show all". Select all of the source files and folders and add them to the project until the new project has the same files and folders as the original project.
  8. Try to compile the new project. You will need to change used namespaces. Typically the change will be to half a dozen namespaces in many files. So you will learn to do a search and replace (within the one project). This will break the compile of the original assembly but that does not matter so much.
Keep the old project in the solution until the migration is finished, you may need to navigate to solve errors in the migration, etc.
Migrating WCF service references

I did not find a way to migrate WCF service references. Create new ones in the new Dot Net assembly.

Run Time (Post code migration)

I did find some breaking differences. These were:
  • Had to change a few Page controls to UserControls. WCF class hierarchy is a bit different. The compile error is clear and it was not difficult to do.
  • The initialisation sequence is a little different. I need to move some initialisation code to different event handlers. I do not remember the details. This did take a few hours and was hit and miss. On one case I do remember I had to move some initialisation to an OnActivated event and add an "initialised" state. I'm sure that is not the intended way, but it worked.
  • I had one binding that did need considerable rework. The binding was to an indexer. Looked like a control binding defect, should have worked. This one also took a few hours to resolve.

It proved viable to migrate our Silverlight application to WPF with few problems. Medium complexity job.

Code conversion took one developer a week. Planning and changes to the application are required before changing references to the WPF framework. Risk is low-moderate provided that your libraries, like Telerik, have a common WPF and Silverlight API.

However, defects were introduced. Most had very high visibility (ribbon bar blank) so are relatively low risk. A few were changes in control behaviour so rigorous post migration testing is required. So allow the same time for testing and introduced defect fixing as was required for the code conversion (pre-testing) stage. But ... if you have good automated UATs manual testing will still be necessary but there will be few introduced defects.


Duncan Bayne said...

Nice work Rob :-) That sounds like a lot of work ... well done for resisting the temptation for a re-write too ... much better to keep the existing structure and tests than lose all that accumulated knowledge.

Jonathan Pantall said...

Hello Rob,
I have a Silverlight project that I want to move to WPF. We're using SL5 with SQL/Entity Framework data model. Do you have any WPF sample code for the project you converted?

Jonathan Pantall

Rishi Ravindran said...

What would be your take on the migration if the original Silverlight app used MVC instead of MVVM architecture?