Building Silverlight solution with RIA service enabled fails on SharePoint 2010 SP1 Environments

This post is a contribution from Raghavendra Nanjaiah, an engineer with the SharePoint Developer Support team.

On a SharePoint 2010 SP1 environment when you try to build Silverlight 4 or 5 solution with the following settings:

  1. Create a new “Silverlight Application” project.
  2. Host the Silverlight application in a new web site (default selected).
  3. Silverlight version = Silverlight 4.
  4. Check ON “Enable WCF RIA Services”.
  5. Open SilverlightApplication.Web project.
  6. Right-click References and add Microsoft.SharePoint.dll.
  7. Ensure “Configuration Manager” for the solution is set to “Any CPU”.
  8. Build the entire solution.

It fails with the following error:

image

For better readability here’s the complete error:

 The "CreateRiaClientFilesTask" task failed unexpectedly.
 System.Web.HttpException (0x80004005): Could not load file or assembly 'Microsoft.Sharepoint.Sandbox' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.Configuration.ConfigurationErrorsException: Could not load file or assembly 'Microsoft.Sharepoint.Sandbox' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Could not load file or assembly 'Microsoft.Sharepoint.Sandbox' or one of its dependencies. An attempt was made to load a program with an incorrect format.
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
    at System.Reflection.Assembly.Load(String assemblyString)
    at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)

 

Here’s the workaround we used to fix this issue.

1. Copy Microsoft.SharePoint.Sandbox.dll from GAC to local drive.

image 

Path: “%windir%\assembly\GAC_64\Microsoft.Sharepoint.Sandbox\14.0.0.0__71e9bce111e9429c”

2. Add reference of the copied Microsoft.SharePoint.Sandbox.dll from GAC to SilverlightApplication1.web.

image

3. Set “Local Copy” property of the Microsoft.SharePoint.Sandbox.dll to “false”.

image

4. Build the solution.

Hope this was helpful!