一、准备

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

三个文件分别是(都是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. 记录: Android测试网速实现

    2.3开始android提供了一个流量统计类, android.net.TrafficStats,通过使用这个类提供的方法,就可以获取设备流量.下面为该类中的常用方法,欢迎大家完善补充 static ...

  2. install mysql from source and troubleshooting example

    I tried to install MySQL 5.7 from source file and upgrading previous MySQL version to the lastest 5. ...

  3. vs2010提取资源

    setlocal enabledelayedexpansion rem cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin set R ...

  4. chrome.debugger

    官网: https://chromedevtools.github.io/devtools-protocol/ https://developer.chrome.com/extensions/debu ...

  5. 循环TRUNCATE表,再ENABLE约束索引等

    CREATE OR REPLACE PROCEDURE STG.FP_REMOVE_MST_OLD_DATA (EXITCODE OUT NUMBER) IS /******************* ...

  6. 大数据入门到精通13--为后续和MySQL数据库准备

    We will be using the sakila database extensively inside the rest of the course and it would be great ...

  7. Oracle 基本语法、触发器、视图

    参考文章:https://www.cnblogs.com/linjiqin/category/349944.html 数据库分类 1.小型数据库:access.foxbase 2.中型数据库:inor ...

  8. 二叉搜索树与双向链表(python)

    题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. # -*- coding:utf-8 -*- # class TreeNo ...

  9. Python SMTP发送邮件

    import smtplibfrom email.mime.text import MIMEText  # 引入smtplib和MIMEText host = 'smtp.163.com'  # 设置 ...

  10. 获取MessageBox按钮本地字符串(OK、Cancel、Yes、No等)

    问题仍然由定制MessageBox引发. 定制MessageBox,虽加入自定义些东西,但仍然希望,最大限度接近系统原生框.碰到的问题,就是其钮文本. 即如MessageBox.Show()之Mess ...