码迷,mamicode.com
首页 > 其他好文 > 详细

Wcf Restful Service服务搭建

时间:2014-11-22 14:37:35      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

  • 目的

使用Wcf(C#)搭建一个Restful Service

  • 背景

最近接到一个项目,客户要求使用Restful 方式接收到数据,并对数据提供对数据的统计显示功能,简单是简单,但必须要使用Restful方式,客户端传递数据就必须使用Rest ful的格式的url,提交方式为Post。

其实在之前的公司里边就开发过一个restful服务,但只记得自己使用的wcf,而今已经忘记的差不多了,还以为只是简单的搭建一个wcf就可以,今天起初就创建了一个wcf项目,发现wcf项目http://localhost:6001/Test.svc好像很让人纠结就是这个url就是多出一个。svc,wsd什么之类的,根本就不是restful,后来上网上搜到原来我们之前不是用的简单的wcf工程,而是通过Wcf rest service template工程创建的。

  • 创建Wcf rest service template工程:

在vs 新建工程-》online templates-》在搜索框中输入Wcf rest service template-》

bubuko.com,布布扣

创建好工程的工程:

bubuko.com,布布扣

  • 这个工程值得注意的有以下地方:

1, Gobal.asax文件中的代码:

 1  public class Global : HttpApplication
 2     {
 3         void Application_Start(object sender, EventArgs e)
 4         {
 5             RegisterRoutes();
 6         }
 7 
 8         private void RegisterRoutes()
 9         {
10             // Edit the base address of Service1 by replacing the "Service1" string below
11             RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
12         }
13     }

这里边是注册了url访问 route,这个访问格式就包含一下集中restful访问类型:

2.访问方式

备注:我默认的url host url为:http://localhost:6001/

访问url:http://localhost:6001/Service1           提交方式:GET      调用函数为:GetCollection

访问url:http://localhost:6001/Service1           提交方式:POST    调用函数为:Create

访问url:http://localhost:6001/Service1/1        提交方式:Get       调用函数为:Get(...) 

访问url:http://localhost:6001/Service1/1        提交方式:PUT       调用函数为:Update(...)

访问url:http://localhost:6001/Service1/1        提交方式:PUT       调用函数为:Delete(...)
3.怎么注册访问方式

[WebGet(UriTemplate = "")]

List<SampleItem> GetCollection()

[WebInvoke(UriTemplate = "", Method = "POST")]
SampleItem Create(SampleItem instance)

[WebGet(UriTemplate = "{id}")]
SampleItem Get(string id)

[WebInvoke(UriTemplate = "{id}", Method = "PUT")]
SampleItem Update(string id, SampleItem instance)

 

[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
void Delete(string id) 

其实我们可以注意到这些"访问url"访问到的“调用的函数”上的注解:

指定了访问url为:http://localhost:6001/Service1 

WebGetAttribute

 a. 决定了提交方式为Get,

 b. UriTemplate ,决定了是否在http://localhost:6001/Service1后边追加其他路径

 c. 其他参数RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json等

WebInvokeAttribute

 a.UriTemplate ,决定了是否在http://localhost:6001/Service1后边追加其他路径

 b.Method,决定了提交方式,可选参数包含Get,Post,Put,Delete,当然也支持Header等其他方式。

c. 其他参数RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json等

  • 关于请求、返回数据格式

REST被受到欢迎原因:

1,分布式架构

2,请求、返回格式支持xml,json。

3,支持异步请求/提交数据,这使得Javascript可以轻松调用RESTful服务。

A.如果手动配置请求、返回数据格式?

wcf rest service template内置了简单的注册方式:在上边我们提到的WebGetAttribute,WebInvokerAttribute中都支持参数RequestFormat,ResponseFormat,可以手动的在代码中制定他们的参数类型:

 1  // Summary:
 2     //     An enumeration that specifies the format of Web messages.
 3     public enum WebMessageFormat
 4     {
 5         // Summary:
 6         //     The XML format.
 7         Xml = 0,
 8         //
 9         // Summary:
10         //     The JavaScript Object Notation (JSON) format.
11         Json = 1,
12     }

B.如果动态配置请求、返回数据格式?

相比手动配置之下,动态选择格式还是更灵活。打开之前创建的项目,在web.config中,设置:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>

将automaticFormatSelectionEnabled设置为true

      设置后,我们就可以通过url访问到当前我们可以访问的路径接口等.

  • 用Entity Framework和POCO Template实现数据模型及存储

未完,待续。。。。

 

 

参考资料:

http://www.cnblogs.com/xiarifeixue/archive/2011/04/27/restful.html

Wcf Restful Service服务搭建

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/yy3b2007com/p/4115056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!