This question is different from the questions posted [here](https://stackoverflow.com/questions/4450340/createriaclientfilestask-task-failed-unexpectedly) and [here](https://stackoverflow.com/questions/2897477/the-createriaclientfilestask-task-failed-unexpectedly) even though the titles are the same.

The problem arises intermittently. The error always occurs if the project has been cleaned or is being built from a fresh checkout. Pushing build again immediately usually results in the build succeeding. The stacktrace I get is:

```
error MSB4018: The "CreateRiaClientFilesTask" task failed unexpectedly. 
System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. 
  at System.IDisposable.Dispose() 
  at Microsoft.ServiceModel.DomainServices.Tools.CreateRiaClientFilesTask.GenerateClientProxies() 
  at Microsoft.ServiceModel.DomainServices.Tools.CreateRiaClientFilesTask.ExecuteInternal() 
  at Microsoft.ServiceModel.DomainServices.Tools.RiaClientFilesTask.Execute() 
  at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() 
  at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__1c.MoveNext() 

```

The problem also occurs intermittently during regular edit-build-execute cycles, and sometimes a restart from Visual Studio is required to make it work on these occasions.

The problem occurs both in MSBuild and Visual Studio 2010. I have all of the latest updates installed (WCF RIA Services SP2). It occurred when using the Silverlight 4 tools, and upgrading to Silverlight 5 has not solved the problem. The problem also still occurs in the beta of VS 11.

I've not been able to change the order of the build as described [here](http://blogs.msdn.com/b/visualstudio/archive/2010/12/21/incorrect-solution-build-ordering-when-using-msbuild-exe.aspx) (referenced from [here](http://forums.silverlight.net/t/85255.aspx)) as MSBuild complains of a circular dependency and Visual Studio says that I can't reference a non-Silverlight project from a Silverlight one. From that page I have also tried following [this link](http://forums.silverlight.net/t/247895.aspx/1) and changed the default ToolsVersion from 2.0 to 4.0 in the registry.

I'm definitely using the 32 bit version of MSBuild, as trying to use the 64 bit version fails with failing to Hydrate the entity framework model. I'm not using the /m switch to build it in parallel either.

The project was initially a web project (which hosts the site that the Silverlight control lives on, RIA services, and contains the Business logic) and a Silverlight project. Changing this to 3 projects (with a class library containing the logic) doesn't make any difference.

The next thing I'm planning on trying is having the project hosting the RIA service separate from the one hosting the Silverlight app, but that's not ideal for us (and I'm not even sure it will work).