Compartilhar via


How to: Share Code through Source Files

[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.]

This topic describes how to share code, and so functionality, between the middle tier and the presentation tier of a WCF RIA Services application using source files. When you change the source file in the middle tier and rebuild your application, the updated code is automatically synchronized in the client project. You put code in a shared source file when the code does not belong in a domain service or an entity class, and you do not want the code to go through the client code generation process.

You can share source files by either using a shared naming convention or by file links. This topic describes how to share source files by using both of these approaches. For information about the advantages and disadvantages of both approaches, see the Shared Code topic.

This topic assumes that you have already created a RIA Services solution. For a description of the procedures for doing this, see the Walkthrough: Creating a RIA Services Solution topic.

To share a source file by using a shared naming convention

  1. In Solution Explorer for a middle-tier project, create any directory structure that you want to contain shared code files.

    The directory structure will be re-created in the client project under the Generated_Code folder.

  2. Add a C# or Visual Basic code file to the directory structure.

    The file can exist anywhere in the directory structure of the middle-tier project.

  3. Depending on the language of the project, name the class by using the shared naming convention: *.shared.cs or *.shared.vb.

    For example, ExampleCode.shared.cs or ExampleCode.shared.vb.

  4. Add code to the shared code file.

  5. Build (Ctrl+Shjift+B) the solution.

  6. In the client project, open the Generated_Code folder and notice that the code file has been copied.

    At the top of Solution Explorer, you might have to click the Show All Files option in the client project to see the Generated_Code folder.

    RIA_SharedCodeFile

  7. Open the code file and notice that no changes have been made to the code during compilation.

  1. In the middle-tier project, add a new code file.

  2. Add code to the code file.

  3. In the presentation-tier project, right-click the project, select Add and then select Existing Item.

  4. Select the code file that you added to the middle tier project.

  5. On the Add button, click the down array and select Add As Link.

    When you compile the project, you will see that the file has not been copied to the client project. Instead, a reference was added to the file.