WCF RIA Service: Pass Array, List or Collection as parameter to Invoke method.

By admin - Last updated: Tuesday, December 7, 2010 - Save & Share - 5 Comments

RIA Service is great WCF service library for Silverlight client, however, I found there are some limitations around customized object. Such as Passing an array, list or collection as parameter to the service method.

For example, in RIA service class,

public void ProcessProducts(Product[] products)
{
// code here
}

Well, this is not allowed by RIA since, you will get an error saying,

Operation named ‘ProcessProducts’ does not conform to the required signature. Parameter types must be an entity type or one of the predefined serializable types.

To get around that, ( hopefully in the future, we don’t have to do this.)
Basically the followings are needed,

  1. Define a wrapper class ProductsWrapper to contain the list of the product objects.
  2. Associate the ProductsWrapper class and the existing Product class with one-to-many relationship.
  3. Expose the newly created ProductsWrapper class.

So in code, we need to define a wrapper class called ProductsWrapper

public void ProcessProducts(ProductsWrapper products)
{
// code here
}

In class ProductsWrapper, we need to define a dummy key and a list of products that you want to pass in,

public partial class ProductWrapper
{
    [Key]
    public Guid Id { get; set; }

    [Include]
    [Association("FK_ProductsWrapper", "Id", "ProductWrapperId")]
    public Product[] ProductList { get; set; }

}

In the existing entity class Product, we need to define a dummy key which acts as foreign key from the primary key of ProductWrapper,

public partial class Product
{
    public Guid ProductWrapperId{ get; set; }

}

As you can see, we try to build one-to-many relationship here as to One ProductWrapper has Many Products.
Almost done, the last step is to expose ProductsWrapper in Query method so that the client code can actually know and generate the entity i.e. ProductWrapper class.
In RIA service class, add a dummy method as below,

public ProductWrapper GetProductWrapper()
{
  return null;
}

Compile the RIA client project, you will see ProcessProducts(ProductsWrapper products) method exposed to you and you will be able to consume it from client side.

I don’t really like this solution, this makes me feel like switching to pure WCF web service for this scenario, however, RIA Service does bring much more convenience.
and this works fine. Maybe WCF RIA will cope with it in the future release.

Thanks.




Posted in WCF, WebServices • Tags: Top Of Page

5 Responses to “WCF RIA Service: Pass Array, List or Collection as parameter to Invoke method.”

Comment from Andrei B
Time August 10, 2011 at 10:51 am

How would you do this in reverse? I need to return a ProductWrapper that has a list of other Entities (that are defined from my DBML file) to my Silverlight Client… but I’m having a REALLY hard time getting it to work. My array, list, or EntitySet, whichever I use, ALWAYS comes to the client Empty (Count = 0)

Any help is appreciated. thx

Comment from Andrei B
Time August 20, 2011 at 4:03 am

I modified the GetProductWrapper and used a DomainDataSource to return a ProductWrapper.

Comment from Jorge Cossio
Time November 16, 2011 at 11:20 am

Did what was advised here. And solution compiled OK. But the thing is that the array returned null on the server side. Was full on client side and nothing came through. Although the Wrapper Id, did the trip.

Something else I need to do?

Thanks

Comment from Anonymous
Time January 28, 2012 at 5:04 am

This is good. I am able to expose ProductWrapper to the client. However, when I invoke ProcessProducts(), passing it a ProductWrapper with a list of Products, the ProductList is empty at the server. It’s almost as if the list of Products do not get serialized. Am I missing something?

Comment from Rami A.
Time January 29, 2012 at 12:52 am

Thank you.

Is this resolved in WCF RIA Services V1.0 SP1?

Write a comment


Captcha: one × 1 =