用 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 供外部调用的更多相关文章

  1. 【Java学习笔记】如何写一个简单的Web Service

    本Guide利用Eclipse以及Ant建立一个简单的Web Service,以演示Web Service的基本开发过程: 1.系统条件: Eclipse Java EE IDE for Web De ...

  2. 使用JDK自带功能,实现一个简单的Web Service接口发布

    万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...

  3. 使用 PHP SOAP 来创建一个简单的 Web Service。

    访问: http://www.debug.com/php-soap-demo.php?client=22 结果: apache: <VirtualHost _default_:80> Do ...

  4. (转)Web Service入门简介(一个简单的WebService示例)

    Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...

  5. Web Service入门简介(一个简单的WebService示例)

    Web Service入门简介 一.Web Service简介 1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从I ...

  6. .net实现一个简单的通用查询数据、导出Excel的网页

    背景:临时提供一个简单的网页,供其他人浏览数据库(Oracel.MSSQL)的某些数据,并导出Excel.支持在配置文件中随时添加或修改sql. 实现:把sql语句等信息保存一个xml文件中,前端页面 ...

  7. 一个简单的demo学习Android远程Service(AIDL的使用)

    这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...

  8. 一个简单的AXIS远程调用Web Service示例

    我们通常都将编写好的Web Service发布在Tomcat或者其他应用服务器上,然后通过浏览器调用该Web Service,返回规范的XML文件.但是如果我们不通过浏览器调用,而是通过客户端程序调用 ...

  9. IDDD 实现领域驱动设计-一个简单业务用例的回顾和理解

    上一篇:<IDDD 实现领域驱动设计-由贫血导致的失忆症> 这篇博文是对<实现领域驱动设计>第一章后半部分内容的理解. Domain Experts-领域专家 这节点内容是昨天 ...

随机推荐

  1. python判断文件和文件夹是否存在

    import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目录或文件不存在就返回False

  2. java切换VPN让你像幽灵一样出现在全国各地

    在很多情况下,有些网络应用的需求会要求模拟人在不同地区访问网站和应用.因而切换IP也就应运而生了,然而IP作为一种稀缺资源不是随便可以获得的.因而会想到应用程序切换VPN来达到全国不同地区访问网络.因 ...

  3. linux /usr/bin/ld cannot find 解决

    问题: 在linux环境编译应用程式或lib的source code时常常会出现如下的错误讯息: /usr/bin/ld: cannot find -lxxx 这些讯息会随着编译不同类型的source ...

  4. ECharts分析xdebug跟踪日志

    2015年12月23日 15:36:11 星期三 之前用的是国外的图表工具, 有点麻烦, 文档是英文的, 看着不顺眼, 发现了百度出品的ECharts, 文档比较全, 功能比较多, 做出的图也比较好看 ...

  5. Effective C++ -----条款43:学习处理模板化基类内的名称

    可在derived class templates内通过“this->“指涉base class templates内的成员名称,或藉由一个明白写出的”base class资格修饰符”完成.

  6. codeforces 567D.One-Dimensional Battle Ships 解题报告

    题目链接:http://codeforces.com/problemset/problem/567/D 题目意思:给出 1 * n 的 field,编号从左至右依次为 1,2,...,n.问射 m 枪 ...

  7. c#操作时间

    本年还剩下多少天 private string GetEndTime() { DateTime dt = DateTime.Now; DateTime startYear = DateTime.Now ...

  8. javascript 操作cookie

    function setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDa ...

  9. 【文件】读取一个文件夹下所有的jpg图片

    今天做视频处理的时候,发现给的视频是用jpg图片的形式给出的,名字的命名规律性不是很强.就想找一种通用的遍历文件夹下图片的方法. 开始在网上找到了下面这份代码,发现只能读取所有的文件夹,文件都被跳过了 ...

  10. iOS- 如何改变section header

    希望这个从UITableViewDelegate协议里得到的方法可以对你有所帮助: - (UIView *) tableView:(UITableView *)tableView viewForHea ...