WCF学习笔记一之通过配置web.config可以通过http访问接口

时间:2018-12-10
本文章向大家介绍WCF学习笔记一之通过配置web.config可以通过http访问接口,主要包括WCF学习笔记一之通过配置web.config可以通过http访问接口相关应用实例、知识点总结和注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、准备

这里涉及到三个文件,现在只是简单的把代码贴出来,后面再详细的讲一下。

三个文件分别是(都是wcf服务应用程序项目下的):

1、IService1.cs

2、Service1.svc

3、Web.config

wcf的契约文件:IService1.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.ServiceModel.Web;
 7 using System.Text;
 8 using DAL;
 9 
10 namespace HttpVisitWCF2
11 {
12     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
13     [ServiceContract]
14     public interface IService1
15     {
16 
17         [OperationContract]
18         [WebGet(UriTemplate="/GetData/{value}",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
19         TestModel GetData(string value);
20 
21         [OperationContract]
22         CompositeType GetDataUsingDataContract(CompositeType composite);
23 
24         // TODO: 在此添加您的服务操作
25     }
26 
27 
28     // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
29     [DataContract]
30     public class CompositeType
31     {
32         bool boolValue = true;
33         string stringValue = "Hello ";
34 
35         [DataMember]
36         public bool BoolValue
37         {
38             get { return boolValue; }
39             set { boolValue = value; }
40         }
41 
42         [DataMember]
43         public string StringValue
44         {
45             get { return stringValue; }
46             set { stringValue = value; }
47         }
48     }
49 }
IService1

wcf契约的实现:Service1.svc.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.ServiceModel.Web;
 7 using System.Text;
 8 using DAL;
 9 using Newtonsoft;
10 
11 namespace HttpVisitWCF2
12 {
13     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
14     public class Service1 : IService1
15     {
16         public TestModel GetData(string value)
17         {
18             TestModel tm = new TestModel();
19             tm.Name = "LiLei";
20             tm.Age = "18"+DateTime.Now;
21             string ret = Newtonsoft.Json.JsonConvert.SerializeObject(tm);
22             TestModel temp = Newtonsoft.Json.JsonConvert.DeserializeObject<TestModel>(ret);
23             return  tm;
24         }
25 
26         public CompositeType GetDataUsingDataContract(CompositeType composite)
27         {
28             if (composite == null)
29             {
30                 throw new ArgumentNullException("composite");
31             }
32             if (composite.BoolValue)
33             {
34                 composite.StringValue += "Suffix";
35             }
36             return composite;
37         }
38     }
39 }
Service1

wcf实现通过http访问wcf接口的web配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  
  
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding"></binding>
      </webHttpBinding>
    </bindings>

    <services>
      <service name="HttpVisitWCF2.Service1" behaviorConfiguration="serviceBehavior">
        <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="HttpVisitWCF2.IService1"/>
      </service>
    </services>

    <!--<behaviors>
      <serviceBehaviors>
        <behavior>
          --><!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --><!--
          <serviceMetadata httpGetEnabled="true"/>
          --><!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --><!--
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>-->

    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <!--这里必须设置-->
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>




  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

二、解释一下

上面这三个文件是最简单的实现了,创建一个项目把代码贴过去就可以了。

为什么要用http访问wcf接口呢?我个人的理解就是实现前后端的分离。前段可以不用有后台代码,通过js从api那里获取数据就可以了,这样的话可以更大程度的解耦前后端。