Due to work needs, now I want to implement interaction between flex and WCF, saying that WCF is an upgraded version of WebService, So If flex and WCF can interact with each other, flex Interacts With WebService (because the interaction mode of WCF is essentially a combination of MSMQ, WebService, and so on, I think the interaction between flex and WebService must be exactly the same as that between WCF, so in the following articles, I will introduce both WCF and WebService together. To be honest, I did not know anything about flex before I got in touch with flex and WCF (WebService) interaction, so I collected it online. Finally, we will summarize all the content on the Internet. Now we can write a process that takes only 2 minutes to complete the interaction between flex and WCF (WebService.
This article will list the interaction steps, principles, and codes between flex and WCF (WebService). Therefore, if you want to complete the operation in 2 minutes, you only need to jump to the description of the Flex operation steps and follow the instructions without reading the principles. If you want to look at the principles and code, it may take additional time to increase based on your personal infrastructure. Okay, I don't want to talk much about it. Now we are working on it. If you have any questions or errors in the article, please criticize and correct them. My email address is: k.liuxu@gmail.com,
My QQ is: 583936908.
(1) Development Environment:
Flex: Developed Using Flash builder 4.5 [language: ActionScript]
WCF (WebService): Uses Visual Studio 2008 [Language C #] (the host is slow, the hard disk is small, and vs2010 cannot be installed)
(2) create a web service:
We need to do this experiment. There is no doubt that we need a web service to provide services for us. Therefore, we need to create a web service ). if you already have a WCF (WebService) reader, you can skip this step and go directly to the (3) Section. Perform the flex operation step to complete flex and WCF (WebService) within 2 minutes) interactive strategy.
(2.1) Open vs2008 and select the WCF template (WebService creation is different from WCF creation, but the services provided are the same, so the WebService production process will not be detailed, in this case, only the creation process of WCF is described ). Below we assume that we are doing a service that provides users with a weight loss diary.
(2, 2) Compile three service methods provided.
The first one is: String GetUserName (string strname). This function is used to input a user name and returns the string "Hello, This username word ".
The second is: userbase getuserbasebyname (string strname). The function is to input the user's name and return an object of the user's information. For details about the returned object, refer to the following code:
The third is diary getdiarybyuserbase (userbase ubobj). The function is to pass in the basic object of the user and return an object about the user's weight loss diary. For details about the returned object, see the following code.
In my opinion, I have explained the above three methods, basically covering all the return values in the operation and the method of passing values. Readers should think it is enough. If you still feel that other methods of passing values and returning values are required, you can send me a letter or add QQ to continue the discussion.
The related service code is not described in detail, as follows:
(, 1) create a service contract related to WCF (due to limited space, the code can be indented as much as it is readable)
(, 2) implement the service contract of WCF (due to limited space, the code can be indented at full readability)
(, 3) check whether the WCF has been connected and publish the three methods
(, 4) Since WCF uses the protocol standard of soap1.1 by default (to put it bluntly, it is the protocol used by WebService, so we need to configure the WCF configuration file, and modify it to the soap 1.1 protocol standard so that flex can correctly link to the WCF Service). modify the configuration file as follows: Delete the content of the box and add the circle content.
Well, now our WCF Service has been created (WebService is almost created and will not be described). The rest of the work is that we use flex to interact with this service.
(3) Flex interaction steps
This step is the most exciting moment in this article. This step allows us to complete service interaction in 2 minutes.
(3, 1) We spent 10 seconds opening the flex development environment, flash builder 4.5.
(3, 2) It takes 5 seconds to create a flex project by following the steps
() It takes us 20 seconds to follow the steps shown in the figure to open the WebService link service address, enter the content, and obtain the WCF Service we just created (actually I think it is the WebService Service linked) (Click data in the menu bar and select link web service)
(3, 3, 1) the following link dialog box is displayed. in the address bar, enter the address of our WCF or WebService. For example, note that when entering the address, you must add "?" at the end of the address. The WSDL character. Otherwise, an error is reported.
(, 2) Click Next. We can see that the method we created with WCF has been listed by Flex. Note that if you cannot proceed to this step, return the content of the second section (2, 2, 4) above. It is possible that your WCF Service is not correctly configured. At the same time, pay attention to the time for flex to parse WCF, which is much longer than WebService. For example, it took me 15 minutes to resolve this issue (of course, this should be excluded from 2 minutes, but it is not the operation time after all ). It took me 50 minutes to resolve a company's WCF. So if you want to parse your WCF, please wait.
(3, 4) Click "finish" and wait for 5 seconds. We can see that the methods and related objects in our services and services are listed in the service status. At the same time, in our project file, the operation class and package of the WCF Service are automatically added to us. Now we can operate on our service methods.
() It took us 40 seconds to connect to the WCF Service. After everything is set up for service communication, it takes 50 seconds to run the following code to operate the three services in WCF. (I spent more than 40 seconds to finish this Code (the layout code is automatically generated by visual drag). "Believe it or not, I believe it." --- reference it to the Ministry of Railways spokesman)
(3, 5, 1) This is the code for calling the first method and
(CODE)
(Effect)
(, 2) This is the code for calling the second method and
(CODE)
(Effect)
(3, 5, 3) This is the code for calling the third method and
(CODE)
(Effect)
(3, 5, 4) the entire mxml code is as follows:
() So far, it took 90 seconds to complete the interaction between flex and WCF (WebService) services. Is it nice? Now we have 30 seconds left for you to continue with the analysis of the following principles.
(, 1) In fact, the principle and process are very simple. First, we create a remote service object, which is actually a remote service proxy, it is responsible for interacting with remote services. Therefore, we use the code:
VaR sobj: service1 = new service1 (); create our proxy object
(, 2) after a proxy object is created, we can use this proxy object and then click a number of methods that can be called on the server. However, when we call these methods, we do not actually know when the returned value will be returned. Therefore, we use an asynchronous token object to uniquely mark the called method, when there is a return value, it is used. So the code is used:
VaR atobj: asynctoken = sobj. getdiarybyuserbase (ubobj); get our asynchronous Token Object
(, 3) after obtaining the asynchronous token object, the asynchronous Token Object distributes the value to a responder. Now your manager receives a command from a superior, but the manager does not answer the question specifically. He gives the answer right to his subordinates (Response object) to handle this problem. Therefore, the following code is available:
VaR rpobj: Mex. rpc. responder = new MX. rpc. Responder (button3_result, button3_fault );
Atobj. addresponder (rpobj );
Because the responder (subordinate) has relatively small permissions, no matter whether the asynchronous Token Object (manager) task can be completed or not, it must be processed unconditionally. Therefore, there is a button3_result, for successful operations, button3_fault processes failed operations.
(3, 6, 4) at last, the following two methods are used: button3_result and button3_fault.
(, 5) Well, at this point, it should be about two minutes. We have completed flex's interaction with WCF (WebService. It should be easy to understand.
(4) summary:
This article is a simple and detailed article about flex and WCF (WebService) that I have summarized after reading almost all the web service and flex interaction articles on the Internet) this article is intended to serve as a reference for compatriots who are new to flex and WCF (WebService) interactions. I also want to explain that many articles on the Internet suggest using an asynchronous token object instead of the listener (addlistener) method to implement interaction between flex and WCF (WebService, I don't think it's right or wrong. In my opinion, since asynchronous tokens can be used for simple and efficient interaction, why not? Haha, haha, so I am standing on the side of the asynchronous token object. However, I don't think addlistener is out of the market. After all, many things have their own special advantages. If the above content is wrong, please mail to k.liuxu@gmail.com, contact QQ: 583936908.
Original article, labor success is hard to come by, please indicate the source when Reprinting
(5) download:
Download the WCF Service Program:
Http://www.aozoz.com/Code/KFlexWCF/LoseWeightService.rar
Flex project file download:
Http://www.aozoz.com/Code/KFlexWCF/KLoseWeight.rar
Download the Word document:
Http://www.aozoz.com/Code/KFlexWCF/KFlexWCF.doc
The above files are stored on my server and I don't know when the problem will occur. If you cannot download the file, contact me in time.