返回首页

介绍WCF技术提供
共享相同的底层基础设施,并根据我们的需要,我们可以选择适当的服务类型:
{A}
在这篇文章中,我想作简要概述WCF数据服务。
根据MSDN,{A2})。
使用WCF数据服务的主要优点是,它允许从任何客户端,支持OData的数据容易获得。
Visual Studio中利用ADO.NET实体框架的数据模型可以方便地创建OData服务。
{A3的基础上,下面的例子}和建立与Visual Studio 2010。WCF数据服务快速入门
例如分为四个步骤:创建一个简单的ASP.NET应用程序定义一个数据模型使用实体框架的基础上知名的{A4}数据库添加到Web应用程序的数据服务创建一个WPF客户端消费服务
让我们开始与ASP.NET应用程序:
文件--- GT;项目--- GT ASP.NET Web应用程序
将它命名为Nort​​hwindService。
下一步是建立相应的数据模型:
右键单击ASP.NET项目--- GT的名称;添加新项--- GT; ADO.NET实体数据模型
对于数据模型的名称,类型Northwind.edmx。
第三步是建立数据服务:
右键单击ASP.NET项目--- GT的名称;添加新项--- GT; WCF数据服务
对于罗斯文类型的服务,名称。
这三个步骤全部完成后,您的项目可能看起来像这样:
{A5}
为了使我们的数据服务的访问,我们需要给予模型内的特定实体的权利:

public static void InitializeService(DataServiceConfiguration config)

{

    config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead

      | EntitySetRights.WriteMerge

      | EntitySetRights.WriteReplace);



    config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead

      | EntitySetRights.AllWrite);



    config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);

}

我们的最后一步将创建一个简单的WPF客户端消耗和修改模型数据:
在解决方案资源管理器中,右键单击解决方案,单击"添加 - >新建项目--- GT WPF应用程序。
输入项目名称NorthwindClient。与此代码替换现有代码在MainWindow.xaml:{C}
,它会给我们的客户看看下面:
{A6的}
我们需要添加数据服务引用到客户端项目:
右键单击项目 - >添加引用--- GT命名空间中的文本框中,键入罗斯文发现

唯一剩下的就是访问业务数据,我们就大功告成了。这段代码复制到MainWindow.xaml.cs:
private NorthwindEntities context;

private string customerId = "ALFKI";



// Replace the host server and port number with the values

// for the test server hosting your Northwind data service instance.

private Uri svcUri = new Uri("http://localhost:12345/Northwind.svc");



private void Window1_Loaded(object sender, RoutedEventArgs e)

{

 try

 {

     // Instantiate the DataServiceContext.

     context = new NorthwindEntities(svcUri);



     // Define a LINQ query that returns Orders and

     // Order_Details for a specific customer.

     var ordersQuery = from o in context.Orders.Expand("Order_Details")

                       where o.Customer.CustomerID == customerId

                       select o;



     // Create an DataServiceCollection<t> based on

     // execution of the LINQ query for Orders.

     DataServiceCollection&lorder> customerOrders = new

         DataServiceCollection&lorder>(ordersQuery);



     // Make the DataServiceCollection<> the binding source for the Grid.

     this.orderItemsGrid.DataContext = customerOrders;

 }

 catch (Exception ex)

 {

     MessageBox.Show(ex.ToString());

 }

}



private void buttonSaveChanges_Click(object sender, RoutedEventArgs e)

{

 try

 {

     // Save changes made to objects tracked by the context.

     context.SaveChanges();

 }

 catch (DataServiceRequestException ex)

 {

     MessageBox.Show(ex.ToString());



 }

}

private void buttonClose_Click(object sender, RoutedEventArgs e)

{

 this.Close();

}

现在,我们可以建立并运行应用程序。
正如你可以看到,揭露和消费WCF数据服务1模型数据是一个简单的过程,但不是问题,更重要的是,我们的WPF客户端可以被轻松地位于一个完全不同的其他客户更换环境与服务本身没有变化。
对于本教程,可运行的代码可以下载{A7}。
这是它,
标记{S3}

回答

评论会员: 时间:2
S