WCF下的restful配置

契约

  WCF 的基本概念是以契约(Contract) 来定义双方沟通的协议,合约必须要以接口的方式来体现,而实际的服务代码必须要由这些合约接口派生并实现。合约分成了四种:
数据契约(Data Contract),订定双方沟通时的数据格式。服务契约(Service Contract),订定服务的定义。操作契约(Operation Contract),订定服务提供的方法。消息契约(Message Contract),订定在通信期间改写消息内容的规范。

1.定义接口

[ServiceContract]
   public interface IServiceTest
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "getuser/{name}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        string Hello(string name);

        [OperationContract]
        [WebInvoke(Method="GET",UriTemplate="getAll",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
        List<Users> All();
    }
     [DataContract]
    public class Users {
         [DataMember]
        public int Id { get; set; }
         [DataMember]
        public string UserName { get; set; }
    }

2.实现接口

 public class Service2 : IServiceTest
    {
        public string Hello(string name)
        {
            return "Hello" + name;
        }


        public List<Users> All()
        {
            return new List<Users> { 
                new Users{ Id=1, UserName="111"},
                new Users{ Id=2, UserName="222"},
            };
        }
    }

协议绑定

  由于 WCF 支持了Http,TCP,Named Pipe,MSMQ,Peer-To-Peer TCP 等协议,而 HTTP 又分为基本 HTTP 支持 (BasicHttpBinding) 以及 WS-HTTP 支持 (WsHttpBinding),而 TCP 亦支持 NetTcpBinding,NetPeerTcpBinding 等通信方式,因此,双方必须要统一通信的协议,并且也要在编码以及格式上要有所一致。

一个设置通信协议绑定的示例如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<!-- 设定服务系统的资讯 -->
<services>
<service name=" CalculatorService" >
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="Binding1" contract="ICalculator" />
</service>
</services>
<!-- 设定通讯协定系统的资讯 -->
<bindings>
<wsHttpBinding>
<binding name="Binding1">
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>

3.web.Config配置

 <system.serviceModel>
    <services>
      <service name="WcfService1.Service2">
        <endpoint address="" behaviorConfiguration="RESTBehaviour"
          binding="webHttpBinding" contract="WcfService1.IServiceTest" />
      </service>
    </services>
    
    <behaviors>
      <endpointBehaviors>
        <behavior name="RESTBehaviour">

          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <httpProtocol>

      <customHeaders>
    <!--跨域处理-->
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />

      </customHeaders>

    </httpProtocol>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

注意,配置里面的service的name=.svc的view MakeUP下的Service

 

posted @ 2016-08-18 17:17  汪汪汪~~  阅读(465)  评论(0)    收藏  举报