DataServiceContext 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 DataServiceContext 資料服務的執行時上下文。
public ref class DataServiceContext
public class DataServiceContext
type DataServiceContext = class
Public Class DataServiceContext
- 繼承
-
DataServiceContext
範例
以下範例展示如何利用新增服務參考工具產生的查詢 DataServiceContext ,隱式執行對 Northwind 資料服務的查詢,回傳所有客戶。 請求 Customers 實體集合的 URI 會由上下文自動決定。 當列舉發生時,查詢會隱含執行。 Northwind 資料服務是在你完成 WCF 資料服務時建立的。
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
// Define a new query for Customers.
DataServiceQuery<Customer> query = context.Customers;
try
{
// Enumerate over the query result, which is executed implicitly.
foreach (Customer customer in query)
{
Console.WriteLine("Customer Name: {0}", customer.CompanyName);
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
' Define a new query for Customers.
Dim query As DataServiceQuery(Of Customer) = context.Customers
Try
' Enumerate over the query result, which is executed implicitly.
For Each customer As Customer In query
Console.WriteLine("Customer Name: {0}", customer.CompanyName)
Next
Catch ex As DataServiceQueryException
Throw New ApplicationException(
"An error occurred during query execution.", ex)
End Try
備註
WCF 資料服務是無狀態的,但 the DataServiceContext 不是。 用戶端的狀態會在互動間維持,以支援如更新管理等功能。 這個類別,以及 DataServiceQuery 代表特定 HTTP 請求給資料服務的類別,是用戶端函式庫中的兩個主要類別。
建構函式
| 名稱 | Description |
|---|---|
| DataServiceContext(Uri) |
使用指定的 DataServiceContext,初始化 |
屬性
| 名稱 | Description |
|---|---|
| ApplyingChanges |
會得到一個值,表示目前 DataServiceContext 是否正在對追蹤物件套用變更。 |
| BaseUri |
取得絕對 URI,識別目標資料服務的根節點。 |
| Credentials |
取得或設定每個使用該 DataServiceContext 物件所建立查詢所使用的認證資訊。 |
| DataNamespace |
取得或設定 Atom 有效載荷資料項目的 XML 命名空間,而非元資料項目。 |
| Entities |
會獲得目前所有被 .DataServiceContext |
| IgnoreMissingProperties |
取得或設定,判斷從該型別讀取的屬性是否必須映射到用戶端型別的屬性。 |
| IgnoreResourceNotFoundException |
當資料服務回傳 404 錯誤(資源未找到)時,是否會觸發例外,取得或設定。 |
| Links |
取得物件目前追蹤 DataServiceContext 的所有關聯或連結的集合。 |
| MergeOption |
取得或設定來自資料服務的實體同步選項。 |
| ResolveName |
取得或設定一個函式,覆蓋用戶端函式庫在將實體傳送到資料服務時所使用的預設類型解析策略。 |
| ResolveType |
取得或設定一個函式,用以覆寫用戶端函式庫在接收資料服務實體時所使用的預設型別解析選項。 |
| SaveChangesDefaultOptions |
取得或設定 SaveChangesOptions 方法所使用的 SaveChanges() 值。 |
| Timeout |
取得或設定用於底層 HTTP 請求的資料服務的逾時選項。 |
| TypeScheme |
取得或設定用來指示服務所使用的型別方案的 URI。 |
| UsePostTunneling |
取得或設定一個布林值,指示是否使用後隧道。 |
方法
事件
| 名稱 | Description |
|---|---|
| ReadingEntity |
發生在實體資料完全讀取到實體物件後。 |
| SendingRequest |
當新角色 HttpWebRequest 被創造時,會發生這種情況。 |
| WritingEntity |
發生在實體完整序列化成 XML 後,請求訊息中出現。 |