WCF RIA services tips: Using resource files
When you’re using RIA services you may have already come across the possibility to use data annotations to enrich entities exposed by your RIA service to modify the way fields are rendered an validated. One of the things that’s supported is the use of resources to make error messages and other string literals localizable. In this post I will show you a handy trick to make working with resources to format your error messages a breeze.
Setting up the project for resources
Before you can start creating localized error messages you need a location to store those messages. For regular .NET projects this means adding a resource file to the project. For RIA services it means you have to do a little more.
First let’s add a new resource file to the server project.
After adding the resource file to the server project, open the resource file and set the code generation option to Public. You will need this later for the client.
Now that you have a resource file in the server project, you will need to do one final step to get the same resources on the client. Right click on the client project and choose Add existing item. This will open up the Add files dialog.
Select both the .resx file and the .designer.cs file for the resource file you want to add. Instead of clicking add blindly, hold back for a second and press the little arrow pointing down beside the add button. You will get a menu that gives you the option to add the files as a link. Use this option to add the files.
What will happen is that Visual Studio will link the files to the project. If you update the resource file in the server project, the client will get updated automatically.
Using the resources for your validation logic
So why go through all the trouble adding a resource file to both the server project and the client project of your RIA services solution? Well, you’re going to need this to localize various error messages and display texts.
Take for example the following entity:
public class Person { [Required(ErrorMessageResourceName="Person_Name_Required", ErrorMessageResourceType = typeof(Resources.Messages))] public string Name { get; set; } }
Notice the Required attribute on the name property. It tells the framework that the field is required and that it should look for the Person_Name_Required message in the Messages resource type (Which is generated automatically).
When you build the project, RIA services will automatically generate a client version of the same person class with the same attribute. This is why you had to go through the extra steps to add the resource file to the client project and make the code generation public in the server project. If you don’t do that, the compiler will be unable to find the resource type and runtime will not be able to display the error message.
Conclusion
The trick to make error messages and display values in a RIA service-enabled Silverlight application is simple, when you think about it. Getting the right code in the right location however can give you a major headache.
I hope this helps to get you going with localization in RIA services.
Cheers.