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. Spark机器学习6·聚类模型(spark-shell)

    K-均值(K-mean)聚类 目的:最小化所有类簇中的方差之和 类簇内方差和(WCSS,within cluster sum of squared errors) fuzzy K-means 层次聚类 ...

  2. 防止CSRF的攻击—Origin和Referer

    防止CSRF的攻击—Origin和Referer 为了防止CSRF的攻击,我们建议修改浏览器在发送POST请求的时候加上一个Origin字段,这个Origin字段主要是用来标识出最初请求是从哪里发起的 ...

  3. Mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    Mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) Linux: MyS ...

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

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

  5. 偶然发现有的IIS里的程序,连接 不上SQL Server数据库, 超时

    经查应用程序池中, 有一个启用32位应用程序,  有时打开它就能连接上SQL SERVER了.

  6. C++ vector 多次删除第一个元素

    转载声明: 代码都是来源于一下连接,做了一点点修改,为了记忆方便,故贴在这里,原文链接:http://blog.csdn.net/doctor_feng/article/details/1188078 ...

  7. 如何查看eclipse、mysql的版本 - 原创

    Eclipse 1)如果实在官网下载的,看压缩包名字就可以看出来,只带有win32字样的是32位,带有win32-x86_64字样的是64位的. 2)找到eclipse安装目录的eclipse.ini ...

  8. Origin软件作图留白过多问题解决

    解决空白过大方法:1.Tools——>Options2.Page——>Copy page setting——>Margin默认是Page,下拉菜单选Border,Clip Borde ...

  9. JQuery小知识点代码

    1.链式操作 $(function(){ /*var oDiv = $('#div1'); oDiv.html('hello'); oDiv.css('background','red'); oDiv ...

  10. 调用http接口的工具类

    网上面有很多,但是我们项目怎么也调不到结果,试了差不多很多案例,都是报connection reset 后来,我发现是有一个验证,需要跳过验证.然后才能调接口.所以找了一个忽略https的方法.进行改 ...