用 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-领域专家 这节点内容是昨天 ...
随机推荐
- NSIS脚本入门和进阶方法
NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序.它提供了安装.卸载.系统设置.文件解压缩等功能.对于新手来说,它有 ...
- Android Broadcast 和 iOS Notification
感觉以上2个机能有许多相似之处,作个记录,待研究!
- 屏幕截图、录像FastStone Capture
作为一款极其优秀好用的屏幕截图软件,FastStone Capture 具有体积小巧.功能强大.操作简便等优点,其方便的浮动工具条和便捷的快捷键堪称完美结合,截图后的图片编辑与保存选项也特别丰富和方便 ...
- LeetCode 231 Power of Two
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...
- bootstrap清除拟态框内添加新HTML再打开时会有缓存现象
$(function(){ $("#editor").on("hidden.bs.modal",function(){ //清除缓存方法 $(this).fin ...
- Ajax如何使用Session
在Ajax中有时会使用到Session,在aspx.cs文件这样获取: string name = Session["name"]; 但是在Ajax中就不能这样获取Session, ...
- linux crontab 学习
安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum install crontabs/sbin/service ...
- 多次快速点击相同button导致重复响应的问题
Button在开发中经常用到,但是如果在瞬间点击多次时会出现多次响应事件的问题,今天给大家分享一下解决方法. 方法一:在Button响应事件中禁止Button允许点击, -(void)buttonAc ...
- printf 与c的基本类型之间的关系
型 書式 注意事項 ssize_t %zd size_t %zu intmax_t %jd uintmax_t %ju ptrdiff_t %t signed char %hhd unsigned c ...
- 关于KVC、KVO
KVC/KVO --------------------KVC--键值编码-------------------作用:通过字符串来描述对象的属性间接修改对象的属性 Student *stu=[[Stu ...