3

As a Silverlight newbie, I am finding it really hard to set up an RIA Web service. The examples available on the web almost always refer to Entity framework as the ORM but we are using NHibernate as our ORM. I am aware of the tutorial by Brad Abrams where he uses NHibernate as the ORM but most of it goes above my head because I am also a newbie at NHibernate and some of the concepts of RIA are not clear to me e.g. DomainService.

I'd like to first keep it simple and ignore the ORM at the moment. So, can anyone point me in the right direction as to how to get a "vanilla" web service going with Silverlight 4.0 and the latest release of RIA? For instance, how would I expose a method which returns the integer 100 and then call the method from my SilverLight application? Also, I am not sure if it's relevant or not but the Silverlight application is hosted in ASP.NET MVC 2.

To me it should be so simple but I'm really struggling with it at the moment.

TIA,

David

DavidS
  • 2,179
  • 4
  • 26
  • 44

1 Answers1

3

These scenarios (non-EntityFramework RIA Services with Silverlight) are definitely under documented and I hope to post some blog entries soon to cover these scenarios (including how to use NHibernate).

Here is one way to do what you are asking:

Install "Silverlight 4 Tools for Visual Studio 2010" if you haven't already:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b3deb194-ca86-4fb6-a716-b67c2604a139&displaylang=en

Create a new Silverlight Navigation Application in Visual Studio 2010 (check the box to enable RIA Services).

Modify the web.config in the web project in the following ways:

In the <system.web> section, add:

<httpModules>
  <add name="DomainServiceModule"
   type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule,
         System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0,
         Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>

Add a <system.serviceModel> section as a peer of <system.web>:

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
     multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Add the following references to the web project:

System.ServiceModel.DomainServices.Hosting  
System.ServiceModel.DomainServices.Server

Create a new class VanillaDomainService in the web project that contains your "return 100" method:

[System.ServiceModel.DomainServices.Hosting.EnableClientAccess()]
public class VanillaDomainService :
                System.ServiceModel.DomainServices.Server.DomainService
{
    public int ReturnInteger100()
    {
        return 100;
    }
}

Now back to the Silverlight Application project, in Home.xaml.cs, in the OnNavigatedTo method, call your new RIA Services method (remember all calls are async):

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        SilverlightApplication1.Web.VanillaDomainContext oneVanillaDomainContext =
           new SilverlightApplication1.Web.VanillaDomainContext();

        oneVanillaDomainContext.ReturnInteger100(
           anInt => MessageBox.Show(anInt.Value.ToString()), null);
    }

Now build and run and that should be it.

I tested this code and it worked for me.

Michael Maddox
  • 12,331
  • 5
  • 38
  • 40
  • Thanks Michael, I will surely give it a try and hopefully that will keep the remaining hair on my head! I'll look forward to your blog entries on how to do that. I'm sure a lot of other people would be interested too. – DavidS Dec 17 '10 at 10:53
  • Hi Michael, I've just run your code which was very clear and easy to follow. However, just in case there are other newbies like me who stumble upon this, you will have to get the references to System.ServiceModel.DomainServices.Hosting and System.ServiceModel.DomainServices.Server by browsing the file system to where you installed the Silverlight tools. In my case those references were found at "C:\Program Files (x86)\Microsoft SDKs\RIA Services\v1.0\Libraries\Server". Now onto tackling the NHibernate problem... – DavidS Dec 17 '10 at 12:47
  • @DavidS: FWIW, I didn't have to navigate on the file system to add those references. They showed up in the normal .NET References dialog for me. I have no idea why it was different for you, but thanks for sharing how you got it done! – Michael Maddox Dec 17 '10 at 13:43
  • @Michael: Yes I was also a bit surprised that they didn't show up in the normal .NET References dialog. But I must say that SilverLight is not something that's easy to pick up as there seems to be "magic" in the background. So for instance, "SilverlightApplication1.Web.VanillaDomainContext" threw me off a bit but then with "Go to Definition (F12)", I found out that some code was getting generated in the background. – DavidS Dec 17 '10 at 14:12
  • @DavidS: Yes, RIA Services has some "magic" code gen that it does. It's possible to replace RIA Services with WCF Data Services / OData or a standard WCF Web Service though, which work in a more straight-forward and predictable manner (although they aren't integrated as tightly with Silverlight as RIA Services). – Michael Maddox Dec 17 '10 at 15:37
  • @Michael: I did try to go down the WCF Web Service route but again couldn't get things going and that was done followin MS's tutorial. So far it's been quite a frustrating experience learning SL. How did you find it when you first started? – DavidS Dec 17 '10 at 18:03
  • @DavidS: I found it quite frustrating as well. I did eventually work through all the issues I had though and as I said, I hope to post the solutions I came up with to my blog. – Michael Maddox Dec 24 '10 at 11:54