天天看點

WCF Data Services用戶端通路

講述了查詢的相關文法和例子,如果在程式中如何使用這些釋出的服務呢?下面對在代碼中通路這些服務的方法進行一下彙總

查詢

這些查詢中可以結合上文的查詢文法等使用

Ø 浏覽器位址:輸入位址,GET請求直接進行

Ø JavaScipt庫:如ExtJS、DOJO、MS AJAX等支援JSON處理的JS庫

Ø Service Reference引用

常用的形式,IDE直接添加引用,使用代理對象和上下文處理

Ø HTTP協定支援處理

增删改

Ø HTTP形式

參考MSDN的規定傳遞參數和請求形式即可,處理有些麻煩

比較常用的形式,具體參考“修改資料”一節

需要處理并發沖突:

DataServiceRequestException

· Entity Framework provider - In the data model, the ConcurrencyMode attribute of a property that is part of the concurrency token for an entity type is set to Fixed.

Insert

HTTP POST

Content-Type:需正确設定

Update

HTTP Put/Merger

Delete

HTTP Delete

例子

用戶端通過代理通路:

//Client Proxy

{

NorthwindEntities ctx = new NorthwindEntities(u);

var q = from c in ctx.Suppliers select c;

foreach (var t in q)

Console.WriteLine(t.City);

}

///添加

var cust = new Customer()

CustomerID = "Test1",

Address = "Beijing",

City = "Peking",

CompanyName = "demo",

ContactName = "test",

ContactTitle = "mr.",

Country = "China",

Fax = "123",

Phone = "111",

PostalCode = "456",

Region = "HD"

};

ctx.AddToCustomers(cust);

ctx.SaveChanges();

///更新

var upCust = (from c in ctx.Customers where c.CustomerID == cust.CustomerID select c).FirstOrDefault();

upCust.ContactName += "-UPD";

ctx.UpdateObject(upCust);

///删除

upCust = (from c in ctx.Customers where c.CustomerID == cust.CustomerID select c).FirstOrDefault();

ctx.DeleteObject(upCust);

可見,在用戶端可以使用LINQ文法進行查詢

除了用戶端,服務端也有很強的支援,下文再說。

繼續閱讀