This article takes. NET platform under the C # language development WebService as a remote data source, detailed introduction of flex and. NET Data communication knowledge points webservice, including connection WebService, Remote Call WebService method, The WebService method is used to transfer parameters and other related knowledge points. The use of the three tags is basically the same, and here the <mx:WebService> tag as an example to introduce.
First look at the following code block:
Copy Code code as follows:
<mx:webservice id= "DataService"
Wsdl= "HTTP://LOCALHOST/FLASHFLEX/DATAWEBSERVICE.ASMX?WSDL"
Useproxy= "false" >
<mx:operation name= "HelloWorld" result= "onsuccess (event)" fault= "Onfault (event)"/>
<mx:operation name= "GetBook" fault= "Onfault (event)" result= "Onobjectsuccess (event)"/>
</mx:WebService>
The WSDL property specifies the WSDL address of the WebService to be accessed, which defines two action labels (<mx:operation>), respectively, corresponding to the WebMethod method defined in WebService. The result property tag accesses the successful handler function of the WebService method; fault, instead, specifies the handler function that accesses the failure. The above two <mx:operation> WebMethod methods corresponding to WebService are as follows:
<summary>
return string
</summary>
<returns></returns>
[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}
<summary>
Returns a Simple object
</summary>
<returns></returns>
[WebMethod]
Public book GetBook ()
{
Return to new book
{
Id = 1,
Name = "The Kingdoms",
Author = "Luo Guan Zhong",
Price = 100
};
}
For example, the WebService method definition and the full process of accessing the WebService through the <mx:WebService> tag of the Flex client (Mxml) Let's take a look at how the flex client invokes the method defined by WebService:
Copy Code code as follows:
<mx:Script>
<! [cdata[
Import Mx.controls.Alert;
Import mx.rpc.events.FaultEvent;
Import mx.rpc.events.ResultEvent;
/**
* Initiate request to WebService--call HelloWorld method, DataService ID for <mx:WebService>
* */
Internal function onrequest (): void
{
Dataservice.helloworld ();
}
/**
* Request successful processing return results
* */
Internal function onsuccess (evt:resultevent): void
{
Alert.show (Evt.result.toString ());
}
/**
* Request failed processing function
* */
Internal function Onfault (evt:faultevent): void
{
Alert.show ("Access WebService failed!");
}
]]>
</mx:Script>
Through the above call, you can complete a flex and. NET WebService interaction. Of course, our client invocation of Flash/flex WebService is also a parameter that can be passed, as follows the WebMethod definition of WebService:
Copy Code code as follows:
<summary>
Converts the passed in parameter to uppercase character returns
</summary>
<param name= "Value" ></param>
<returns></returns>
[WebMethod]
public string Converttoupper (string value)
{
return value. ToUpper ();
}
By configuring <mx:operation> executing this method under the <mx:WebService> tab, you can access the following:
<mx:operation name= "Converttoupper" result= "onsuccess (event)" fault= "Onfault (event)"/>
/**
* Initiate a request to WebService
* */
Internal function onrequest (): void
{
Dataservice.helloworld ();
Dataservice.converttoupper ("ABCDEFG");
}
In addition, we can pass the parameters through <mx:request>, here only need to know <mx:request></mx:request> The parameter configuration in is the same as the WebMethod method parameter provided by WebService.
Back to the previous look at the WebService method definition, one of the methods Getbook is returned by a book object, if the object is returned how do we get the value of this object in the Flex client? See the following code example in detail:
Copy Code code as follows:
Internal function onobject (): void
{
Dataservice.getbook ();
}
Internal function onobjectsuccess (evt:resultevent): void
{
Get the return value directly through the result property of the event, and then access the property directly OK
Alert.show (Evt.result.Name);
}
/**
* Request failed processing function
* */
Internal function Onfault (evt:faultevent): void
{
Alert.show ("Access WebService failed!");
}
This completes the server-side WebService call to return the object to the client.