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那里獲取數據就可以了,這樣的話可以更大程度的解耦前后端。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM