01 March 2010

Running CSLALight 3.8.2 under .NET 4 and Silverlight 4

Update December 23, 2013: this is article is pretty much outdated. Currently it gets a lot of traffic. I hope some people reading it would like to leave a comment and tell me WHY the are reading it

CSLA .NET by Rockford Lhotka is a business layer framework, but it does not run under .NET4 and Silverlight 4 yet. Fortunately this framework comes with sources, and I set out to get a simple sample application to run under Silverlight 4. As always, it is a lot more work to find out what to do than to actually do it, but eventually I was successful. To make life for those who want to follow me a little harder: I am using Visual Studio 2010RC and the Silverlight 4 tools beta. I know those are not supposed to work together, but if you follow this procedure you actually can run Silverlight 4 tools inside the RC. This procedure, however, included hacking an installer and then editing some registry keys. Follow this procedure at your own risk. It worked for me, but maybe the procedure for getting CSLA to run under .NET 4 and Silverlight 4 may work under VS2010 B2 as well.

1. Download these files
  • cslalightcs-3.8.2-100201.zip
  • lightsamples-3.8.2-100201.zip

Which can all be found on the CSLA.NET site.

2. Unzip the files in working directories

The net result should be something like this
CSLA
  - clsacs
  - clsalightcs
  - Dependencies
  - README.txt
LightSamples
  - cslalight
  - readme.txt

3. Compile clsacs.sln for .NET 4.0
  • Start Visual Studio 2010, and open the solution clsacs.sln in CSLA\cslacs.
  • Let Visual Studio 2010 convert the project
  • right-click the csla project, select properties and change the target framework to .NET 4.0
  • Change the active solution configuration to “Release”
  • Rebuild the solution. This will give some errors. If you check the clsa project’s references, you will see warning signs indicating missing assemblies, which are:
    • PresentationCore
    • PresentationFramework
    • System.Printing
    • System.Runtime.Serialization
    • System.ServiceModel
    • System.Workflow.Runtime
    • WindowsBase
  • Remove these references and replace them by their .NET 4.0 brethren.
  • Add a reference to System.Xaml
  • Rebuild the solution. You will get a whole lot of warnings but CSLA will compile.
4. Compile cslalightcs.sln for Silverlight 4
  • Start Visual Studio 2010, and open the solution clsalightcs.sln in CSLA\cslalightcs.
  • Let VS2010 convert the project
  • right-click the Cslalight project, select properties and change the Target Silverlight Version to 4.0
  • Add a reference to System.Net
  • Change the active solution configuration to “Release”
  • Rebuild the solution. Once again, a whole lot of warnings but CSLAlight will compile

5. Convert and compile the SimpleApp sample

If you browse into LightSamples\cslalight\cs\SimpleApp with the Windows Explorer you will find a simple CSLA/CSLA light solution that demonstrates the basic setup of the CSLA data portal for Silverlight. We will convert this application to work under our freshly created CSLA and CSLALight libraries.

  • Open the SampleApp.sln solution with Visual Studio 2010
  • Let Visual Studio 2010 convert the solution
  • Click “Yes” to upgrade to .NET 4.0 and ignore further errors
  • The solution contains 4 projects.
    • Set the Target Silverlight Version of Library.Client and SimpleApp to Silverlight 4
    • Set the target framework for Library.Server to .NET 4.0. No need to change SimpleApp.Web, Visual Studio has already done that
  • In project Library.Client:
    • Check the references. You will find “Csla” missing.
    • Remove the reference
    • Click Add Reference/Browse
    • Browse to CSLA\cslalightcs\Csla\ClientBin.
    • Add References to Csla.dll and System.Windows.Interactivity.dll
  • Repeat the procedure for SimpleApp
  • In project Library.Server:
    • Check the references. You will find “Csla” missing.
    • Remove the reference
    • Click Add Reference/Browse
    • Browse to CSLA\cslacs\Csla\bin\Release
    • Add a Reference to Csla.dll
  • Repeat the procedure for SimpleApp.Web.

6. Change the ServiceReferences.ClientConfig

This actually took me the longest, because I did not understand the problem. If you run the application (right click SimpleApp.Web/SimpleAppTestPage.aspx and click “View in Browser”), enter some values “for “Name” and “City” and hit “Save” you get an error indicating that there is no endpoint for contract “WcfPortal.IWcfPortal”.

Apparently something has changed in how Service Contracts are referenced in Silverlight 4. The solution turns out to be this:

  • open the ServiceReferences.ClientConfig file in the SimpleApp project
  • Locate the text contract="Csla.WcfPortal.IWcfPortal" 
  • Remove the “Csla.” prefix, including the dot, so that only contract="WcfPortal.IWcfPortal" remains
  • Run SimpleApp.Web/SimpleAppTestPage.aspx again

Hit some values for “Name” and “City” again, hit “Save” and the text “Inserted Client” appears. You can set some breakpoints to see CSLA actually goes back to the server for that. And, of course, you can right-click on the Silverlight application, click “Silverlight” on the ‘context menu’ and see that actually runs under Silverlight 4

This of course guarantees by no means that all the features of Csla will work under Silverlight 4 but at least we can go forward playing around in this new environment.

5 comments:

Kevin Klasman said...

These directions worked perfectly for me (using VS2010 Beta 2) except that the SimpleApp.Web project in the SimpleApp solution is missing clientaccesspolicy.xml.

Once I added one (copied from the CSLA Rolodex sample app) the application ran fine.

One question though...what does “And, of course, you can right-click on the Silverlight application, click “Silverlight” on the ‘context menu’ and see that actually runs under Silverlight 4” mean? I'm very new to Silverlight.

Thanks so much for this very helpful post!

Kevin, in Wilmington, MA

Loc#alJoost said...

What I mean is - move your mouse cursor to the SimpleApp Silverlight application en then right-click your mouse. Then a small menu pops up, with only one entry - "Silverlight". If you click that, you get some info on the Silverlight version you are running.

skyle said...

Thanks man! It really helps me a lot! I hope you post more articles related to this. :D I need more samples and tutorials for CSLALight and Silverlight 4. Thanks again!

dbl said...

Joost,

Have you ever put CSLA and MVVMLight together? I'm working on it but having a challenge getting the data to come through. Right now when the DataPortal on the server side runs I'm getting an error because it thinks the objects are in the .Client namespace instead of the .Server namespace. Any thoughts or samples I might could use?

Joost van Schaik said...

@dbl pooh, this is an old article running on an outdated piece of software. Yeah currently I am starting a project doing just that, but I don't have any clear samples (yet). I am mostly using the default CSLA stuff, still getting to grips with that. Mixing and matching with MVVMLight messaging, commands and triggers works well, but my ViewModels descend from CSLA viewmodels now