WCF defines soap and rest-style webservice

Source: Internet
Author: User

Excerpt from other posts, which are recorded for later use.

1. Define a service data contract (SOAP is the same as rest)Public class Employee
   {

[DataMember]
Public string Id {get; set;}
    

[DataMember]
Public string Name {get; set;}
    

[DataMember]
Public string Department {get; set;}
   

[DataMember]
Public string Grade {get; set;}
   

Public override String ToString ()
      {
return string. Format ("ID: {0,-5} name: {1,-5} level: {2,-4} Department: {3}", ID, Name, Grade, Department);
      }
  } 2. Define a service behavior contract (SOAP is the same as rest)

Next we define the following interface Iemployeesservice, which represents the service contract. Unlike SOAP-based service contract definitions, we do not need to apply the OperationContractAttribute attribute on the appropriate action method, but the ServiceContractAttribute feature applied on the interface/class is still required. The substitution of the OperationContractAttribute attribute here is WebGetAttribute and WebInvokeAttribute, which are defined in the System.ServiceModel.Web assembly.

--rest

[ServiceContract]
public interface Iemployees
{
[WebGet (UriTemplate = "All")]
Ienumerable<employee> GetAll ();


[WebGet (UriTemplate = "{id}")]
Employee Get (string id);


[WebInvoke (UriTemplate = "/", Method = "POST")]
void Create (employee employee);


[WebInvoke (UriTemplate = "/", Method = "PUT")]
void Update (employee employee);


[WebInvoke (UriTemplate = "{id}", Method = "DELETE")]
void Delete (string id);

}



--soap

[ServiceContract]

public interface Iemployees
{
[OperationContract]
Ienumerable<employee> GetAll ();


[OperationContract]
Employee Get (string id);


[OperationContract]
void Create (employee employee);


[OperationContract]
void Update (employee employee);


[OperationContract]
void Delete (string id);

}

The contract interface Iemployeesservice defines 5 operations, respectively, for obtaining, adding, modifying, and deleting employee data. In accordance with the rest design principle, we represent the employee information being manipulated as a network resource, and the type of operation is best matched to the corresponding HTTP method. The match between the action type of the resource and the HTTP method in the action method is reflected by the WebGetAttribute and WebInvokeAttribute attributes applied to them.

WebGetAttribute is for the Get method, while the other HTTP methods are represented by the method property of WebInvokeAttribute. In Iemployeesservice, both the GetAll and get methods used to get employee information are applied WebGetAttribute features, while the other Create, The update and Delete methods apply the WebInvokeAttribute attribute, and the method property is set to put, POST, and delete, respectively.

Both WebGetAttribute and WebInvokeAttribute have the same property uritemplate, which is used to define the template as the final action URI. Not only can we specify a static path relative to the endpoint address through the UriTemplate property, but we can also implement a mapping between the dynamic part and the parameter in the path through the placeholder.

Also, as an example of the 5 action methods defined in the contract interface Iemployeesservice, if the endpoint address is http://127.0.0.1:3721/employees, Because the uritemplate of the getall operation that is used to return all the employee lists is set to "all", its address is http://127.0.0.1:3721/employees. The UriTemplate for the get operation that returns the specified employee ID is set to "{ID}", which means that we specify the employee ID directly in the URI that represents the requested address, and it is automatically mapped to the parameter ID of the action method. The delete operation for deleting a specified employee has the same UriTemplate setting and is used to create and update operations that add new employees and modify existing employee information, because the employee object as a parameter has an id attribute, so the endpoint address is taken directly.

WCF defines soap and rest-style webservice

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.