偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

實(shí)例理解WCF數(shù)據(jù)服務(wù)

開發(fā) 后端
簡(jiǎn)而言之:如果使用WCF數(shù)據(jù)服務(wù),就可以通過Rest的方式來訪問和更改數(shù)據(jù)。我們今天將透過實(shí)例講解WCF數(shù)據(jù)服務(wù)。

  Msdn解釋:

簡(jiǎn)而言之:如果使用WCF數(shù)據(jù)服務(wù),就可以通過Rest的方式來訪問和更改數(shù)據(jù)。

  實(shí)戰(zhàn):

  1:新建Asp.net 空Web應(yīng)用程序:

  2:因?yàn)閃CF數(shù)據(jù)服務(wù)需要ado.net 實(shí)體,所以添加一個(gè)實(shí)體,命名為Northwind

3:添加了數(shù)據(jù)實(shí)體后,需要添加一個(gè)WCF數(shù)據(jù)服務(wù)

NorthwindWcfDataService.cs 代碼如下:

 

  1.   namespaceNorthwindDataServiceDemo  
  2.   {  
  3.   publicclassNorthwindWcfDataService: DataService</* TODO: 在此放置數(shù)據(jù)源類名*/>  
  4.   {  
  5.   // 僅調(diào)用此方法一次以初始化涉及服務(wù)范圍的策略。  
  6.   publicstaticvoidInitializeService(DataServiceConfigurationconfig)  
  7.   {  
  8.   // TODO: 設(shè)置規(guī)則以指明哪些實(shí)體集和服務(wù)操作是可見的、可更新的,等等。  
  9.   // 示例:  
  10.   // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);  
  11.   // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);  
  12.   config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;  
  13.   }  
  14.   }  
  15.   }  
  16.   publicclassNorthwindWcfDataService: DataService</* TODO: 在此放置數(shù)據(jù)源類名*/

 

  在此放置數(shù)據(jù)源類名,在這里作為數(shù)據(jù)源的是Northwind.edmx 生成的NorthwindEntities。

  將代碼修改為:

  publicclassNorthwindWcfDataService: DataService<NorthwindEntities>

  因?yàn)樾枰O(shè)置規(guī)則來指明哪些實(shí)體集和服務(wù)操作是可見的、可更新的,等等。

  所以將

 

  1.   // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);  
  2.   // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); 

 

  修改為:

 

  1.   config.SetEntitySetAccessRule("*", EntitySetRights.All);  
  2.   config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); 

 

  完整的代碼如下:

 

  1.   namespaceNorthwindDataServiceDemo  
  2.   {  
  3.   publicclassNorthwindWcfDataService: DataService<NorthwindEntities>  
  4.   {  
  5.   // 僅調(diào)用此方法一次以初始化涉及服務(wù)范圍的策略。  
  6.   publicstaticvoidInitializeService(DataServiceConfigurationconfig)  
  7.   {  
  8.   // TODO: 設(shè)置規(guī)則以指明哪些實(shí)體集和服務(wù)操作是可見的、可更新的,等等。  
  9.   // 示例:  
  10.   // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);  
  11.   // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);  
  12.   config.SetEntitySetAccessRule("*", EntitySetRights.All);  
  13.   config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);  
  14.   config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;  
  15.   }  
  16.   }  
  17.   } 

 

  4:所有一切都操作完畢后,可以在瀏覽器中查看。

好了,現(xiàn)在數(shù)據(jù)服務(wù)已經(jīng)實(shí)現(xiàn)了,剩下的就是使用客戶端來調(diào)用了。

  創(chuàng)建控制臺(tái)程序來調(diào)用WCF數(shù)據(jù)服務(wù)

  1:添加控制臺(tái)應(yīng)用程序:

2:添加服務(wù)引用:

3:修改Program.cs 代碼如下:

 

  1.   namespaceNorthwindConsoleApp  
  2.   {  
  3.   classProgram  
  4.   {  
  5.   staticvoidMain(string[] args)  
  6.   {  
  7.   UriserviceRootUri = newUri("http://localhost:34098/NorthwindWcfDataService.svc/");  
  8.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  9.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  10.   varproducts = northwindContext.Products.ToList();  
  11.   foreach(varproduct inproducts)  
  12.   {  
  13.   Console.WriteLine("{0},{1}", product.ProductID, product.ProductName);  
  14.   }  
  15.   Console.ReadLine();  
  16.   }  
  17.   }  
  18.   } 

 

  這段代碼主要是查詢Products,

  因?yàn)槭褂昧薟CF數(shù)據(jù)服務(wù),所以客戶端可以使用linq的方式來查詢數(shù)據(jù),從本質(zhì)的角度來分析的話,不同的Linq查詢?cè)诤笈_(tái)都會(huì)變成不同http請(qǐng)求地址,具體的請(qǐng)求地址可以查看RequestUri屬性。

  結(jié)果如下:

在這里可以看到Order_Details 的count 為0,

  如果想要在查詢Products的時(shí)候,同時(shí)查詢所有的Order_Details 那么可以將代碼修改如下:

  varproducts = northwindContext.Products.ToList();

  改為

  varproducts = northwindContext.Products.Expand("Order_Details").ToList();

  完整的代碼如下:

 

  1.   staticvoidMain(string[] args)  
  2.   {  
  3.   UriserviceRootUri = newUri("http://localhost:34098/NorthwindWcfDataService.svc/");  
  4.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  5.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  6.   varproducts = northwindContext.Products.Expand("Order_Details").ToList();  
  7.   foreach(varproduct inproducts)  
  8.   {  
  9.   Console.WriteLine("id:{0},Name:{1},Orders:{2}",  
  10.   product.ProductID,  
  11.   product.ProductName,  
  12.   product.Order_Details.Count);  
  13.   }  
  14.   Console.ReadLine();  
  15.   } 

 

3:使用Silverlight來調(diào)用WCF數(shù)據(jù)服務(wù)。

  1:創(chuàng)建Silverlight應(yīng)用程序

2:MainPage.xaml 代碼如下:

 

  1. <UserControlxmlns:sdkUserControlxmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="NorthwindSilverlightApp.MainPage" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6. mc:Ignorable="d" 
  7. dd:DesignHeight="300"d:DesignWidth="400">  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  8.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  9. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  10.   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  11.   mc:Ignorable="d" 
  12.   dd:DesignHeight="300"d:DesignWidth="400"> 
  13.   <Gridx:NameGridx:Name="LayoutRoot"Background="White"> 
  14.   <Grid.RowDefinitions> 
  15.   <RowDefinitionHeightRowDefinitionHeight="Auto" /> 
  16.   <RowDefinitionHeightRowDefinitionHeight="*" /> 
  17.   </Grid.RowDefinitions> 
  18.   <StackPanelGrid.RowStackPanelGrid.Row="0"HorizontalAlignment="Left" > 
  19.   <ButtonContentButtonContent="First"Click="First_Click" /> 
  20.   </StackPanel> 
  21.   <sdk:DataGridGrid.Rowsdk:DataGridGrid.Row="1"x:Name="dataGrid1" /> 
  22.   </Grid> 
  23.  </UserControl> 

 

  同理也需要添加服務(wù)引用:

3:MainPage.xaml.cs 代碼如下:

 

  1.   namespaceNorthwindSilverlightApp  
  2.   {  
  3.   publicpartialclassMainPage: UserControl  
  4.   {  
  5.   publicMainPage()  
  6.   {  
  7.   InitializeComponent();  
  8.   }  
  9.   privatevoidFirst_Click(objectsender, RoutedEventArgse)  
  10.   {  
  11.   UriserviceRootUri = newUri("http://localhost:34098/NorthwindWcfDataService.svc/");  
  12.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  13.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  14.   try 
  15.   {  
  16.   varproducts = northwindContext.Products.ToList();  
  17.   dataGrid1.ItemsSource = products;  
  18.   }  
  19.   catch(Exceptionex)  
  20.   {  
  21.   MessageBox.Show(ex.Message);  
  22.   }  
  23.   }  
  24.   }  
  25.   } 

 

  4:運(yùn)行,結(jié)果如下:

這是因?yàn)镾ilverlight 只支持異步操作,而

  varproducts = northwindContext.Products.ToList();

  使用的是同步操作

  修改First_Click 代碼如下:

 

  1.   privatevoidFirst_Click(objectsender, RoutedEventArgse)  
  2.   {  
  3.   UriserviceRootUri = newUri("http://localhost:34098/NorthwindWcfDataService.svc/");  
  4.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  5.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  6.   varproductsQuery = northwindContext.Products;  
  7.   northwindContext.BeginExecute<Product>(productsQuery.RequestUri,  
  8.   (ar) =>  
  9.   {  
  10.   varproducts = northwindContext.EndExecute<Product>(ar).ToList();  
  11.   dataGrid1.ItemsSource = products;  
  12.   },  
  13.   null);  
  14.   } 

 

  再次運(yùn)行:

Silverlight 的異步

  修改MainPage.xaml 代碼

 

  1. <StackPanelGrid.RowStackPanelGrid.Row="0"HorizontalAlignment="Left"Orientation="Horizontal" > 
  2.   <ButtonContentButtonContent="First"Click="First_Click" /> 
  3.   <ButtonContentButtonContent="Second"Click="Second_Click" /> 
  4.   </StackPanel> 

 

  之所以我在First_Click 中使用匿名委托是有原因的,因?yàn)槿绻銍L試寫下面的代碼會(huì)阻塞瀏覽器。

 

  1.   privatevoidSecond_Click(objectsender, RoutedEventArgse)  
  2.   {  
  3.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  4.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  5.   varproductsQuery = northwindContext.Products;  
  6.   IAsyncResultar = northwindContext.BeginExecute<Product>  
  7.   (productsQuery.RequestUri, nullnull);  
  8.   ar.AsyncWaitHandle.WaitOne();  
  9.   varproducts = northwindContext.EndExecute<Product>(ar).ToList();  
  10.   dataGrid1.ItemsSource = products;  
  11.   } 

 

  這個(gè)問題的原因是ar.AsyncWaitHandle是在UI線程上執(zhí)行的,所以會(huì)阻塞UI線程。

  解決這個(gè)問題的方式也比較簡(jiǎn)單,使用ThreadPool或者是Task:

  修改代碼如下,使用ThreadPool

 

  1.   privatevoidSecond_Click(objectsender, RoutedEventArgse)  
  2.   {  
  3.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  4.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  5.   varproductsQuery = northwindContext.Products;  
  6.   ThreadPool.QueueUserWorkItem((obj) =>  
  7.   {  
  8.   IAsyncResultar = northwindContext.BeginExecute<Product>  
  9.   (productsQuery.RequestUri, nullnull);  
  10.   ar.AsyncWaitHandle.WaitOne();  
  11.   varproducts = northwindContext.EndExecute<Product>(ar).ToList();  
  12.   dataGrid1.ItemsSource = products;  
  13.   });  
  14.   } 

 

  運(yùn)行:

原因如下:

最后完整的代碼如下:

 

  1.   privatevoidSecond_Click(objectsender, RoutedEventArgse)  
  2.   {  
  3.   NorthwindService.NorthwindEntitiesnorthwindContext =  
  4.   newNorthwindService.NorthwindEntities(serviceRootUri);  
  5.   varproductsQuery = northwindContext.Products;  
  6.   ThreadPool.QueueUserWorkItem((obj) =>  
  7.   {  
  8.   IAsyncResultar = northwindContext.BeginExecute<Product>  
  9.   (productsQuery.RequestUri, nullnull);  
  10.   ar.AsyncWaitHandle.WaitOne();  
  11.   varproducts = northwindContext.EndExecute<Product>(ar).ToList();  
  12.   Deployment.Current.Dispatcher.BeginInvoke(() =>  
  13.   {  
  14.   dataGrid1.ItemsSource = products;  
  15.   });  
  16.   });  
  17.   } 

 

  作者:LoveJenny

  出處:http://www.cnblogs.com/LoveJenny/    

原文鏈接:http://www.cnblogs.com/LoveJenny/archive/2012/02/13/2350020.html

【編輯推薦】

  1. WCF初接觸實(shí)作之服務(wù)發(fā)布和使用
  2. 利用WCF實(shí)現(xiàn)將服務(wù)器端的錯(cuò)誤信息返回
  3. 看WCF Web API的第一印象
  4. jQuery調(diào)用WCF開發(fā)實(shí)例經(jīng)驗(yàn)分享
  5. 手把手教你如何應(yīng)用WCF中的REST
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2016-09-08 23:47:17

大數(shù)據(jù)大數(shù)據(jù)服務(wù)

2009-11-05 16:01:51

WCF單調(diào)服務(wù)

2019-10-29 14:15:25

云存檔數(shù)據(jù)服務(wù)技術(shù)

2021-05-21 14:19:45

數(shù)據(jù)服務(wù)API技術(shù)

2021-08-27 11:05:13

Commvault

2009-12-07 17:13:23

WCF技術(shù)

2009-12-22 16:14:01

WCF服務(wù)元數(shù)據(jù)

2009-11-12 15:23:57

ADO.NET數(shù)據(jù)服務(wù)

2015-07-31 16:26:46

IBM收購Compose

2009-11-13 13:35:54

ADO.NET數(shù)據(jù)服務(wù)

2024-11-01 10:37:31

2013-04-27 10:07:04

大數(shù)據(jù)全球技術(shù)峰會(huì)阿里淘寶

2023-09-28 11:42:15

2016-10-17 09:33:24

大數(shù)據(jù)產(chǎn)業(yè)鏈場(chǎng)景應(yīng)用

2010-03-02 14:06:37

WCF服務(wù)實(shí)例管理模式

2009-11-19 17:08:55

Oracle數(shù)據(jù)服務(wù)器

2015-05-06 15:41:22

大數(shù)據(jù)服務(wù)HDFS云計(jì)算

2014-06-18 10:50:09

大數(shù)據(jù)大數(shù)據(jù)服務(wù)神州數(shù)碼

2017-08-23 18:28:40

華為

2017-01-07 11:45:43

醫(yī)療健康大數(shù)據(jù)虛擬化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)