日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
實(shí)例理解WCF數(shù)據(jù)服務(wù)

  Msdn解釋:

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、成都小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了彌渡免費(fèi)建站歡迎大家使用!

簡(jiǎn)而言之:如果使用WCF數(shù)據(jù)服務(wù),就可以通過(guò)Rest的方式來(lái)訪問(wèn)和更改數(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  
  4.   {  
  5.   // 僅調(diào)用此方法一次以初始化涉及服務(wù)范圍的策略。  
  6.   publicstaticvoidInitializeService(DataServiceConfigurationconfig)  
  7.   {  
  8.   // TODO: 設(shè)置規(guī)則以指明哪些實(shí)體集和服務(wù)操作是可見(jiàn)的、可更新的,等等。  
  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 

 

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

  將代碼修改為:

  publicclassNorthwindWcfDataService: DataService

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

  所以將

 
 
 
 
  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  
  4.   {  
  5.   // 僅調(diào)用此方法一次以初始化涉及服務(wù)范圍的策略。  
  6.   publicstaticvoidInitializeService(DataServiceConfigurationconfig)  
  7.   {  
  8.   // TODO: 設(shè)置規(guī)則以指明哪些實(shí)體集和服務(wù)操作是可見(jiàn)的、可更新的,等等。  
  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)了,剩下的就是使用客戶端來(lái)調(diào)用了。

  創(chuàng)建控制臺(tái)程序來(lá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的方式來(lái)查詢數(shù)據(jù),從本質(zhì)的角度來(lái)分析的話,不同的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來(lái)調(diào)用WCF數(shù)據(jù)服務(wù)。

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

2:MainPage.xaml 代碼如下:

 
 
 
 
  1. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  3. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  5. mc:Ignorable="d" 
  6. dd:DesignHeight="300"d:DesignWidth="400">  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  7.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  8. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  9.   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  10.   mc:Ignorable="d" 
  11.   dd:DesignHeight="300"d:DesignWidth="400"> 
  12.    
  13.    
  14.    
  15.    
  16.    
  17.    
  18.    
  19.    
  20.    
  21.    
  22.   

 

  同理也需要添加服務(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(productsQuery.RequestUri,  
  8.   (ar) =>  
  9.   {  
  10.   varproducts = northwindContext.EndExecute(ar).ToList();  
  11.   dataGrid1.ItemsSource = products;  
  12.   },  
  13.   null);  
  14.   } 

 

  再次運(yùn)行:

Silverlight 的異步

  修改MainPage.xaml 代碼

 
 
 
 
  1.  
  2.    
  3.    
  4.    

 

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

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

 

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

  解決這個(gè)問(wèn)題的方式也比較簡(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  
  9.   (productsQuery.RequestUri, null, null);  
  10.   ar.AsyncWaitHandle.WaitOne();  
  11.   varproducts = northwindContext.EndExecute(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  
  9.   (productsQuery.RequestUri, null, null);  
  10.   ar.AsyncWaitHandle.WaitOne();  
  11.   varproducts = northwindContext.EndExecute(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


網(wǎng)站標(biāo)題:實(shí)例理解WCF數(shù)據(jù)服務(wù)
URL地址:http://www.dlmjj.cn/article/cciijhd.html