Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply REST in WCF

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use REST in WCF". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use REST in WCF.

1: create a new WCF service application named RestWCF and choose to host it in the new site.

2: create a new entity class Product with the following code:

[DataContract] public class Product {[DataMember] public Guid Id {get; set;} [DataMember] public String Name {get; set;}}

3: create a new service contract IProductService:

[ServiceContract] public interface IProductService {[OperationContract] [WebGet (UriTemplate= "Product")] List GetProducts (); [OperationContract] [WebInvoke (UriTemplate= "Product", Method= "PUT")] bool CreateProduct (Product product); [OperationContract] [WebInvoke (UriTemplate= "Product", Method= "POST") bool UpdateProduct (Product product); [OperationContract] [WebInvoke (UriTemplate= "Product", Method= "DELETE")] bool DeleteProduct (Product product);}

Note that there are some CRUD operations here, but in addition to OperationContract decorations, there are WebGet,WebInvoke features to decorate.

UriTemplate means Uri template, if the web address is http://www.rest.com. Then the address of the rest service is http://www.rest.com/Product.

OK. The above contract roughly indicates

GET-GetProducts method.

PUT-CreateProduct.

POST----UpdateProduct

DELETE---DeleteProduct .

4: create a new service class: ProductService, and implement the service interface. The code is as follows:

Public class ProductService:IProductService {public List GetProducts () {return new List () {new Product () {Id=Guid.NewGuid (), new Product () {Id=Guid.NewGuid (), Name= "222"}, new Product () {Id=Guid.NewGuid (), Name= "333"},};} public bool CreateProduct (Product product) {return true;} public bool UpdateProduct (Product product) {return true;} public bool DeleteProduct (Product product) {return true;}}

Of course, in fact, it must be a database operation, so let's Mock it here.

5: create a new ProductService.svc file with the following code:

There is one thing to note here. Service corresponds to the full name of the containing class = namespace + class name.

6: configure the web.config file.

Here, because we want to configure the Rest service, we add webHttp Behavior and enable helpEnabled. Exe. Here we set the defaultBodyStyle= "Bare" to represent no packaging.

DefaultOutgoingResonseFormat= "Json" means the default output format is json, and if not set, the default is xml.

7: browse in the browser because we enabled HelpEnabled. So the address we entered is:

8:OK, we have configured the service, so let's test it and open fiddler.

The results are as follows:

Here we submit the POST request.

Next time we will use Silverlight to invoke Get,Post,Put,Delete 's Rest service.

Oh, I forgot, these are all uppercase, so you * remember GET,POST,PUT,DELETE.

At this point, I believe you have a deeper understanding of "how to apply REST in WCF". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report