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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 紫色阴影

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相关知识,可以参考这篇论文