How to create RESTful WCF Service
来源:互联网 发布:淘宝店铺流量突然暴跌 编辑:程序博客网 时间:2024/05/16 13:07
/*by Jiangong SUN*/
Based on my last post about how to create, host, test and consume WCF Service, here we will discover how to create RESTful WCF Service.
“Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. REST has emerged as a predominant Web service design model.” from wiki
“Those principles of REST service are:
User agents interact with resources, and resources are anything that can be named and represented. Each resource can be addressed via a unique Uniform Resource Identifier (URI).
Interaction with resources (located through their unique URIs) is accomplished using a uniform interface of the HTTP standard verbs (GET, POST, PUT, and DELETE). Also important in the interaction is the declaration of the resource's media type, which is designated using the HTTP Content-Type header. (XHTML, XML, JPG, PNG, and JSON are some well-known media types.)
Resources are self-descriptive. All the information necessary to process a request on a resource is contained inside the request itself (which allows services to be stateless).
Resources contain links to other resources (hyper-media).” from microsoft
We don't need to change our class "HelloWorldService", so it should always be:
namespace MyWCFServices{ /// <summary> /// Create a service class and implement service interface /// </summary> public class HelloWorldService : IHelloWorldService { public string GetMessage(string name) { return "Hello world from " + name + "!"; } public string GetPerson(string person) { return "Person name :" + person + "!"; } }}
While, the inteface "IHelloWorldService", should be modified.
using System.ServiceModel; //used for ServiceContract. it contains the types necessary to build Windows Communication Foundation (WCF) service and client applications.using System.ServiceModel.Web;namespace MyWCFServices{ [ServiceContract] public interface IHelloWorldService { [OperationContract] [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/XML/{name}")] string GetMessage(string name); [OperationContract] [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/JSON/{person}")] string GetPerson(string person); }}
The content of "HelloWorldService.svc" should always be :
<!--Host web service--><%@ServiceHost Service="MyWCFServices.HelloWorldService" %>
And then we should modify the web.config of "HostDevServer"
<?xml version="1.0" encoding="utf-8"?><!-- Pour plus d'informations sur la configuration de votre application ASP.NET, consultez http://go.microsoft.com/fwlink/?LinkId=169433 --><configuration> <!--Root node of config file--> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer> <system.serviceModel> <!--Top node of WCF Service--> <behaviors> <serviceBehaviors> <!--Specify the bahaviors of a service--> <behavior name="MyServiceTypeBehaviors"> <!-- httpGetEnabled Enable other programs locate the metadata of this web service, client applications can't generate proxy and use web service without medatadata--> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="REST"> <!-- Important --> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <services> <!--List hosted WCF Services in this web site--> <!--Each service has its name, behavior and endpoint--> <service name="MyWCFServices.HelloWorldService" behaviorConfiguration="MyServiceTypeBehaviors"> <!--Here wsHttpBinding should be replaced by webHttpBinding for REST Service, and behaviorConfiguration should be "REST" as defined in <endpointBehaviors>--> <endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="MyWCFServices.IHelloWorldService"/> <!--this endpoint is for metadata exchange--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel></configuration>
We need to run the service in "HostDevServer" before we test in browser.
Firstly we'll test the first method "GetMessage",
Then, we test the second method "GetPerson",
I hope this helps! Enjoy coding!
reference:
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide
http://msdn.microsoft.com/en-us/magazine/dd315413.aspx
http://en.wikipedia.org/wiki/Representational_state_transfer
- How to create RESTful WCF Service
- How to create WCF
- How to create a generic WCF service proxy ?
- How to create simple web service in VS2010, NOT WCF service
- How to create, host, test and consume a WCF Web Service
- How to create Web Service dll
- How to Pass IList As parameter to wcf service
- How to make changes to wcf service without breaking clients
- How to Build a RESTful API Web Service with PHP
- Android Restful Webservice Tutorial – How to create RESTful webservice in Java – Part 2
- How to Create a Windows NT/ Windows 2000 Service
- How to Create a Windows NT/ Windows 2000 Service
- How to create service which restarts on crash
- How to create (and deploy) a windows service in C# ?
- 构建WCF RESTful service示例
- How to create singleton
- How to create alarm
- How to Create Tables
- OSI七层模型
- Strtus2标签<s:url>
- CentOS 6.2安装配置LAMP服务器(Apache+PHP5+MySQL+phpmyadmin)
- 【Visual C++】游戏开发笔记三十九 浅墨DirectX教程之七 他山之石:几种几何体的快捷绘制法
- bash: ./configure: 权限不够
- How to create RESTful WCF Service
- C++编程调试秘笈----读书笔记(4)
- as3 TextField自动调整高度
- linux输出错误重定向
- Linux标准输入/输出和重定向
- Raspberry Pi with Node.js and Arduino
- [Not Mine] My weekend project: an Arduino, Raspberry Pi & Node.js robot
- 服务器 添加硬盘
- Plainte au maire pour tapage nocturne