在创建Silverlight Application项目的时候,是否启用Enable WCF RIA Service这个选项,Silverlight Client和Server Project 建立一个RIA Service Link关联。如果我们需要分离这种关联,可以使用WCF RIA Service Class Library项目来分离这种关联。
1.新增Silverlight Application项目,如果选中Enable WCF RIA SERVICE,则默认在Silverlight Client和Server Project之间默认建立了RIA SERVICE LINK关联。
2.如果启用了WCF RIA SERVICE,如何去除这个RIA SERVICE LINK呢?
新增项目后会有两个项目,一个为Client,一个为Server。SERVER默认命名是xxx.Web的。如图:
打开Silverlight Client项目属性,WCF RIA Service Link设置为No Project Set即可。
3.新增一个WCF RIA Service Class Library。在xxx.web项目新增一个文件夹Models,并新增一个类Customer。
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string EmailAddress { get; set; }
public IList<Customer> GetRetrieve()
{
List<Customer> custList = new List<Customer>
{new Customer()
{ CustomerId = 1,
FirstName="Bilbo",
LastName = "Baggins",
EmailAddress = "bb@hob.me"},
new Customer()
{ CustomerId = 2,
FirstName="Frodo",
LastName = "Baggins",
EmailAddress = "fb@hob.me"},
new Customer()
{ CustomerId = 3,
FirstName="Samwise",
LastName = "Gamgee",
EmailAddress = "sg@hob.me"},
new Customer()
{ CustomerId = 4,
FirstName="Rosie",
LastName = "Cotton",
EmailAddress = "rc@hob.me"}};
return custList;
}
}
然后在SLRIA项目引用SLRIAService项目,SLRIA.WEB项目引用SLRIAService.web项目。
接着绑定数据到DataGrid。这里不细说。运行程序。会出现下面的错误提示。
这时需要在Web.config添加下面的配置节点。
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>
</system.web>
</configuration>
参考资料:http://msdn.microsoft.com/en-us/library/ee707336%28v=vs.91%29.aspx