How to set up RIA services with Silverlight 4.0 and without EF

和自甴很熟 提交于 2019-11-30 10:51:08

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!