Wednesday, May 5, 2010

Encountered Silverlight RIA Services Errors

I ran into a couple errors while working with Silverlight and RIA Services today and figure I provide the resolutions to them real quick if you run up against them.

Entity Referenced Across Multiple Domain Services

I got the following error when I created a DTO/Entity object and then used the same DTO/Entity in two different Domain Services. I was a little surprised to see that using the same DTO/Entity across Domain Services would cause an error when the proxy to the RIA service is being built in the Silverlight project.

The entity type 'XXX' is exposed by multiple DomainService types. Entity types cannot be shared across DomainServices.

The resolution is to move you code and put all of the methods that use the DTO/Entity into the same Domain Service.

Method Parameters Not Serializable

The error message is very obvious in that it is stating that primitive types like int, string, date, etc. can only be used. However there are some subtleties that you should understand.

Parameter 'XXX' of domain operation entry 'XXX' must be one of the predefined serializable types.

First you will get this error when you pass a complex type into a Domain Service method. However you will not get this error when you pass a complex type to an insert, update, delete entity method. I found that a little confusing.

Second I was actually coding this up in VB.net for the client. You will get this error if any of the parameters for a method are ByRef and not ByVal. You will get this whether you are passing in a primitive type or not.

Domain Service Methods Not Set Up Properly

Here is another error you may get if you do not set up your Domain Service methods incorrectly when the proxy is built in the Silverlight project.

Parameter 'XXX' of domain method 'XXX' must be an entity type exposed by the DomainService, either directly via a query operation, or indirectly via an included association.

This error is basically saying that I have an Insert, Update or Delete method but I have no methods that return an object of that type or the object type is not referenced through an association. Basically it is saying that an Insert, Update and Delete method cannot exist for an object type if it is never loaded – which makes sense.

You can also potentially get this error if you do not have a Key attribute defined on your DTO/Entity.

One scenario is that you may hypothetically only ever need an insert of a DTO/Entity so you will not want to have a get method. What you should do is just add an empty get method that returns a list of that object type. If you want, throw an exception when that method is called. What this will do is suppress the error and allow you to just perform inserts.

No comments: