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.