惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
T
Tenable Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hacker News: Front Page
S
Secure Thoughts
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
M
MIT News - Artificial intelligence
T
Tor Project blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V
V2EX
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
D
Docker
J
Java Code Geeks
GbyAI
GbyAI
H
Heimdal Security Blog
The Hacker News
The Hacker News
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
The Register - Security
The Register - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 紫色阴影

Android ellipsize的小问题 腾讯招聘贴 有意向的人发邮件或者msn Asp.net MVC tips -- 也做为 要写漂亮的代码 (2) 要写漂亮的代码 (1) 在团队中如何推行一项新的实践 事件驱动的javascript 验收测试的自动化 项目中的技术风险 欢迎需求变动,拥抱变化 成为MVP 使用动态数据库访问对象 使用Spring.Net对Web页面进行依赖注入 如何把握设计的尺度 初窥Managed Extensibility Framework Bug驱动开发简介 Unity的属性注入 持续集成简介 尝试使用WatiN进行TDD 关于站立式会议
简单介绍使用WCF的Web编程模型开发REST风格的Web Service
紫色阴影 · 2008-01-10 · via 博客园 - 紫色阴影

  WCF中的Web编程模型提供了一种以REST风格来设计Web Service的功能,它不同于以往基于SOAP或者WS-*规范的Web Service,而是以URI和http协议为中心的。对于操作的每一个资源有唯一的标志符,而利用不同的http动作(例如GET,POST,PUT,DELETE)来对这些资源进行相应的操作。同时该模型中还提供URI Template,它是用来定义参数化的URI,使URI的某些部分作为参数在服务中使用。可能这样解释十分含糊不清,下面用一个小例子来说明这种Web编程模型。

  在该例子中,我们使用Northwind数据库,.NET Framework 3.5,开发工具我使用的是VS2005.

  首先我们实现一个简单的需求,就是能够获取所有的Customers,这样我们定义一个Customer的Service Contract,其中包括名为GetAllCustomers的Operation Contract。

  [ServiceContract]
  public interface ICustomerService
  {
      [OperationContract]
      [WebGet(UriTemplate = "")]
      List<Customer> GetAllCustomers();
  }

可以看到,这是WCF中一个标准Service Contract的定义,其中的WebGet属性可能是以前没有见过的,它表示该服务是用Get动作来获取数据,UriTemplate为空字符串表示它不匹配任何参数。

接下来我们实现这个Service,使之从数据库中的Customer表读取所有数据,这里的Customer是一个Data Contract。

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
 public class CustomerService : ICustomerService
 {
     public List<Customer> GetAllCustomers()
     {
         List<Customer> customerList = new List<Customer>();

         //.........从数据库中获得所有的Customer

         return customerList;
     }
 }

这样,服务已经定义好并且有了实际的实现,以及我们应该用Get方法来访问这个服务,最后,该服务需要被发布出来以让外界访问。

 ICustomerService customerService = new CustomerService();
 WebServiceHost customerServiceHost
     = new WebServiceHost(customerService, new Uri("http://localhost:3333/"));
 customerServiceHost.AddServiceEndpoint(typeof (ICustomerService), new WebHttpBinding(), "customers");
 customerServiceHost.Open();

在浏览器中敲入http://localhost:3333/customers,返回的是以xml格式来描述的所有customers。

如果我们想根据CustomerID来查询某一个Customer,需要在Service Contract中增加一个Operation Contract。

[OperationContract]
[WebGet(UriTemplate = "{customerID}")]
Customer GetCustomer(string customerID);

这里的customerID是参数,如果访问的URI是http://localhost:3333/customers/ALFKI,那么customerID匹配的就是ALFKI。

接下来便实现该Operation Contract,当我们在浏览器中敲入http://localhost:3333/customers/ALFKI,返回的是xml格式描述的ALFKI这个Customer的信息。

本文主要介绍如何使用WCF的Web编程模型来开发REST风格的Web Service,在接下来的文章中会介绍更多的相关内容,比如复杂的UriTemplate、POST/PUT/DELETE的http动作等。如果想了解REST相关知识,可以参考这篇论文