一、准备

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

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

1、IService1.cs

2、Service1.svc

3、Web.config

wcf的契约文件:IService1.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DAL; namespace HttpVisitWCF2
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{ [OperationContract]
[WebGet(UriTemplate="/GetData/{value}",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
TestModel GetData(string value); [OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服务操作
} // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello "; [DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
} [DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}

IService1

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DAL;
using Newtonsoft; namespace HttpVisitWCF2
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public TestModel GetData(string value)
{
TestModel tm = new TestModel();
tm.Name = "LiLei";
tm.Age = ""+DateTime.Now;
string ret = Newtonsoft.Json.JsonConvert.SerializeObject(tm);
TestModel temp = Newtonsoft.Json.JsonConvert.DeserializeObject<TestModel>(ret);
return tm;
} public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}

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那里获取数据就可以了,这样的话可以更大程度的解耦前后端。

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. python大法好——Python 面向对象

    Python 面向对象  Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 面向对象技术简介 类(Class): 用来描述具有相同的属性和方法 ...

  2. JavaScript: Constructor and Object Oriented Programming

    Constructor :  Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...

  3. 搭建mxnet-gpu docker的pyhon remote kernel

    起因 最近看mxnet的东西,打算给实验室的机器装一个mxnet的环境,无奈实验室里面机器已经装了tensorflow,运行了好久了,环境比较老.而mxnet可是支持最新的cuda9.1和cudnn7 ...

  4. 26.Hibernate-主键和映射.md

    目录 1.复合主键映射 [toc] 2.集合映射 2.1Set集合 2.2其他集合 [toc] 3.集合数据的读取 [toc] 4.一对多和多对一映射 4.1概念 4.2配置和开发 4.2.1关键点 ...

  5. HTTPS协议、TLS协议、证书认证过程解析

    一.HTTPS 协议 HTTPS协议其实就是HTTP over TSL,TSL(Transport Layer Security) 传输层安全协议是https协议的核心. TSL可以理解为SSL (S ...

  6. 坑之mysql 字符串与数字操作

    select "123"+1 = 124; select "1a23"+1 = 2; select "aa23"+1 = 1; select ...

  7. NLTK 统计词频

    import nltk Freq_dist_nltk = nltk.FreqDist(list) for k,y in Freq_dist_nltk: print str(k),str(y)

  8. 如何强制停止http请求

    http请求很多时候会受到网络阻塞.重连等原因导致响应很慢,如果此时做了一些操作,但过几秒后又响应了之前的请求,就会造成很多问题,此时我们可以使用abort()方法强制停止http请求: let aj ...

  9. 小强学渲染之OpenGL渲染管线详析

    什么是OpenGL? OpenGL是一套图形硬件的软件API接口库,它直接和GPU交互,将3D场景渲染绘制到2D屏幕上.总结说,OpenGL的功能是将程序中定义的各种2D或3D模型绘制到帧缓存中,或者 ...

  10. python 迭代器生成

    博客:http://www.cnblogs.com/alex3714/articles/5765046.html 列表生成式 [i*2 for i in range(10)] #创建时就生成,不调用也 ...