WCF服务轻量级服务,可供JS调用

返回值格式:XML、Json

工程结构:

示例代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace RESTfulWebServices
{
public class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}

实体类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web; namespace RESTfulWebServices
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetList/{name1}/{name2}",
BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<UserInfo> GetList(string name1, string name2); [OperationContract]
[WebGet(UriTemplate = "GetUserInfo",
BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
UserInfo GetUserInfo();
}
}

服务契约

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace RESTfulWebServices
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public List<UserInfo> GetList(string name1, string name2)
{
List<UserInfo> list = new List<UserInfo>();
UserInfo u1 = new UserInfo();
u1.Age = ;
u1.Name = name1;
u1.Sex = "女";
list.Add(u1); UserInfo u2 = new UserInfo();
u2.Age = ;
u2.Name = name2;
u2.Sex = "男";
list.Add(u2); return list;
} public UserInfo GetUserInfo()
{
UserInfo u2 = new UserInfo();
u2.Age = ;
u2.Name = "王老五";
u2.Sex = "男";
return u2;
}
}
}

服务实现

 <?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="GetPostBehavior" name="RESTfulWebServices.Service1">
<endpoint address="" behaviorConfiguration="GetPostEndBehaviors" binding="webHttpBinding"
contract="RESTfulWebServices.IService1">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="GetPostEndBehaviors">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="GetPostBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>

配置文件

运行结果:

无参服务调用方式:地址+方法名 http://localhost:1768/Service1.svc/GetUserInfo

XML格式返回值:

带参数服务调用方式:地址+方法名+参数1+参数2+参数N  http://localhost:1768/Service1.svc/GetList/Jim/Tom

Json格式返回值:

示例源码下载:RESTfulWebServices.rar

RESTful 服务(配备 WCF)介绍

RESTful 服务示例的更多相关文章

  1. 实战SpringCloud响应式微服务系列教程(第十章)响应式RESTful服务完整代码示例

    本文为实战SpringCloud响应式微服务系列教程第十章,本章给出响应式RESTful服务完整代码示例.建议没有之前基础的童鞋,先看之前的章节,章节目录放在文末. 1.搭建响应式RESTful服务. ...

  2. weblogic 10.x 上开发restful服务

    之前已经学习过 利用JAX-RS快速开发RESTful 服务,当时是jboss环境,如果原封不动的迁移到weblogic 10.x 版本,会杯具的发现应用启动失败,需要做些小调整: 项目结构如下: 需 ...

  3. java 利用JAX-RS快速开发RESTful 服务

    JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/jsr/detail?id=311 .从 ...

  4. 使用Spring Security Oauth2完成RESTful服务password认证的过程

            摘要:Spring Security与Oauth2整合步骤中详细描述了使用过程,但它对于入门者有些重量级,比如将用户信息.ClientDetails.token存入数据库而非内存.配置 ...

  5. java_java 利用JAX-RS快速开发RESTful 服务

    JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/jsr/detail?id=311 .从 ...

  6. RESTful服务最佳实践

    本文主要读者 引言 REST是什么 统一接口 基于资源 通过表征来操作资源 自描述的信息 超媒体即应用状态引擎(HATEOAS) 无状态 可缓存 C-S架构 分层系统 按需编码(可选) REST快速提 ...

  7. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  8. jersey2.26+spring5+jpa一步步搭建restful服务

    前言 首先,为什么想选择Jersey做restful服务呢?我个人比较喜欢它的插件化设计,可以方便的注入自己的全局处理逻辑.再一个就是可以生成wadl描述文件,供查询服务方法.所以在学习spring的 ...

  9. 我们必须要知道的RESTful服务最佳实践

    看过很多RESTful相关的文章总结,参齐不齐,结合工作中的使用,非常有必要归纳一下关于RESTful架构方式了,RESTful只是一种架构方式的约束,给出一种约定的标准,完全严格遵守RESTful标 ...

随机推荐

  1. 【Python】常用内建模块(卒)

    内容来自廖雪峰的官方网站 笔记性质 1.datetime 2.collections 3.base64 4.struct 5.hashlib 6.itertools 7.contextlib 8.XM ...

  2. 微信小程序:其中wxml和wxss的样式说明

    微信小程序:其中wxml和wxss的样式说明 一.简介 对于css不熟悉的Android程序员来说,开发微信小程序面临的一个比较困难的问题就是界面的排版了.微信小程序的排版就跟wxml和wxss有关了 ...

  3. samba 4.7.16 安装配置详解

    系统:Centos 7.4 x64位 服务版本:samba-4.7.1.samba-client-4.7 Samba 简介 Samba 是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服 ...

  4. Windows 10 安装 到SSD硬盘

    1.更换SSD硬盘 2.安装windows 10 系统(升级太慢,建议全新安装) 3.全程不到1个小时个月安装完成. 4.这个分数惨不忍睹,但是速度还是蛮快. 5.挂载机械硬盘,安装驱动,window ...

  5. 启动工程Ehcache报错

    缓存组建用的Ehcache,在启动的时候报了下面的错误,虽然不影响使用,看着还是有点碍眼:   DEBUG net.sf.ehcache.util.UpdateChecker - Update che ...

  6. IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config

    https://support.microsoft.com/en-ie/help/3151973/iis-shared-configuration-feature-requires-all-serve ...

  7. AtCoder Regular Contest 096

    AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...

  8. linux tzselect 设置时区

    date -R 检查时间 tzselect 按照提示逐步设置 //这里演示的是设置东八区 TZ='Asia/Shanghai'; export TZ 添加到/etc/profile source pr ...

  9. coredump调试小结

    在已经启动的进程中使用gdb,用gdb attach 查看so文件中的函数列表 nm -D *.so 关于c.c++类的gdb调试,强烈推荐一本书:debug hack

  10. code for 1 - 分治

    2017-08-02 17:23:14 writer:pprp 题意:将n分解为n/2, n%2, n/2三部分,再将n/2分解..得到一个序列只有0和1,给出[l, r]问l到r有几个1 题解:分治 ...