5 Most Common Asked Questions When Using .Net RIA Services
- 16
- Add a Comment
If you're new here, you may want to subscribe to my RSS feed or become a fan of this blog on Facebook. Thanks for visiting!
From the comments on my previous articles on .Net RIA Services and from the search queries that lead to those articles, a pattern of a recurring set of questions showed up and I am sure that most .Net RIA Services enthusiasts face the same issues.
I will try and address a few of this issues in this post.
Oh, and please subscribe to my RSS Feed if you would like to read more on .Net RIA Services and other cool stuff I write about.
How to Exclude a Property with an Unsupported Type
I am sure this happened to most of you. You have a DomainService and you write a method to return one of your custom types that uses an Unsupported Type in .Net RIA Services. You build your solution and you get the following error.
Service operation named ‘GetUser’ does not conform to the required signature. Both return and parameter types must be an entity type or one of the predefined serializable types.
The error message is actually not that helpful as you tend to first think that there is something wrong with the method signature in the DomainService. But as it happens the issue is with the return type or the type of the parameter of that method.
The solution is really simple, all you have to do is add the ExcludeAttribute available in System.Web.DomainServices. For example in your class you have a reference to a MembershipUser in a property and you want to Exclude it your code would look like this
[Exclude] public MembershipUser MyUser { get; set; }
The exclude attribute Indicates that an entity member should not exist in the code generated client view of the entity, and that the value should never be sent to the client.
How to Consume Services from Different Domains (URIs)
This is a question that came up in the comments of my Hello World .Net RIA Services example.
So the scenario is you have one client Silverlight application but multiple services that you want to consume, these can be on the same server or reside in different datacenters. This scenario can happen in delocalized units in large organizations where the client needs to figure out which server it should query for a specific info.
Luckily the guys behind .Net RIA Services thought about this and offer us a really simple way to achieve it. For each DomainService in our ASP.Net application a DomainContext is generated for the client application.
The generated class has 3 constructors, but the one we are interested in is the one that takes a Uri as a parameter.
/// <summary> /// Constructor used to specify a data service URI. /// </summary> /// <param name="serviceUri"> /// The MyService data service URI. /// </param> public MyServiceContext(Uri serviceUri) : base(new HttpDomainClient(serviceUri)) { }
Using this constructor we can specify which Server we want to connect to.
How to Return a String in a DomainService
This does not apply to strings only but for any Supported Type by the framework.
The solution is very simple, even though it took me a few minutes to figure it out. All you have to do is add the ServiceOperationAttribute to your method so that it’s generated in the DomainContext of the client application.
[ServiceOperation] public string GetMyString() { return "MyString"; }
How to Share a Class with the Silverlight Client
Sometimes we write utility classes that we would like available in our Business Tier as well as in our Presentation Tier. To achieve this in .Net RIA Services what we would do is append the .shared.cs suffix to the file that contains the class.
The generator automatically copies the content of that class that would be compiled both at the ASP.Net application as well as at the Silverlight Client level. Here is an excerpt from the .Net RIA Service overview:
This code can be compiled and executed on both the server and the client tiers. It is important to take a moment to consider why this is so. The .NET RIA Services framework automatically creates client proxy classes in the Silverlight project for all entities exposed by the server’s DomainService class. The framework preserves the entities’ namespace, name, and public properties.
When our sample server project compiles, the entity types are actually those generated by our Entity Framework database. However, when this code compiles on the client, these entity types are the generated client proxies.
In this way, the shared application logic is shared at the source level and compiled separately by the server and client projects. This makes it easy to author code once that can be used across tier boundaries.
What are all the Attributes Available in the .Net RIA Services Framework
Entity Attributes
These are attributes that can be applied to Entities used in your application:
ExcludeAttribute: Indicates that an entity member should not exist in the code generated client view of the entity, and that the value should never be sent to the client.
IncludeAttribute: When applied to an entity association, this attribute indicates that the framework should follow the association when non-null during serialization to send associated objects to the client. This attribute can also be used to specify member projections.
MetadataTypeAttribute: Used for associating a metadata class with the entity class.
Metadata Attributes
The Metadata attributes are used in the Metadata class associated with an entity:
AssociationAttribute: Used to mark an Entity member as an association
ConcurrencyCheckAttribute:This attribute is used to mark the members of a Type that participate in optimistic concurrency checks.
CustomValidationAttribute: Validation attribute that executes a user-supplied method at runtime.
DataTypeAttribute: Allows for clarification of the DataType represented by a given property (such as Password vs URL)
DisplayAttribute: DisplayAttribute is a general-purpose attribute to specify user-visible globalizable strings for types and members. The string properties of this class can be used either as literals or as resource identifiers into a specified ResourceType
DisplayColumnAttribute: Sets the display column, the sort column, and the sort order for when a table is used as a parent table in FK relationships.
DisplayFormatAttribute: Allows overriding various display-related options for a given field. The options have the same meaning as in BoundField.
FilterUIHintAttribute: An attribute used to specify the filtering behavior for a column.
KeyAttribute: Used to mark one or more entity properties that provide the entity’s unique identity
RangeAttribute: Used for specifying a range constraint
RegularExpressionAttribute: Regular expression validation attribute
RequiredAttribute: Validation attribute to indicate that a property field or parameter is required.
StringLengthAttribute: Validation attribute to assert a string property, field or parameter does not exceed a maximum length
TimestampAttribute: This attribute is used to mark a Timestamp member of a Type.
UIHinAttribute: Attribute to provide a hint to the presentation layer about what control it should use.
ValidationAttribute: Base class for all validation attributes.
DomainService Attributes
Note that most of the CRUD operations can be inferred from the name of the method and the is not need to use the attribute.
AuthorizationAttribute: Identifies the permissions required to invoke a DomainOperationEntry.
CustomAttribute: Attribute applied to a DomainService method to indicate that it is a custom method.
DeleteAttribute: Attribute applied to a DomainService method to indicate that it is a delete method.
IgnoreOperationAttribute: Attribute applied to a DomainService method to indicate that code generators should not infer a method as a domain operation.
InsertAttribute: Attribute applied to a DomainService method to indicate that it is an insert method.
MetadataProviderAttribute: Apply this attribute to a DomainService to specify one or more custom TypeDescriptionProvider Types that should be registered for all entity types exposed by the DomainService.
QueryAttribute: Attribute applied to a DomainService method to indicate that it is a query method.
RequiresAuthenticationAttribute: Identifies the authentication requirements needed to invoke a DomainOperationEntry.
RequiresRolesAttribute: Identifies the roles required to invoke a DomainOperationEntry.
ResolveAttribute: Attribute applied to a DomainService method to indicate that it is a resolve method.
ServiceOperationAttribute: Attribute applied to a DomainService method to indicate that it is a service operation.
UpdateAttribute: Attribute applied to a DomainService method to indicate that it is an update method.
Hope this helps.
Hatim

16 Comments
Brad Abrams
April 8th, 2009
at 12:37pm
Great post! I think I even learned a couple of things
Hatim
April 8th, 2009
at 1:29pm
Brad,
WOW
Thanks man it means a lot !!
Brad Abrams : Link Round up on .NET RIA Services (March 13th)
April 14th, 2009
at 1:45pm
[...] Hatim answers some of the 5 Most Common Asked Questions When Using .Net RIA Services [...]
billy
April 22nd, 2009
at 10:46am
kool
Dennis
May 6th, 2009
at 9:42pm
Hatim - thanks for the post, but one thing I’m really struggling with is the [Include] attribute to get to associated data. I have a pretty simple scenario where one table has a foreign key releationship to another table, and I want to get to the related data, not the key.
I tried using the [Include] attribute in the metadata class, but the data sent to the client has a null entry for client. Here’s a snippet of what I’m doing:
[MetadataTypeAttribute(typeof(Artist.ArtistMetadata))]
public partial class Artist
{
internal sealed class ArtistMetadata
{
// Metadata classes are not meant to be instantiated.
private ArtistMetadata()
{
}
[Include]
public Gender Gender;
}
}
I really would appreciate any help you could give on this (and I’m surprised that this isn’t one of the most frequently asked questions!)
Thanks!
Dennis
Hatim
May 7th, 2009
at 9:19am
Dennis,
If I understand correctly what you are trying to do is load the Gender data for Artist. say for example you wanna write something like this:
myArtist.Gender right?
if so you need to remember to call LoadGender from your context otherwise from what I tried it will always be empty. Unless there is a better way of doing this I couldn’t find any on my own.
I also believe the metadata has nothing to do with this actually.
Let me know if this answers your question.
Hatim
Dennis
May 7th, 2009
at 1:18pm
Hatim - You’ve saved the day for me! I had misunderstood what I should do in this case, and was thinking that the associated data would come in when the request for Artist was made (triggered by the [Include] metadata attribute).
I simply called ctx.LoadGenders() and everything is working as expected. I’ve also removed the [Include] metadata attribute and it still works.
I really appreciate your answer on this, and I think the new silverlight3 stuff with the .net ria services is really great.
Thanks again!
Dennis
Ronald Widha
May 8th, 2009
at 7:55pm
again awesome post. it’s helping me alot learning RIA
John McFetridge
May 17th, 2009
at 7:39pm
Surely there is an optional way to do eager loading as there are many cases where one would have build a dictionary on the client to associate the child class to the parent. It seems to me that LINQ has all the relationships and I oroginally thought that [Include] would do the trick but alas that is not case
Bob Harrison
May 20th, 2009
at 6:52pm
I added a domain service to my silverlight app using the ado .net entity model. I have an abstract class called MainPanel. There are types of Main panels. Main panels can connect with 3 types of sub-panels. So there is a one/many relationship of main panel to sub-panel. One of the sub-panels has an entity name ReaderPanel, a type of sub-panel.
When I try to compile the client project part of the solution I get an error similiar to the one above.
Entity ‘SAEUI.Web.ReaderPanel’ has a property ‘MainPanel’ with an unsupported type.
The two other types of sub-panels have the same foreign key pointer back to main panel, but I don’t get any error for those.
Noam
May 31st, 2009
at 5:39pm
Great stuff. Thanks.
I have one question on the use of [Exclude]. It works great if I own the object being passed. But what if I don’t? Can the exclusion be done through a metadata class not part of the main business class?
batamania
June 18th, 2009
at 1:15pm
yup, why don’t silverlight ria allow us to load the associate class immediately when it is called.
for instance, Supervisor and staff.
Supervisor.staff should just load all the staff under that supervisor.
Can someone show me how to do it rightly?
big fan
September 27th, 2009
at 5:14am
Awesome post Hatim — thanks!
Talu
October 5th, 2009
at 11:18am
Thank you a lot, it was very usefull!
Silverlight and RIA: Ignoring a Property in Your Business Object - Deborah's Developer MindScape
December 8th, 2009
at 6:49am
[...] this out without the help of Colin Blair from the Microsoft Silverlight forums, who found it on Hatim's Blog post. Thanks Colin and [...]
jaskirat
January 5th, 2010
at 1:01pm
the post was very helpful, though i’m struggling with a small problem. I have a custom method which is private, returns nothing and has one parameter in my domain service class, but when i compile my code it doesn’t show up in the generated code.
Please help