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




原创粉丝点击