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 each tier, yet they’ll be insync. This is called the RIA Link.
Now that causes the problem that you are writing server side code that will run in the browser instead of the server, so its important to think about where do you want the code to run.
Executing ASP.NET methods from the Client.
.NET RIA Services also provides a set of Silverlight controls to access data in the Server like the Domain Data Source. This control will send a query on the Silverlight load to consume data from the server and presenting that to the Silverlight tier.
Though you won’t find it on the ToolBox after installing .NET RIA Services, you can add a namespace and type it by hand.
First add the reference to System.Windows.Ria.Controls and recompile the Silverlight application.
Add the namespace on the UserControl:
and once you type the control, intellisense will provide you the rest.
<riaControls:DomainDataSource x:Name="myriaControl" />
Of course you now need to query the data you want to consume using control parameters. Domain Data Sources are very powerful, if you used ASP.NET data sources you’ll be half way there. They give you the ability to sort, filter, edit, paging and grouping as well. I highly recommend you play with them consuming known data to understand further how they work.
Using the .NET RIA Services Class Libraries.
From the July 09 release with .NET RIA Services, the developer can create libraries in ASP.NET that can be also referenced in any Silverlight application. The library will be a single entity and assembly instead of making a copy of itself to be in multiple projects. Yet they’ll be a link between both tiers.
Create a new .NET RIA Services Class library, found on your template with calling New Project
To use this library, you’ll need to Add a Reference from ASP.NET website as well as add the reference to that library from the Silverlight project.
Now that library cannot have references to libraries outside the .NET 3.5 dlls. Adding a COM library will cause a compiling error when .NET RIA Services tries to recompile it to use in Silverlight. Yet there is a huge advantage in using these libraries, yet still the 2 projects that will be generated will exist inside a directory.
The results shows 2 libraries. in a folder. When you select to add a reference for the Web application (ASP.NET) you’ll be able to select the RIA Service Library
Make sure you added in Silverlight as well as ASP.NET.
Now write a method on the Silverlight project just created:
Then in ASP.NET you can see the method:
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
RIAServicesLibrary1.Class1 myclass = new RIAServicesLibrary1.Class1();
Easy to now consume methods from Silverlight with ASP.NET. In the Part 2 we saw how to consume ASP.NET data from Silverlight, the sample above is a good example how to consume Silverlight data from the ASP.NET tier.
One thing I haven’t being able to figure it out, if why the .Web project gets created, I thought you can in RIA access that ASP.NET library, so who can access that library created and how is the link established? I cannot access the 2 classes below I created in Silverlight or ASP.NET.
To answer a few emails: Question) How can I have an application like the business template without Authentication?
To answer people asking me about the business application, you can just use the navigation application template that does not include the .NET RIA services and the Authentication won’t be enable by default.
Visual Studio 2008 will ask you if you want to enable .NET RIA Services, I suggest you to enable that to enjoy the powerful Silverlight and ASP.NET communication, yet the result will be what you are looking for, a template like the one in Part 3 without the Login/Logout controls.
PS: Best resource for .NET RIA Service posted by Brad Abrams