How to create RESTful WCF Service
2013-01-07 04:33
555 查看
/*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:
While, the inteface "IHelloWorldService", should be modified.
The content of "HelloWorldService.svc" should always be :
And then we should modify the web.config of "HostDevServer"
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
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 a generic WCF service proxy ?
- How to create simple web service in VS2010, NOT WCF service
- RESTful WCF Services with No svc file and No config and How to use Service Route
- How to create, host, test and consume a WCF Web Service
- How to create WCF
- Monotouch/WCF: How to consume the wcf service without svcutil
- How to Create a User Profile Service Application
- How to create WSDL SourceCode from a webservice.
- How to Create a Windows NT/ Windows 2000 Service
- How to use jquery ajax and android request security RESTful WCF
- MSMQ&Com+ Service: How to create an Com+ Service in .NetFramework
- How to Create a Windows NT/ Windows 2000 Service
- [Windows Azure] How to Create and Deploy a Cloud Service?
- How to Pass IList As parameter to wcf service
- How to create service which restarts on crash
- [转]How to create a Windows service by using Sc.exe
- How to make a simplest WCF service work on Win7 with VS2010
- Service Station - An Introduction To RESTful Services With WCF
- How to create (and deploy) a windows service in C# ?
- How and Why to use the System.servicemodel.MessageParameterAttribute in WCF