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. JavaWeb请求中文乱码

    解决中文乱麻问题,页面端发出的数据作两次encodeURI var name="张三"; encodeURI(encodeURI(name)); 后台解码: URLDecoder. ...

  2. React Native 常用学习链接地址

    Android Studio下载http://www.android-studio.org/ 第二章:Android Studio概述(一)http://ask.android-studio.org/ ...

  3. Gnostice PDFtoolkit VCL的安装

    Installation and Uninstallation For New Users Close all open applications including the IDE. Run the ...

  4. 逆序对算法(reverse pair)

    逆序对(reverse-pair) 思想和归并排序的思想一样,时间复杂度是O(nlgn). 就是在统计逆序对个数的表达式需要注意一下. 具体实现 #include <iostream> # ...

  5. 有关写log的思考

    前言 在软件开发过程中,log往往是不太引人注意的环节,大部分的log都只是开发人员为了调试bug临时加上的.这样出来的软件,最后的log往往杂乱无章没有系统性.我们判断一个软件系统的log写的好不好 ...

  6. NUnit单元测试笔记

    vs2010 和 NUnit 问题处理. . 在 <configuration> 下 加 ... <startup> <requiredRuntime version=& ...

  7. hdu 2126 Buy the souvenirs 二维01背包方案总数

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. NumPy数据类型

    NumPy - 数据类型 NumPy 支持比 Python 更多种类的数值类型. 下表显示了 NumPy 中定义的不同标量数据类型. 序号 数据类型及描述 1. bool_存储为一个字节的布尔值(真或 ...

  9. scala学习手记25 - Curry化

    curry翻译为中文就是咖喱.意为使用curry可以让代码更有味道. scala里的curry化可以把函数从接收多个参数转换成接收多个参数列表.也就是说我们要编写的函数不是只有一个参数列表,这个参数列 ...

  10. ps切图步骤

    1.复制图层到新建 2.alt + i + r  裁剪 依次按 3.ctrl + alt + shift + s  保存 裁剪图标  复制到图层 , 删除背景,并复制样式 就可以做到 背景透明.