zzWCF實現RESTFul Web Service

來源:互聯網
上載者:User

標籤:deploy   metadata   scl   完全   sys   資料   debugging   查詢   不同   

http://www.cnblogs.com/KeithWang/archive/2012/02/14/2351826.html

http://blog.csdn.net/qq_26054303/article/details/48655985

http://www.cnblogs.com/LNCLSH/p/3781572.html

共同學習了前面一些概念,終於開始正題了哈。RESTful的Web Service調用直觀,返回的內容容易解析。這裡先會描述一個簡單的情境--Web Service提供一個方法來搜尋個人資訊,傳入人名,返回完整個人資訊。
下面我們一步步用WCF實現一個RESTful的Web Service。在這之後分別描述用普通Console程式host在本地,以及用IIS發布到網路。

1. Contract

namespace WcfRESTful
{
[ServiceContract]
public interface IPersonRetriever
{
[OperationContract]
[WebGet(UriTemplate = "Persons/{name}", ResponseFormat = WebMessageFormat.Json)]
Person GetPerson(string name);
}

[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public DateTime Birthday { get; set; }
}
}

這裡需要注意的是在方法GetPerson()上面的WebGetAttribute:
1.1 WebGetAttribute定義了該方法的訪問方式為RESTful的Get(關於RESTful可以參考本小博中關於REST介紹的文章)。
1.2 UriTemplet描述了URL匹配的格式,當格式匹配時,{name}位置的字串會被對應傳入為方法參數。
1.3 ResponseFormat指定了返回的資料格式,可選項為JSON和XML。

 

2. Contract實現

namespace WcfRESTful
{
public class PersonRetriever : IPersonRetriever
{
public Person GetPerson(string name)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new Person { Name = name, Age = 22, Birthday = DateTime.Now };
}
}
}

這個實現裡面,我們簡單的返回一個用傳入名參數為name的Person執行個體。這裡補充一點:如果ContectType是"Text",如果返回結果串包含特別字元(比如轉義,雙引號,XML檔案片段等),有些情況會在IE中解析不正常,造成欄位缺失,目前沒有找到相關資料說明IE解析規則。為了方便和謹慎起見,直接用"text/plain"。

 

3. 在Console中Host Service

在第1,2步的基礎上,我們開始在console中host這個service。

namespace WcfRESTful
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://127.0.0.1:9998/PersonRetriever");
using (ServiceHost host = new ServiceHost(typeof(PersonRetriever), baseAddress))
{
WebHttpBinding binding = new WebHttpBinding();
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IPersonRetriever), binding, baseAddress);
WebHttpBehavior httpBehavior = new WebHttpBehavior();
endpoint.Behaviors.Add(httpBehavior);
host.Opened += delegate
{
Console.WriteLine("Hosted successfully.");
};
host.Open();
Console.ReadLine();
}
}
}
}

讓後我們通過URL:http://127.0.0.1:9998/PersonRetriever/Persons/Tom 就可以訪問該Service了,其中"Tom"是需要查詢的人名。在IE中輸入該URL,斷行符號之後的結果如:


4. 在IIS中Host Web Service

4.1建立一個WCF Service(或者Web Service依.Net Framework版本不同而定)工程,把第1,2步的Contract和實現Copy到App_Code檔案夾下面。

4.2修改Service.svc - 注意,Factory="System.ServiceModel.Activation.WebServiceHostFactory"必須添加才可以直接在IE查看結果,但是Matedata將被屏蔽不能顯示。

<%@ ServiceHost Language="C#" Debug="true" Service="WcfRESTful.PersonRetriever" CodeBehind="~/App_Code/PersonRetriever.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

4.3添加Endpoint到Web.config

    <system.serviceModel>
<services>
<service name="WcfRESTful.PersonRetriever" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="WcfRESTful.IPersonRetriever"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

4.4添加工程目錄到IIS虛擬路徑,命名為WCFTest。

以上4.1-4.4的所有步驟都完成了,我們通過URL:http://16X.19X.18X.6X/wcftest/Service.svc/Persons/Tom 一樣可以得到上面的結果{"Age":22,"Birthday":"\/Date(1329226087212-0500)\/","Name":"Tom"}。

這裡需要補充一點,在4.1步驟,我們建立一個Web Service工程,僅僅是為了少寫一些Web.Config的配置(會預設有system.web,complier等配置資訊),其實我們完全可以建立App_Code檔案夾,把Contact和Contract實現拷入該檔案夾,然後在外層手工建立Service.svc,Web.config並寫入相應配內容,一樣可以成功部署和使用。

 

5. 總結
RESTful Web Service用更簡單通用的協議(HTTP,少了SOAP這一道封裝和解析),更直接的結果,讓人眼前一亮,在資源不需要互動邏輯和複雜結構的情況下還是不錯的選擇

 

zzWCF實現RESTFul Web Service

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.