Creating RIA Services Solutions
[WCF RIA Services Version 1 Service Pack 2 is compatible with either .NET framework 4 or .NET Framework 4.5, and with either Silverlight 4 or Silverlight 5.]
WCF RIA Services enables you to create solutions that address a variety of scenarios when creating business applications. These scenarios range from quite elementary to significantly complex. An elementary scenario might, for example, consist of a single Silverlight application that accesses only a few domain services in the middle tier. A more complex scenario could contain multiple Silverlight applications that are all linked to a common middle-tier layer that exposes a large set of domain services. This topic addresses issues that arise when creating and managing a RIA Services solution. It addresses questions such as the following:
What are the different ways of structuring a RIA Services solution?
Which structure is best for a particular scenario?
How can you troubleshoot a solution?
How can you deploy a solution?
This topic also provides some guidance to the Walkthroughs and other topics contained in this section that take you through tasks associated with a number of typical scenarios relevant to RIA Services. This section contains the following six Walkthroughs:
This section also contains the following three topics:
How to: Add or Remove a RIA Services Link on managing the RIA Services links.
Using the Domain Service Wizard documenting the UI elements in the dialog box.
Debugging a RIA Services Solution providing some guidance on what to do when things go wrong with creating a RIA Services solution.
The walkthroughs presented in this section of the RIA Services documentation require several prerequisite programs, such as Visual Studio 2010 and the Silverlight Runtime and SDK, be installed and configured properly, in addition to WCF RIA Services and the WCF RIA Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.
Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for WCF RIA Services node. Follow the instructions provided there to ensure that you encounter as few problems as possible when working through this RIA Services walkthroughs in this section.
In all RIA Services solutions, a link (known as a RIA Services link) exists between a Silverlight project and its associated middle-tier project. A RIA Services link is a special form of project-to-project reference that facilitates generating presentation-tier code from code in the middle tier. You create a RIA Services link by selecting the Enable WCF RIA Services check box when you create a new Silverlight project. You can also create a RIA Services link for existing projects in the project properties. In some cases, you create the RIA Services link between application projects, and in other cases you create the link between class library projects, as described in the following sections.
When a link exists between the projects, the Silverlight project receives proxy classes that enable it to access the middle-tier code. There is no way to specify that only certain parts of the middle tier apply to a Silverlight project. The following rules apply to the use of RIA Services links:
The link is defined in a Silverlight client project.
The link always points from a Silverlight client project to a .NET server project or class library.
There can only be one link per Silverlight client project.
The link cannot point to other Silverlight client projects.
Multiple Silverlight client projects can point to the same server project or class library.
A Silverlight application cannot link directly to a class library project.
For more information on managing RIA Services links, see How to: Add or Remove a RIA Services Link in this section.
In the default solution structure, RIA Services creates a single client project and a (single) server project. When you create a new project using the Silverlight Application template and check the Enable WCF RIA Services box, a solution with this default structure gets created. A RIA Services link then already exists between the two projects. When you build the solution, client code is generated for domain services and shared code. The following illustration shows the default solution structure.
The default solution structure is convenient because all domain service types and shared code added to the server project are automatically available to the Silverlight client project after you build the solution. Also, the shared code you add to the server project is visible in the client project. This structure works well when you do not have many domain services in the server project and you do not need to reuse business logic across many different Silverlight applications.
In a solution with the default structure, you can add more Silverlight applications with a RIA Services link to the server project. However, there are limitations of the default structure. The generated code for each Silverlight client can access all of middle-tier code from the server project. For example, if you have three Silverlight applications linked to a single server project and you want to add a domain service that will be used by only one of the Silverlight applications, all three client applications will have a generated domain context for the domain service and will be able to access that domain service.
For more information on creating RIA Services solutions in Visual Studio 2010, see Walkthrough: Creating a RIA Services Solution. For more information about the code generated for a client project, see Client Code Generation.
Silverlight Business Application template
RIA Services also provides a Silverlight Business Application template. This template provides a convenient starting point for building a business application that utilizes Silverlight for the client. The template builds on the Silverlight Navigation Application and uses RIA Services to support authentication and user registration. When you create a project by using the Silverlight Business Application template, RIA Services creates the solution with the default structure. The Silverlight Business Application template automatically adds the following features:
The default authentication mode for the Business Application is Forms Authentication. To use Windows Authentication, you simply change the value of the authentication element in the Web.config file, such as <authentication mode="Windows"/>, and change the value of the Authentication property on the generated WebContext class. The Business Application template automatically contains code to support either FormsAuthentication or Windows Authentication. For more information, see the Walkthrough: Using the Silverlight Business Application Template topic.
The Services folder of the server project contains the domain services that expose user registration and user authentication. The user registration service utilizes the ASP.NET membership provider to create new users. In the server project, a folder named Models contains classes for defining properties for user and registration data. You can add properties to these classes to customize the user data for your application.
In the client project, the Business Application contains code to simplify developing the presentation layer. The Libs folder and the Controls folder contain assemblies and controls that are used within the template. The Login folder (located within the Views folder) contains the authentication and user registration controls. These controls are automatically enabled in the project. The Resources folder (located in the Assets folder) contains resource strings for text within the project. As you add text to your application, you can add them to the file for application strings.
For more information, see Walkthrough: Using the Silverlight Business Application Template.
RIA Services provide the WCF RIA Services Class Library project type to support sharing code through libraries. By using class libraries, you package business logic into reusable N-tier class library components.
The following illustration shows a solution structure that uses a RIA Services class library.
In the illustration, notice that a RIA Services link does not exist between the application projects. Instead, the link exists between the class library projects. You can use any number of the class libraries in your application, and you can reuse the class library in any number of applications.
Using RIA Services class libraries provides the following advantages:
The server and client portions of a single domain of data can be developed and packaged as a single component. This component can be reused in multiple applications.
Client proxy code generation and source code sharing occurs in only one location. The location is between the tiers of the component and not in every Silverlight application.
Multiple Silverlight applications within a single Web application project can refer to just the class libraries they need. A Silverlight client does not, in this way, have to see all the business logic exposed by the middle tier.
With RIA Services class libraries, you can build a flexible solution structure that provides only the components that are needed for an application. The following illustration shows a solution structure that uses multiple RIA Services class libraries.
If you add a Class Library project to a Silverlight Business Application project, you cannot add an authentication service to the server project in the class library. The User object in the Silverlight Business Application template is inaccessible to the class library.
For more information, see Walkthrough: Creating a RIA Services Class Library.