Monday, November 23, 2009

Silverlight 3, RIA services, and Resource (RESX) Files

One of the advantages of using RIA services is that you can build your domain entity tier independently from the rest of the application tiers and then wire it in as needed, whether as LINQ or simple POCO classes.

If you decide to annotate your entities with resources, it can get a little dicey. Most examples online focus on resource files within Silverlight or entities defined within Silverlight. However, it is quite common to have existing POCO objects that point to a resource file. For example, I might declare a user entity like this:

public class User
        public int ID { get; set; }

        [Display(Name="UserName", Description="UserNameDescription", ResourceType=typeof(EntityResource))]
        [RegularExpression("^([A-Za-z0-9])+$", ErrorMessageResourceName="BadUserName", ErrorMessageResourceType=typeof(EntityResource))]
        public string UserName { get; set; }

In this example, my resources are defined in the same project as the user entity, in a EntityResource.resx file.

When I build my DomainService to use this object, the first thing I get is an error related to accessing the resource file. I'm told it needs to be flagged as public. This is simple enough: I go into the designer and switch the access the modifier.

RIA Services with Resource File

Now we can compile again, but again, we get an error. This time it's in one of those auto-generated files that ends with .g and it says that my LOB.Entity.EntityResource type doesn't exist! What happened? I marked my domain service with EnableClientAccess, but the resource type doesn't make it to the Silverlight client.

It turns out the magic of RIA (for Silverlight 3) only goes so far ... we need to go ahead and manually bring the resource file over. Fortunately, we can do it in a way that prevents us from having to duplicate code.

In the silverlight project, figure out where you want the resource files to reside. I chose a folder called Links to keep all of my cross-linked resources in one place. On the folder, I right-click and choose "Add Existing Item." I then navigate to the resource file in my server side project, and add it ... as a link.

RIA Services with Resource File

Be sure to add the Designer.cs file as well, and you should be good.

Jeremy Likness


  1. Jeremy,
    We recently decided to move our Ria services validation errors to resx files. I followed this blog post and it worked great! I have a problem though. Out app has mulitiple projects which are loaded using MEF. I can get the resx files to work in the root project but I get an error when other projects try to use the resx files:

    Could not find any resources appropriate for the specified culture or the neutral culture.

    I found an article that said there is a thing called MEF dynamic resource loading but I cant find information about it anywhere. You and brada were the only ones who blogged about resx and RIA but you both did it with a single client project.

    I was wondering if you have gotten resx files to be shared accoss MEF projects? Or if you know anything about dynamic resource loading?


  2. So to share them I would probably export them and import them, and then the same rules would apply as anything else: use DeploymentCatalog or similar to pull them in, and make sure copy local is false for everything but one project reference in the solution.

    Does this answer your question about programmatic access?

  3. Thanks for the link,
    My problem is that RIA automatically downloads entities that have explicit calls to the resx file. This can be simply reproduced by creating a new silverlight business application and adding a new silverlight project to the solution. After compiling there are 5 errors saying it can’t find the ‘Resource’ in the namespace.

    I then added the resx files as link to the new project as well as the main. That makes it compile but throws the error mentioned in my first post when I try to access it.

    As to sharing the files using MEF, I am not exactly sure how to export and import a resx file since they don’t implement an interface?

  4. They don't have to implement an interface. If you can programmatically reference them, you can stick them as a property and export them that way.

  5. hi Sir,

    After deploying the app if i have modified my resx file on the server its values are not reflecting in the client.

    i mean that i have resourec files at server created the link to those on client then i deployed the app.

    now if i have modified the string value in the server its not refelecting at client.

    please do the needful.

    thanks and regards,

  6. Hey Jeremy, I know this topic is pretty old, but if you could check out this problem in order to provide some guidance to the unexperienced (me) , it would be nice:


  7. Thanks a lot for sharing that information. It's good to know more about the RIA services.

    perth web design