基於WCF的API實現

本文程序基於VS201三、EF6.一、WCFweb

WCF有2種方式,一是SOAP,一種是Restful數據庫

因爲程序是基於PCL(可移植類庫)的,因此不能用直接引入WCF服務的方式app

網上的Restful方式的文章也有一些,可是都沒有解決個人問題,最終仍是在stackoverflow上找到了解決方法框架

言歸正傳,先看下代碼結構(本人也是第一次用,結構可能很差,歡迎一塊兒交流)ide

 

Client是用來測試的客戶端,沒什麼,能夠忽略測試

Contracts不用說就是契約了spa

Services是實現契約3d

Entity是EF實體框架rest

HostingService是用Windows service 作的宿主服務code

1、Contracts

對契約的分類理解不深,因此大概寫了一下,直接看詳細代碼

1 [ServiceContract]
2     public interface IBoardService
3     {
4         [OperationContract]
5         [WebInvoke(Method = "POST", UriTemplate = "getConfigData/{email}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
6         string GetConfigData(string email);
7     }


這裏指明瞭是要POST請求,若是要Get,就把WebInvoke 換成WebGet

2、Services

1 public class BoardService : IBoardService
2     {
3         public string GetConfigData(string email)
4         {
5             return "successed";
6         }
7     }

這樣寫完你在調用的時候會提示 AddressFilter 和 EndpointDispatcher 不匹配,我搜到的也就到這了,這也是困擾了我很久的問題

最終在http://stackoverflow.com/questions/6919768/rest-wcf-service 找到了答案

在類上邊加上 [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]

而後果真變化了,錯誤變爲 ContractFilter 和 EndpointDispatcher 不匹配,不用擔憂,到這就說明WCF部分OK了

3、HostingService

到這就是關鍵的App.config了,配置對了,就能夠Post到了,具體以下

 1   <system.serviceModel>
 2     <bindings>
 3       <webHttpBinding>
 4         <binding name="boardServiceBinding">
 5           <security mode="None"/>
 6         </binding>
 7       </webHttpBinding>
 8     </bindings>
 9 
10     <protocolMapping>
11       <add scheme="webHttp" binding="webHttpBinding" />
12     </protocolMapping>
13 
14     <behaviors>
15       <serviceBehaviors>
16         <behavior name="BoardBehavior">
17           <serviceMetadata httpGetEnabled="true" />
18           <serviceDebug includeExceptionDetailInFaults="false" />
19         </behavior>
20       </serviceBehaviors>
21 
22       <endpointBehaviors>
23         <behavior name="REST">
24           <webHttp />
25         </behavior>
26       </endpointBehaviors>
27     </behaviors>
28 
29     <services>
30       <service behaviorConfiguration="BoardBehavior" name="BoardServices.Services.BoardService">
31         <endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding"
32           contract="BoardContracts.ServiceContract.IBoardService">
33           <identity>
34             <dns value="localhost" />
35           </identity>
36         </endpoint>
37         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
38         <host>
39           <baseAddresses>
40             <add baseAddress="http://127.0.0.1:8080/BoardService" />
41           </baseAddresses>
42         </host>
43       </service>
44           </services>
45   </system.serviceModel>

請注意endpointBehaviors 和 endpoint的寫法

到此Post服務就基本OK了

4、Entity

順便說下Entity吧,在這裏我用了Code First,至於3種First的區別,請Google 去吧,在此不作討論

首先的問題是Entity我也熟悉,徹底描述清楚表之間的關係不容易

因而發現VS2013有一個從數據庫導入的Code First模式,SQL SERVER會用吧,那就先建表吧,而後導入就OK了

此處我想不用圖片了吧,EF的導入真的很簡單

 

代碼等回家再上傳吧,裏邊真的挺亂的,你們就找到本身須要的部分就行了

代碼地址:http://pan.baidu.com/s/1bntlLoF

相關文章
相關標籤/搜索