zoukankan      html  css  js  c++  java
  • 基于WCF的API实现

    本文程序基于VS2013、EF6.1、WCF

    WCF有2种方式,一是SOAP,一种是Restful

    由于程序是基于PCL(可移植类库)的,所以不能用直接引入WCF服务的方式

    网上的Restful方式的文章也有一些,但是都没有解决我的问题,最终还是在stackoverflow上找到了解决方法

    言归正传,先看下代码结构(本人也是第一次用,结构可能不好,欢迎一起交流)

    Client是用来测试的客户端,没什么,可以忽略

    Contracts不用说就是契约了

    Services是实现契约

    Entity是EF实体框架

    HostingService是用Windows service 做的宿主服务

    一、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

    二、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了

    三、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了

    四、Entity

    顺便说下Entity吧,在这里我用了Code First,至于3种First的区别,请Google 去吧,在此不做讨论

    首先的问题是Entity我也熟悉,完全描述清楚表之间的关系不容易

    于是发现VS2013有一个从数据库导入的Code First模式,SQL SERVER会用吧,那就先建表吧,然后导入就OK了

    此处我想不用图片了吧,EF的导入真的很简单

    代码等回家再上传吧,里边真的挺乱的,大家就找到自己需要的部分就好了

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

  • 相关阅读:
    svn笔记
    cakephpurl生成
    cakephp加载流程
    jquery validate使用方法
    我需要具备的知识和能力总结
    cakePHP国际化和本地化
    javascript片段收集
    ecmall一个典型的操作
    9. Technical Overview 技术概述
    42. Spring Security Dependencies春季安全依赖
  • 原文地址:https://www.cnblogs.com/heyixiaoran/p/4000695.html
Copyright © 2011-2022 走看看