Data binding is a feature introduced back in Flash MX 2004 Professional. It allows you to tie properties of components to one another that will automatically update when certain component events fire. It makes creating web-service-driven applications that much easier because there is very little code required. In fact, you will see that the preceding example can be done in a single line of code.
Now test the movie again. Again, you get the same results as before, but the difference is that the only ActionScript in this file is the line that activates the webServiceConnector component. Now that we are interacting successfully with our web service and have found the easiest way to work with them, let's actually make a useful web service. In the following example, you will create a web service that accepts a parameter, then squares it, and returns the results. Open a text editor, create a new file and enter the following code, and then save the file to your web root directory as squareService.asmx. <%@ WebService Language="c#" debug="true" %> using System.IO; using System.Web.Services; public class Square: System.Web.Services.WebService{ [WebMethod(Description="Square the number")] public int squareNum(int sentNum) { int myReturn = sentNum*sentNum; return myReturn; } } Much like our previous web service, this one starts off by declaring that it is a web service written in C#. It then grabs the object classes we need. Next, it declares the web service class. After that, we declare the web method and set its description. Then we create the web method. Notice we declare that the result being returned will be an integer. And the parameter being sent will also be an integer. Save this file and then browse to it and choose the squareNum web method. You will see a screen like Figure 25.9. As you can see, the web service has provided a field where we can place a number to test, so enter the number 8 and click Invoke. You should then see another browser screen like Figure 25.10, indicating that it worked. Figure 25.9. The web service provides a field to test the web method.Figure 25.10. Use parameters in web methods to make web services work for you.Now we will use the webServiceConnector component to create a small app that will use this web service. Again, the only code we will need is the code that will activate the connection. But before you can use the web service, you have to add the WSDL to the Web Services panel, so do that first (http://localhost/squareService.asmx?WSDL).
The preceding code is nothing new; we create the event listener object. Then we set the event to the listener that will trigger the webServiceConnector to activate. And finally, we add the event listener to the Button component. Now that you have seen how to create and consume web services, let's create an application that will use a web service that we did not create. |