一、准备

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

三个文件分别是(都是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. [DataContract]
  29. public class CompositeType
  30. {
  31. bool boolValue = true;
  32. string stringValue = "Hello ";
  33.  
  34. [DataMember]
  35. public bool BoolValue
  36. {
  37. get { return boolValue; }
  38. set { boolValue = value; }
  39. }
  40.  
  41. [DataMember]
  42. public string StringValue
  43. {
  44. get { return stringValue; }
  45. set { stringValue = value; }
  46. }
  47. }
  48. }

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 = ""+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配置

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.  
  4. <system.web>
  5. <compilation debug="true" targetFramework="4.0" />
  6. </system.web>
  7.  
  8. <system.serviceModel>
  9. <bindings>
  10. <webHttpBinding>
  11. <binding name="webBinding"></binding>
  12. </webHttpBinding>
  13. </bindings>
  14.  
  15. <services>
  16. <service name="HttpVisitWCF2.Service1" behaviorConfiguration="serviceBehavior">
  17. <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="HttpVisitWCF2.IService1"/>
  18. </service>
  19. </services>
  20.  
  21. <!--<behaviors>
  22. <serviceBehaviors>
  23. <behavior>
  24. --><!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --><!--
  25. <serviceMetadata httpGetEnabled="true"/>
  26. --><!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --><!--
  27. <serviceDebug includeExceptionDetailInFaults="false"/>
  28. </behavior>
  29. </serviceBehaviors>
  30. </behaviors>-->
  31.  
  32. <behaviors>
  33. <endpointBehaviors>
  34. <behavior name="webBehavior">
  35. <!--这里必须设置-->
  36. <webHttp/>
  37. </behavior>
  38. </endpointBehaviors>
  39. <serviceBehaviors>
  40. <behavior name="serviceBehavior">
  41. <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->
  42. <serviceMetadata httpGetEnabled="true"/>
  43. <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
  44. <serviceDebug includeExceptionDetailInFaults="false"/>
  45. </behavior>
  46. </serviceBehaviors>
  47. </behaviors>
  48.  
  49. <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  50. </system.serviceModel>
  51.  
  52. <system.webServer>
  53. <modules runAllManagedModulesForAllRequests="true"/>
  54. </system.webServer>
  55.  
  56. </configuration>

二、解释一下

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

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

WCF学习笔记一之通过配置web.config可以通过http访问接口的更多相关文章

  1. 【Vue学习笔记1】全局配置 Vue.config

    1.slient 类型:boolean: 默认:false: 用法:Vue.config.silent = true  用于取消 Vue 所有的日志与警告

  2. WCF学习笔记(2)——使用IIS承载WCF服务

    通过前面的笔记我们知道WCF服务是不能独立存在,必须“寄宿”于其他的应用程序中,承载WCF服务的应用程序我们称之为“宿主”.WCF的多种可选宿主,其中比较常见的就是承载于IIS服务中,在这里我们来学习 ...

  3. PHP学习笔记----IIS7下安装配置php环境

    原文:PHP学习笔记----IIS7下安装配置php环境 Php如何安装 Php版本的选择 Php在windows下的(php5.4.7)有两种版本: VC9 x86 Non Thread Safe ...

  4. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  5. WCF学习笔记之传输安全

    WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...

  6. WCF 学习笔记之异常处理

    WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...

  7. WCF 学习笔记之双工实现

    WCF 学习笔记之双工实现 其中 Client 和Service为控制台程序 Service.Interface为类库 首先了解契约Interface两个接口 using System.Service ...

  8. golang学习笔记8 beego参数配置 打包linux命令

    golang学习笔记8 beego参数配置 打包linux命令 参数配置 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/docs/mvc/contro ...

  9. WCF学习心得------(三)配置服务

    配置服务 配置服务概述 在设计和实现服务协定后,便可以进行服务的配置.在其中可以定义和自定义如何向客户段公开服务,包括指定可以找到服务的地址,服务用于发送和接受消息的传输和消息编码,以及服务需要的安全 ...

随机推荐

  1. leetcode206

    /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNo ...

  2. leetcode207

    拓扑排序问题. class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>&a ...

  3. Redux的梳理

    学习Redux之前,我了解了它需要去解决什么问题: 用户使用方式复杂 不同身份不同使用方式 多个用户可以协作 与服务器大量交互,或者使用websocket 视图数据从多个来源获取 共享组件状态 组件之 ...

  4. Mac 系统下 mysql 的安装与配置

    1.mysql 的安装 1)官网下载 mysql 安装包:http://www.mysql.com/downloads/ 2)下载后解压打开安装包,点击 pkg 文件进行安装 3)注意:最后一步弹窗会 ...

  5. java 集合是否有序

    参考:https://www.cnblogs.com/hoobey/p/5914226.html

  6. React页面插入script

    项目中遇到插入广告的需要,而广告的信息只是一个url链接,这个链接返回的时一个js,和以前插入广告有点不同.所有找了很多方式. 先来展示广告链接返回的信息: 假设广告链接为:http://192.16 ...

  7. spring-AOP之通知和顾问

    通知和顾问都是切面的实现形式,其中通知可以完成对目标对象方法简单的织入功能. 而顾问包装了通知,可以让我们对通知实现更加精细化的管理,让我们可以指定具体的切入点. 通知分为前置通知,环绕通知及后置通知 ...

  8. 在delphi中XLSReadWriteII.组件的应用实例(2)

    第三方组件:XLSReadWriteII.v.5.20.67_XE3 实例源码如下:   unit Unit1; interface uses Winapi.Windows, Winapi.Messa ...

  9. cdnbest架设cdn同一个源用不同的端口访问如何设置

    在站点里的应用防火墙-->高级设置里配置 比如test.com要同时用80和88访问

  10. 1. Go安装

    和任何语言一样,开始使用之前都要先安装好他的开发/编译环境. Go是由谷歌开发的一个开源的编译型的静态语言,编译型语言最大的优点就是效率高运行速度快. Go语言支持Linux,Windows,Mac等 ...