First impression of a WCF web API

Source: Internet
Author: User

Every few days, will open the http://aspnet.codeplex.com to see what is dynamic, two days ago accidentally opened and found a new thing, WCF web API, originally I on WWW (WCF, WF, WPF) I don't have a good impression. Although it seems that there is a lot of demand on the recruitment websites, I found that this web API seems to be about rest, so I looked at it and found that it was a pity. Unfortunately, the current web API is still in the preview version, and there are few documents, only Source code With a sample that is almost useless, no results can be found on the Internet. When I saw a sentence two days ago, it probably meant that everyone was thinking about whether or not to rest the previous two years. Now we have begun to think about how to rest. Although there are some examples, on the. NET platform, rest seems to be just a concept. There are very few people studying it, not to mention putting it into production. I have been learning about WCF for a while and have made several examples when I first came out. However, I have never liked the method of referencing the WCF Service and generate a bunch Code And then use the lame *** client to call the service. This raises a question: Do you need to generate two clients to call the same service in two clients? In addition, since it is a service, it should be an "open" thing. Although WSDL is well known, it is too complicated. If you want to use a browser to call the WCF Service, in reality, it seems impossible to call a Web Service (of course, get) from a browser. The web service seems to be not pure. Finally, HTTP is a perfect protocol, this is especially true for operational data. No matter how complicated the requirements are, HTTP will eventually be available for addition, deletion, modification, and query of data. As a truly open and General Service, using the basic HTTP method, there are almost no tasks that cannot be completed. Therefore, for operations on "user" data, there is no need to customize RPC methods such as getuser and createuser by using get, post, put, and delete methods, why do we have to spend some time to know whether to use create or add to add a user? In a sense, every website is actually a web service, isn't it? Data is provided based on HTTP. As long as you enter the URL, it will always present the desired result to your eyes. If such a service is to be provided using a framework similar to WCF, how do people who use this Web Service evaluate it? He will never know whether to add a view or get or display after your "service... however, people who do technology tend to like to think about an infinite number of simple problems, and then develop a solution to their own faults, so as to lure people into being fooled, when it comes to the final full-head package, we will return to the essence of the problem and launch a both-in-one solution. If this is still not thorough, imagine that you need scissors now, however, soon the experts designed the Swiss Army knife, because they thought that you would probably need to use it ..., at the end of the day, you found that you used scissors almost every day, and there were almost no ear spoons, tweezers, bottle starters, and so on. Finally, I took Zhang Xiaoquan in my hand. I have read one sentence before: Please return niub to niub. What I want to say is, please return HTTP back to HTTP, whether at the front end or on the server side.

There have been a lot of things to be seen in the past two days, including ironjs, MongoDB, And the WCF web API. In fact, I am working on a similar framework during this time, is in ASP. net MVC to develop a lightweight rest framework (the framework seems to be a bit up). The basic idea is to use MVC action as the data service and use M $ syndication as the data transmission format, webrequest and webresponse are used to manually initiate requests and return responses to the service. Currently, atom and JSON content are supported, and the core part has been completed, I was going to write a few articles to discuss with my colleagues after I put it into use, but now I have discovered the WCF web API, which makes me very confused, I don't know whether to continue or study the web API first. Yesterday, I read the authoritative guide to MongoDB. I had nothing to worry about this evening and opened the source code package of the WCF web API, I want to know what is going on with this so-called web API, but there is not much content, but because there is a new demand for a project next week, it will take some time off from next week, therefore, you must record what you are reading today, so that you do not know where to leave it in the next 20 days.

To learn more about ASP. the principle of the net application depends on global, httphandler, and httpmodel. In the web API example (contactmanager_advanced), it is not found that httphandler and httpmodel are used, so you can only view global:

 protected void application_start (Object sender, eventargs E) {// use MEF for providing instances var catalog = new assemblycatalog (typeof (global ). assembly); var Container = new compositioncontainer (catiner); var Config = httphostconfiguration. create (). addformatters (New contactpngformatter (), new contactfeedformatter ("http: // localhost: 9000/contact"), new vcardformatter (), new calendarformatter ()). setresourcefactory (New mefresourcefactory (container )). addmessagehandlers (typeof (loggingchannel), typeof (uriformatextensionmessagechannel); setmappings (); routetable. routes. mapserviceroute 
  
    ("Contact", config); routetable. routes. mapserviceroute 
   
     ("contacts", config) ;}
   
  

Obviously, this section is meaningful,

The catalog variable refers to the directory for MEF import. The container is a combination container. With the two object instances, you can create an httphostconfiguration instance using custom formatter, Formatter What is it? If you understand atomentry, you will know that atomentry is actually a format that defines some fixed content (attributes), such as ID, title, summary, and link, for a user object, how does username indicate that this atomentry can only be extended, and this formatter extends this atomentry. Setresourcefactory This method defines a resource factory. What is a resource? In rest, the service that users need to request operations is called a resource. http: // localhost/user is a URL. That's right, but this URL represents the user service, each URL represents a resource, but http: // localhost/user? Id = think8848 and http: // localhost/user are the same resource, which is not difficult to understand, but designing accessible resources is also a complicated topic, I will not describe it in detail here. If you are interested, you can refer to the book restful web service. In this Code, how do I set resources and use resources? MEF of M $ is used here. MEF is a complicated framework. To put it simply, it is a bit similar to dependency injection. M $ defines it as follows: "managed extensibility framework or MEF is a lightweight application for creating scalability. Program . Application developers can use this library to discover and use extensions without configuration. Extension developers can also use this library to easily encapsulate code to avoid generating vulnerable hard dependencies. Through MEF, you can not only reuse Extensions within an application, but also between applications .". That is to say, you can use MEF to obtain some interface instances that meet the criteria (retrieved by the attribute feature. Set a resourcefactory here. When the call arrives, you will find the service instance in this resourcefactory. In our own program, you can use other methods to achieve the resource factory function, for example, the IOC framework. The name addmessagehandlers looks intuitive. As I have observed, we used httprequest to change it to httprequestmessage, so we should regard this message as a request, you can clearly see that addrequesthandler is the processing program for adding requests. Preliminary Prediction , In this processing program, you should be able to do such work as permission control.

In the last two sentences, the keyword is mapserviceroute. This is actually an extension method defined in the WCF web API. The method is to register a urlrouting rule for services of the generic parameter type, it is back to the scope of MVC.

For the WCF web API, debugging the source code is much more complicated than the MVC Framework. Today, we have found a lot of things and we have to digest them later.

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.