Flex and. NET Interoperability (ii): WebService-based data access (top)

Source: Internet
Author: User
Tags uppercase character wsdl
Flex provides <mx:WebService>, <mx:HTTPService>, and <mx:RemoteObject> tags for direct access to remote data, This makes it easier to communicate data interactively with remote server-side data sources (such as WebService) that are developed by various locales.

This article takes. NET platform under the C # language development of WebService as a remote data source, details of flex and. NET WebService communication knowledge points, including connection WebService, Remote Call WebService method, Pass the relevant knowledge points such as parameters to the WebService method. Three tags are used basically the same way, here is the <mx:WebService> tag as an example to introduce.

First look at the following code block:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>-->1<mx:webserviceid= "DataService"
2wsdl= "HTTP://LOCALHOST/FLASHFLEX/DATAWEBSERVICE.ASMX?WSDL"
3useproxy= "false" >
4<mx:operationname= "HelloWorld" result= "onsuccess (event)" fault= "Onfault"/>
5<mx:operationname= "GetBook" fault= "Onfault (event)" result= "Onobjectsuccess"/>
6</mx:webservice>

The WSDL attribute specifies the WSDL address of the WebService to be accessed, which defines two action labels (<mx:operation>) corresponding to the WebMethod method defined in WebService. The result property tag accesses the handler function after the WebService method succeeds; fault, instead, specifies the handler for the access failure. The above two <mx:operation> WebMethod methods corresponding to WebService are as follows:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1///<summary>
2///return string
3///</summary>
4///<returns></returns>
5[webmethod]
6publicstringHelloWorld ()
7{
8return "HelloWorld";
9}
10
11///<summary>
12///returns a Simple object
13///</summary>
14///<returns></returns>
15[webmethod]
16publicBookGetBook ()
17{
18returnnewBook
19{
20id=1,
21name= "Kingdoms",
22author= "Luo Guan Zhong",
23price=100
24};
25}

As above is the WebService method definition and the full process of accessing WebService through <mx:WebService> tags on the flex client (mxml). Let's take a look at how the flex client is going to invoke the method defined by WebService:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1<mx:script>
2<! [cdata[
3importmx.controls.alert;
4importmx.rpc.events.faultevent;
5importmx.rpc.events.resultevent;
6
7/**
8* request to WebService--call HelloWorld method, DataService ID for <mx:WebService>
9**/
10internalfunctiononRequest (): void
11{
12dataservice.helloworld ();
13}
14
15/**
16* request successfully processed return result
17**/
18internalfunctiononSuccess (evt:resultevent): void
19{
20alert.show (Evt.result.toString ());
21}
22
23
24/**
25* processing function for request failure
26**/
27internalfunctiononFault (evt:faultevent): void
28{
29alert.show ("Access WebService failed!");
30}
31]]>
32</mx:script>

With the above call, you can complete the interaction of a flex and. NET WebService. Of course, our client call to WebService in Flash/flex is also a parameter that can be passed, with the following WebService WebMethod definition:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1///<summary>
2///converts the passed in parameter to uppercase character return
3///</summary>
4///<paramname= "Value" ></param>
5///<returns></returns>
6[webmethod]
7publicstringConvertToUpper (StringValue)
8{
9returnvalue. ToUpper ();
10}

This can be accessed by configuring <mx:operation> executing the method under the <mx:WebService> tab, as follows:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>-->1<mx:operationname= "Converttoupper" result= "onSuccess (event) "Fault=" Onfault (event) "/>

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>-->1/**
WebService initiated a request to the
3**/
4internalfunctiononRequest (): void
5{
6//dataservice.helloworld ();
7dataservice.converttoupper ("ABCDEFG");
8}


In addition, we can pass parameters through <mx:request>, here we just need to know <mx:request></mx:request> The parameter configuration in the WebService is OK with the same name as the WebMethod method parameter provided by the.

Back to the front look at the WebService method definition, where one method Getbook is to return a book object, and if it is the object returned by our flex client how to get the value of this object? See the following code example in detail:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1internalfunctiononobject (): void
2{
3dataservice.getbook ();
4}
5
6internalfunctiononObjectSuccess (evt:resultevent): void
7{
8//directly through the result property of the event to get the return value, and then directly access the property is OK
9alert.show (Evt.result.Name);
10}
11
12/**
13* processing function for request failure
14**/
15internalfunctiononFault (evt:faultevent): void
16{
17alert.show ("Access WebService failed!");
18}

  • Related Article

    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.