RESTful 服务示例
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 服务示例的更多相关文章
- 实战SpringCloud响应式微服务系列教程(第十章)响应式RESTful服务完整代码示例
本文为实战SpringCloud响应式微服务系列教程第十章,本章给出响应式RESTful服务完整代码示例.建议没有之前基础的童鞋,先看之前的章节,章节目录放在文末. 1.搭建响应式RESTful服务. ...
- weblogic 10.x 上开发restful服务
之前已经学习过 利用JAX-RS快速开发RESTful 服务,当时是jboss环境,如果原封不动的迁移到weblogic 10.x 版本,会杯具的发现应用启动失败,需要做些小调整: 项目结构如下: 需 ...
- java 利用JAX-RS快速开发RESTful 服务
JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/jsr/detail?id=311 .从 ...
- 使用Spring Security Oauth2完成RESTful服务password认证的过程
摘要:Spring Security与Oauth2整合步骤中详细描述了使用过程,但它对于入门者有些重量级,比如将用户信息.ClientDetails.token存入数据库而非内存.配置 ...
- java_java 利用JAX-RS快速开发RESTful 服务
JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/jsr/detail?id=311 .从 ...
- RESTful服务最佳实践
本文主要读者 引言 REST是什么 统一接口 基于资源 通过表征来操作资源 自描述的信息 超媒体即应用状态引擎(HATEOAS) 无状态 可缓存 C-S架构 分层系统 按需编码(可选) REST快速提 ...
- 基于SpringBoot开发一个Restful服务,实现增删改查功能
前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...
- jersey2.26+spring5+jpa一步步搭建restful服务
前言 首先,为什么想选择Jersey做restful服务呢?我个人比较喜欢它的插件化设计,可以方便的注入自己的全局处理逻辑.再一个就是可以生成wadl描述文件,供查询服务方法.所以在学习spring的 ...
- 我们必须要知道的RESTful服务最佳实践
看过很多RESTful相关的文章总结,参齐不齐,结合工作中的使用,非常有必要归纳一下关于RESTful架构方式了,RESTful只是一种架构方式的约束,给出一种约定的标准,完全严格遵守RESTful标 ...
随机推荐
- ubuntu 安装pip
apt-get install python3-pip
- 在winform中,禁止combobox随着鼠标一起滑动!
在winform中,如果form上或者是控件上有一个combobox控件,当你选择这个控件,当你鼠标移动其他地方,滑动鼠标时,这时combobox的选择值就会随之鼠标一起变化,如果你不想让comboB ...
- mongoose@4.5.2的eachAsync bug
自称踩坑大王,幸好没有地雷,哈哈哈哈哈哈,今天用了mongoose的 eachAsync() 方法,没想到,会出现 Trace: [RangeError: Maximum call stack siz ...
- Mysql 常用单词
单词: CRUD:增删改查(create/read/update/delete) create:新增项目 read:查询 update:修改 delete:删除 desc 表名:查看表结构 drop: ...
- Cnblog页面美化小记
Cnblog页面美化小记 这两天我在网上翻找了许许多多的资料,打开了不计其数的博客,对着\(js\).\(html\).\(css\)等文件删删改改,在浏览器和\(vscode\)间辗转腾挪...总算 ...
- UVA 10288 Coupons (概率)
题意:有n种纸片无限张,随机抽取,问平均情况下抽多少张可以保证抽中所有类型的纸片 题解:假设自己手上有k张,抽中已经抽过的概率为 s=k/n:那抽中下一张没被抽过的纸片概率为 (再抽一张中,两张中,三 ...
- Activity启动的四种方式
Activity启动方式有四种,分别是: standardsingleTopsingleTasksingleInstance 可以根据实际的需求为Activity设置对应的启动模式,从而可以避免创建大 ...
- 因为swap分区无法启动
用户启动时停在如下截图
- Android中getDimension,getDimensionPixelOffset和getDimensionPixelSize 区别
getDimension 获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘 返回float getDimensionPixelOffset 获取某个dim ...
- github上十二款最著名的Android播放器开源项目
1.ijkplayer 项目地址: https://github.com/Bilibili/ijkplayer 介绍:Ijkplayer 是Bilibili发布的基于 FFplay 的轻量级 Andr ...