WCF系列之.net(3.0/3.5)Rest使用示例
生活随笔
收集整理的這篇文章主要介紹了
WCF系列之.net(3.0/3.5)Rest使用示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上面我介紹了在.NET(3.0/3.5)開發WCF的使用示例。
這篇,我將講解Rest 這個炙手可熱的新的開發方式,至于rest 是什么,請大家百度下。
我們還是以下面的方式做示例:
服務接口-》服務-》客戶端
1.服務接口:數據接口
using System.ServiceModel; using System.ServiceModel.Web; //這里就是REST 要關鍵引用的類 using System.Runtime.Serialization; namespace Wcf.IProductService {[ServiceContract]public interface IProduct{//REST 四個 GET POST PUT DELETE [OperationContract] //注意.NET3.5必須要帶這個 4.0 開始就可以取消了[WebGet(UriTemplate = "all", ResponseFormat = WebMessageFormat.Xml)]List<Product> GetAll();/*IEnumerable<Product> GetAll();在.NET3.5不可以 4.0就可以 很郁悶 找了2天 問題依舊 后來把 IEnumerable改成List 問題解決 原因.NET4.0 對IEnumerable增加了新特性*/[OperationContract][WebGet(UriTemplate = "{id}")]Product Get(string id);//添加 其中路徑為/ [OperationContract][WebInvoke(UriTemplate = "/", Method = "POST")]void Create(Product product);//更新 [OperationContract][WebInvoke(UriTemplate = "/", Method = "PUT")]void Update(Product product);//刪除 [OperationContract][WebInvoke(UriTemplate = "{id}", Method = "DELETE")]void Delete(string id);} }數據成員:
using System.Runtime.Serialization; //序列化 namespace Wcf.IProductService {[DataContract(Namespace = "http://yuhao.com")]public class Product{[DataMember]public string Id { get; set; }[DataMember]public string ProductName { get; set; }[DataMember]public string Price { get; set; }} }2.服務:config配置
<system.serviceModel><!--注意這里傳統的WCF區別--><services><service name="Server.ProductService"> <!--這里只有服務名稱 沒有行為--><endpoint address="http://127.0.0.1:1808/ProductService" binding="webHttpBinding" contract="Wcf.IProductService.IProduct" /><!--Address(地址) 提高訪問地址 可以自己設置--><!--Binding(綁定) 提供訪問的模式 --><!--Contract(契約) 表示的是提高的接口 帶命名空間--><!--大家看到了沒有 多簡單沒有 那個復雜的元數據--> </service></services></system.serviceModel>實現服務類
using Wcf.IProductService; using System.ServiceModel.Web; namespace Server {public class ProductService:IProduct{public static List<Product> products = new List<Product>(){new Product{Id="1",ProductName="Iphone4s",Price="3788"},new Product{Id="2",ProductName="Ipad3",Price="3288"}};#region IProduct 成員//public IEnumerable<Product> GetAll() .net 3.5 不支持 害我調試半天public List<Product> GetAll(){return products;}public Product Get(string id){Product product = products.FirstOrDefault(p => p.Id == id);if (null == product){return null;}return product;}public void Create(Product product){products.Add(product);}public void Update(Product product){this.Delete(product.Id);products.Add(product);}public void Delete(string id){Product product = this.Get(id);if (null != product){products.Remove(product);}}#endregion} }開啟服務類?:
using System.ServiceModel; using System.ServiceModel.Web;//這個類是關鍵 using Wcf.IProductService; namespace Server {class Program{static void Main(string[] args){using (WebServiceHost host = new WebServiceHost(typeof(ProductService))){host.Open();Console.WriteLine("服務已啟動");Console.Read();}}} }3.客戶端:config配置
<system.serviceModel><behaviors><endpointBehaviors><behavior name="webBehavior"><webHttp/></behavior></endpointBehaviors></behaviors><client><endpoint name="Server.ProductService" address="http://127.0.0.1:1808/ProductService" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="Wcf.IProductService.IProduct"/></client></system.serviceModel>客戶端調用
ChannelFactory<IProduct> ChannelFactory = new ChannelFactory<IProduct>("Server.ProductService");IProduct proxy = ChannelFactory.CreateChannel();//Response.Write(proxy.Get("1").ProductName);Array.ForEach<Product>(proxy.GetAll().ToArray(), product => Response.Write(product.ProductName));好了,代碼到此就結束了,大家發現沒有這個比傳統的SOAP代碼省多了 而且還很簡單 太棒了!
大家在開發Rest的時候,盡量多注意.net版本,我就因為一個返回數組的問題,折騰2天!
下一篇,我將講解.net 4.0 開發WCF 的開發方式 和以往更不同哦!
示例代碼:WcfDemo(.net3.5)REST.zip
轉載于:https://www.cnblogs.com/flyfish2012/archive/2013/02/25/2931955.html
總結
以上是生活随笔為你收集整理的WCF系列之.net(3.0/3.5)Rest使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XR的两张图
- 下一篇: MegaSAS RAID卡 BBU Le