A simple WCF RESTful service

Source: Internet
Author: User
Tags datetime json

WCF a lot of rest instances online, here is my days to learn and practice through, is a note it.

1. Service Contract

[ServiceContract]public interface  IRESTService{}

In the specific operation definition, there are several parameters to note:

1, WebGet and webinvoke the difference seems to be the definition of method is different, webget use "get", WebInvoke is more flexible.

2, UriTemplate with {value} corresponding parameter list.

3, Webmessageformat includes XML and JSON, there is an online article to achieve raw, has not been carefully studied.

In the case of a simple example, you can customize a return object

1 [OperationContract]
2 [WebGet(UriTemplate = "{name}?token={token}",  ResponseFormat = WebMessageFormat.Json)]
3 Person GetPerson(string name, string token);

And a return picture of the

1 [OperationContract]
2 [WebGet(UriTemplate = "Data/{id}?token={token}")]
3 Stream GetData(string id, string token);

Note the variable type in the definition is string, and the other token is to make a simple authentication in the future, not to think clearly, not in the context of this discussion.

2. Host

It's best to use webservicehost directly,

WebServiceHost restHost = new WebServiceHost(typeof (RESTService),new Uri ("http://localhost/RestService"));

Trouble with ServiceHost, but more flexible.

1 ServiceHost GetRestHost(Uri  baseAddress)
2 {
3         ServiceHost host = new ServiceHost(typeof (RESTService), baseAddress);
4         WebHttpBinding binding = new WebHttpBinding();
5         ServiceEndpoint endpoint =  host.AddServiceEndpoint(typeof(IRESTService), binding,  baseAddress);
6         WebHttpBehavior httpBehavior = new  WebHttpBehavior();
7         endpoint.Behaviors.Add(httpBehavior);
8         return host;
9 }

3. Concrete Operation Example

3.0 first define a simple data contract

1 [DataContract]
2 public class Person
3 {
4     [DataMember]public string Name{get;set;}
5     [DataMember]public DateTime Birthday{get;set;}
6 }

Left a datetime type of data, ready to study the client when used, not all say JS under processing JSON date and WCF is not the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.