Working on Silverlight .NET RIA Services Part 2

Wednesday, September 02 2009

If you haven’t please read part 1 before reading this post.

The most powerful thing about .NET RIA Services besides the whole framework, is the concept of sharing code from ASP.NET and Silverlight without extra steps and setting up Web Services manually. .NET RIA services provides you an automatic creation of the proxy classes to share the code between those 2 tiers.

.NET RIA Services is simple, yet introduces a few things for the developer to learn, I hope this simple tutorial will help learn those.

How to share new code.

We create a class on the ASP.NET tier.

image

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SilverlightApplicationRIA.Web.MyCode
{
    public class MyClass
    {
        public string foo()
        {
            return "Hello World";
        }
    }
}

Writing something so simple and compiling it will generate a proxy file in Silverlight to be created, the key is to name your classes with .shared in order for the .NET RIA Service to pick it up and generate the proxy class.

If we look into the Silverlight application will find hidden files in the project that you can see going to the Windows Explorer.

image

 

How to share existing code.

You can select an existing file and select to Add Link instead of just add to create the same file in both tiers.

image

This option is important to use existing items that you want to exist in both tiers, the physical files are not copied, it just creates a link between them in the hard disk.

 

How to share a library.

You can share .NET 3.5 libraries with Silverlight 3.0. This is what makes .NET RIA Services great, write it once, share it twice.

 

Consuming the shared code in Silverlight.

The pattern to consume the code in Silverlight from .NET RIA Services is pretty simple as if you remember there is a proxy copy on Silverlight created. So just add the correct namespace and execute the method synch not asynch like you would do in a web service for Silverlight.

image

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            SilverlightApplicationRIA.Web.MyCode.MyClass myClass = new SilverlightApplicationRIA.Web.MyCode.MyClass();
            string sText = myClass.foo();
        }

The sText will show a “Hello World” answer.

 

What about accessing to the ASP.NET Security.

I’ll cover this part extensively on part 3 of this series, yet to quickly answer the question of all of you that email me about this, you can in Silverlight consume the security context of the ASP.NET application by using the RiaContext class.

image

Happy .NET RIA Services coding.

Cheers

Al

Comments

.NET RIA Services Part 4: Calling methods on the Server from Silverlight. said on 9.08.2009 at 8:29 AM

In Part 2 I introduced the concept of sharing code between tiers. When creating a new class in ASP.NET with the name .shared, .NET RIA Service will generate a hidden class as well in Silverlight with the same code. So you are compiling the code once in


.NET RIA Services Part 4: Calling methods on the Server from Silverlight. said on 9.08.2009 at 8:29 AM

In Part 2 I introduced the concept of sharing code between tiers. When creating a new class in ASP.NET


Calling methods on the Server from Silverlight | Silverlight Travel said on 9.11.2009 at 1:53 PM

Pingback from Calling methods on the Server from Silverlight | Silverlight Travel


Computer Accessories said on 10.15.2009 at 4:51 AM

I didn't bring this out but the functions that you create on the server side are converted into WCF web services. What .NET RIA Services does is automate this process. Effectively, you write code and .NET RIA Services takes care of providing the mechanical code that glues the parts together by, for instance, applying the WCF attributes. And, if you want to return an ADO.NET DataSet, you can do that, also.