现在的位置: 首页 > 综合 > 正文

Silverlight 4 + RIA Services之商业应用系列—-7 使用RIA Service Class Library

2013年10月07日 ⁄ 综合 ⁄ 共 3719字 ⁄ 字号 评论关闭

 part1: 如何使用RIA Services

part2: RIA Services更新和验证

part3:RIA Services数据新增

part4:皮肤的更改

part5:报表的展示

part6:Endpoint的设置

part7:如何使用RIA Service Class Library 

part8:url重写和界面友好 

 

 我今天看那个帖子的回帖有人说网上现在根本找不到一个完整的例子使用RIA Service Class Library这个模板的例子。我这里就还是使用AdvancedWorks给大家提供个例子。

1.       首先是创建一个普通的Silverlight Application项目,你也可以使用Business Application模板。

不勾选使用RIA Service,如下图:

2.       然后添加一个RIA Service Class Library模板。

 

3.       创建好以后的结构如下图:

 

4.       接下来是需要给Sample例子添加Reference

 

RIAServiceClassLibrarySample.Web添加ReferenceAdventureWorksClassLibrary.Web这个Project

同样的方法给RIAServiceClassLibrarySample添加AdventureWorksClassLibrary这个Reference

5.       下面这一步就是就不多说了,创建Domain Service,最后AdventureWorksClassLibrary.Web结构如下:

 

6.       Domain Service完成后我们需要给RIAServiceClassLibrarySample添加一个dll:

 

现在我们就可以在RIAServiceClassLibrarySample中使用RIA Service了。

我就直接给出在mainPage中的代码吧:

XAmL

<UserControl x:Class="RIAServiceClassLibrarySample.MainPage"

   xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="600" d:DesignWidth="800">

 

    <Grid x:Name="LayoutRoot" Background="White">

        <data:DataGrid Name="EmployeesGrid"></data:DataGrid>

    </Grid>

</UserControl>

 

后台.cs文件代码:

   private EmployeeDomainContext _employeeContext = new EmployeeDomainContext();

        public MainPage()

        {

            InitializeComponent();

            LoadOperation<Employee> loadOe= this._employeeContext.Load(this._employeeContext.GetEmployeesQuery());

            EmployeesGrid.ItemsSource = loadOe.Entities;

        }

 

7.       最后一步是把在AdventureWorksClassLibrary.Web项目中生成的app.config文件内容拷贝到RIAServiceClassLibrarySample.Web项目中的web.config中。

8.     <?xml version="1.0"?>

9.      

10.  <!--

11.    For more information on how to configure your ASP.NET application, please visit

12.    http://go.microsoft.com/fwlink/?LinkId=169433

13.    -->

14.   

15.  <configuration>

16.      <system.web>

17.          <compilation debug="true" targetFramework="4.0" />

18.          <httpModules>

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

20.          </httpModules>

21.      </system.web>

22.      <system.serviceModel>

23.          <serviceHostingEnvironment aspNetCompatibilityEnabled="true"

24.            multipleSiteBindingsEnabled="true" />

25.      </system.serviceModel>

26.      <connectionStrings>

27.          <add name="ADVENTUREWORKSEntities" connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=WSBEI510019;Initial Catalog=ADVENTUREWORKS;Persist Security Info=True;User ID=sa;Password=Ist12345;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

28.      </connectionStrings>

29.      <system.webServer>

30.          <modules runAllManagedModulesForAllRequests="true">

31.              <add name="DomainServiceModule" preCondition="managedHandler"

32.                type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

33.          </modules>

34.          <validation validateIntegratedModeConfiguration="false" />

35.      </system.webServer>

36.  </configuration>

37.   

 

38.   运行一下看看结果:

 

不错。。。。

总结一下: 使用RIA Service Class Library时需要注意:

a.     Reference的添加。

b.     添加System.ServiceModel.DomainServices.Client.dll这个Assembly

c.      Web.config文件的修改。

Cheers

抱歉!评论已关闭.