日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
實例理解WCF數據服務

  Msdn解釋:

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

簡而言之:如果使用WCF數據服務,就可以通過Rest的方式來訪問和更改數據。

  實戰(zhàn):

  1:新建Asp.net 空Web應用程序:

  2:因為WCF數據服務需要ado.net 實體,所以添加一個實體,命名為Northwind

3:添加了數據實體后,需要添加一個WCF數據服務

NorthwindWcfDataService.cs 代碼如下:

 
 
 
 
  1.   namespaceNorthwindDataServiceDemo  
  2.   {  
  3.   publicclassNorthwindWcfDataService: DataService  
  4.   {  
  5.   // 僅調用此方法一次以初始化涉及服務范圍的策略。  
  6.   publicstaticvoidInitializeService(DataServiceConfigurationconfig)  
  7.   {  
  8.   // TODO: 設置規(guī)則以指明哪些實體集和服務操作是可見的、可更新的,等等。  
  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 

 

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

  將代碼修改為:

  publicclassNorthwindWcfDataService: DataService

  因為需要設置規(guī)則來指明哪些實體集和服務操作是可見的、可更新的,等等。

  所以將

 
 
 
 
  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.   // 僅調用此方法一次以初始化涉及服務范圍的策略。  
  6.   publicstaticvoidInitializeService(DataServiceConfigurationconfig)  
  7.   {  
  8.   // TODO: 設置規(guī)則以指明哪些實體集和服務操作是可見的、可更新的,等等。  
  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)在數據服務已經實現(xiàn)了,剩下的就是使用客戶端來調用了。

  創(chuàng)建控制臺程序來調用WCF數據服務

  1:添加控制臺應用程序:

2:添加服務引用:

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,

  因為使用了WCF數據服務,所以客戶端可以使用linq的方式來查詢數據,從本質的角度來分析的話,不同的Linq查詢在后臺都會變成不同http請求地址,具體的請求地址可以查看RequestUri屬性。

  結果如下:

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

  如果想要在查詢Products的時候,同時查詢所有的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來調用WCF數據服務。

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

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.   

 

  同理也需要添加服務引用:

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:運行,結果如下:

這是因為Silverlight 只支持異步操作,而

  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.   } 

 

  再次運行:

Silverlight 的異步

  修改MainPage.xaml 代碼

 
 
 
 
  1.  
  2.    
  3.    
  4.    

 

  之所以我在First_Click 中使用匿名委托是有原因的,因為如果你嘗試寫下面的代碼會阻塞瀏覽器。

 
 
 
 
  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.   } 

 

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

  解決這個問題的方式也比較簡單,使用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.   } 

 

  運行:

原因如下:

最后完整的代碼如下:

 
 
 
 
  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


分享名稱:實例理解WCF數據服務
URL地址:http://www.5511xx.com/article/cciijhd.html