用 C# 实现一个简单的 Rest Service 供外部调用
用 C# 实现一个简单的 Restful Service 供外部调用,大体总结为4点:
- The service contract (the methods it offers).
- How do you know which one to access from the URL given (URL Routing).
- The implementation of the service.
- How you will host the service.
详细的基本步骤如下所示:
1):工程结构(Class Library Project)

2): IRestDemoService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web; namespace EricSunRestService
{
[ServiceContract(Name = "RestDemoServices")]
public interface IRestDemoServices
{
[OperationContract]
[WebGet(UriTemplate = Routing.GetClientRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string GetClientNameById(string Id);
} public static class Routing
{
public const string GetClientRoute = "/Client/{id}";
}
}
3):RestDemoService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation; namespace EricSunRestService
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDemoServices : IRestDemoServices
{
public string GetClientNameById(string Id)
{
string ReturnString = "HaHa id is: " + Id; return ReturnString;
}
}
}
4):Host Service 工程结构 (Console Application)

5):Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EricSunRestService;
using System.ServiceModel.Web; namespace EricSunHostService
{
class Program
{
static void Main(string[] args)
{
RestDemoServices demoServices = new RestDemoServices();
WebServiceHost _serviceHost = new WebServiceHost(demoServices, new Uri("http://localhost:8000/DemoService"));
_serviceHost.Open();
Console.ReadKey();
_serviceHost.Close();
}
}
}
6):运行Host程序,在浏览器中输入对应Service的Url

更多信息请看如下链接:
http://www.progware.org/Blog/post/A-simple-REST-service-in-C.aspx
用 C# 实现一个简单的 Rest Service 供外部调用的更多相关文章
- 【Java学习笔记】如何写一个简单的Web Service
本Guide利用Eclipse以及Ant建立一个简单的Web Service,以演示Web Service的基本开发过程: 1.系统条件: Eclipse Java EE IDE for Web De ...
- 使用JDK自带功能,实现一个简单的Web Service接口发布
万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...
- 使用 PHP SOAP 来创建一个简单的 Web Service。
访问: http://www.debug.com/php-soap-demo.php?client=22 结果: apache: <VirtualHost _default_:80> Do ...
- (转)Web Service入门简介(一个简单的WebService示例)
Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...
- Web Service入门简介(一个简单的WebService示例)
Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...
- .net实现一个简单的通用查询数据、导出Excel的网页
背景:临时提供一个简单的网页,供其他人浏览数据库(Oracel.MSSQL)的某些数据,并导出Excel.支持在配置文件中随时添加或修改sql. 实现:把sql语句等信息保存一个xml文件中,前端页面 ...
- 一个简单的demo学习Android远程Service(AIDL的使用)
这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...
- 一个简单的AXIS远程调用Web Service示例
我们通常都将编写好的Web Service发布在Tomcat或者其他应用服务器上,然后通过浏览器调用该Web Service,返回规范的XML文件.但是如果我们不通过浏览器调用,而是通过客户端程序调用 ...
- IDDD 实现领域驱动设计-一个简单业务用例的回顾和理解
上一篇:<IDDD 实现领域驱动设计-由贫血导致的失忆症> 这篇博文是对<实现领域驱动设计>第一章后半部分内容的理解. Domain Experts-领域专家 这节点内容是昨天 ...
随机推荐
- splice 很好用
splice 类似一个 copy pos 和 delete的组合拳,用起来很方便. arrayObject.splice(index,howmany,item1,.....,itemX) // ind ...
- Javascript之setTimeout
参考:http://codethoughts.info/javascript/2015/07/06/javascript-callbacks/
- Oracle10g下载地址--多平台下的32位和64位
前段时间ORACLE把10G的下载从官网拿掉了 ,许多童鞋不知道ORACLE 10g 的下载地址,这里我附上oracle 10g 下载的链接,方便大家下载. 点击链接使用迅雷即可下载. ...
- buffer正确的拼接方式
var chunks = []; var size = 0; res.on('data',function(chunk){ chunks.push(chunk); size+= chunk.lengt ...
- 内存管理_JAVA内存管理
Java虚拟机规范中试图定义一种Java内存模型(Java Memory Model,JMM)来屏蔽各个硬件平台和操作系统的内存访问差异,以实现让Java程序在各种平台下都能达到一致的内存访问效果.那 ...
- VC++ LoadLibrary失败,错误126(找不到指定的模块)
在VS中调用一个资源模块dll,LoadLibrary返回值为NULL,没有加载成功.GetLastError后原因为"找不到指定的模块"!代码如下: HINSTANCE hIns ...
- oracle触发器,一个表新增、修改的同时同步另一张表
oracle创建触发器,把本地新增.修改数据过程同步到另一个服务器上去. 如果是本地,加数据库名即可.如果是远程服务器,不是一台机器,做一个db_link操作即可. ----------------- ...
- EF查询视图只得到一条记录
1.出错结果:数据库表视图有多条数据,在使用EF框架进行查询时却只得到一条数据(注:拦截EF得到的sql语句在数据库进行查询并没有任务问题). 2.出错原因:该视图中没有ID或者主键,EF查询时进行反 ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【python】入门学习(八)
异常处理: python在遇到问题时会自动引发异常,也可以用raise故意引发异常,异常种类必须是已有的 >>> raise IOError('This is a test.') T ...