WCF rest work summary (1)

Source: Internet
Author: User

Recently, I learned how to use WCF rest. Rest has many benefits: efficient, simple, and resource-oriented, and client calls have become very simple. For more information about getting started with rest, visit the Internet to share the problems encountered and solutions ~

I. Prepare the environment

Using WCF rest, you can download a template and preview it roughly.

After the template is downloaded, select this template for the new project. By default, a crudCodeNew friends can use this to quickly familiarize themselves with rest-style WCF.

This also provides us with a very convenient function help. You only need to add help after our service to display the service details and introduction.

2. Build a rest WCF Service

1. Interface implementation

When our request is get, we generally use [webget] to obtain data, and add, modify, delete, and use the method in [webinvoke] to decide whether to use post or put. Pay attention to the following:

The value in method must be uppercase post put.OtherwiseHTTP Error 405 405This method is not allowed for the resource identified by the request. It is not allowed to use the method specified in the request line.

We can also useResponseformatAndRequestformatTo specify the format of accepted and returned parameters.

2. For the class inheritance interface, first paste part of the basic CRUD

3. Register a route in global

Has a friend who has used MVC found it very much like MVC after writing it ~

Iii. Implementing the call service

1. Implement get service

Simply put, you can directly input http: // localhost: 55914/itaskservice/tasks/State/1 in a browser to get all data with state 1 transmitted in JSON format.

The rest-style WCF eliminates the need to add service references to clients as before.Without WSDL, It is concise, and a lot of efficiency is improved, making access easier.

Rest sends a request because there is no proxyOur client can be accessed by JS. If jquery only needs $. Get to send a request, I will mainly discuss the access method of C #.

The WebClient method is better to encapsulate this class. It is simple to call, but it is not easy to implement some functions due to excessive encapsulation.

Webrequest is flexible.

The above two classes are frequently used. Here we recommend that you use one of the WCF tools to access the rest service very easily.

Httpclient is also a tool class developed by Microsoft and integrated in the latest. Net 4.5.

You can submit any of the above three items.

But here is a problem. We used to get the proxy class by calling the method. But now we don't have a class. What we get is a string of JSON-format data. How can we make it as convenient as a class? what about the operation?

For example, Company A has released the rest WCF Service data format. Company B wants to call Company A's services. Previously, both WS and WCF had proxies to generate proxy classes. Now, what we get is JSON data, but we haven't entity class to be deserialized

For example, I want to display the data as a table.

There are two methods

1. Create a class against the JSON Data Client and deserialize JSON into a class object through deserialization

In this way, there are few troublesome classes. If there are more classes, you need to create many entity classes. The more important thing is that the entity classes on the server have updated the services under the old WCF or ws update. Now you need to update the client entity classes. isn't it very troublesome?

Therefore, the following method is recommended.

2. Convert JSON deserialization to dynamic type

JSON is converted to dynamic type. At first, I used a third-party json.net. After trying to convert JSON to a set type, I always encountered an error. If someone knows how to get it, I 'd like to inform you.

Later, I tried to useJavascriptserializer

The method of use is simple, no matter whether it is a serialized set or a single one.This time will be displayed normally.~ The code above shows the accepted JSON type as a table.

Okay, this problem is solved. You can send the GET request in the last three methods. After all, it's easy to read the code directly.

We can see that the first and third are very simple. Here, we should note that the encoding problem is caused by garbled characters in Chinese.

So pay attention to the encoding problem. In the second one, you can get the response. contenttype first to get the encoding so that you can automatically identify the encoding format.

Besides, the actual request sent by httpclient is in message. ensurestatusissuccessful ();

Through any of the above methods, we can get JSON and then passConverts JSON deserialization to dynamic.It can be as simple as an object class ~~

Write it here today

Next we will talk about post and other submitted data and upload and download of native stream files.

Finally, I wish you a pleasant weekend. Welcome to the technical exchange.

Refer:

Http://blog.csdn.net/fangxinggood/article/details/6235662

Original article:

Http://www.cnblogs.com/wlflovenet/archive/2011/10/28/WCFREST.html

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.