本文程序基于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的导入真的很简单
代码等回家再上传吧,里边真的挺乱的,你们就找到本身须要的部分就行了