Home  >  Article  >  Backend Development  >  Flex and .NET interoperability (2): Data access based on WebService (1)

Flex and .NET interoperability (2): Data access based on WebService (1)

巴扎黑
巴扎黑Original
2016-12-20 15:17:131100browse

Flex provides , and tags to directly access remote data, which is used to develop remote server data sources provided by various different language environments (such as WebService) makes data interactive communication easier.

This article uses WebService developed in C# language under the .NET platform as a remote data source to introduce in detail the data communication knowledge points between Flex and .NET's WebService; including connecting to WebService and remotely calling WebService methods , passing parameters and other related knowledge points to the WebService method. The usage of the three tags is basically the same. Here we take the tag as an example.

First take a look at the following code block:

12wsdl="http://localhost/FlashFlex/DataWebService.asmx?wsdl"
3useProxy=" false">
4
5
6

wsdl attribute can be specified to the wsdl address of the WebService to be accessed, which defines two operation tags ( ), respectively corresponding to the WebMethod method defined in WebService. The result attribute marks the processing function after successful access to the WebService method; fault, on the contrary, specifies the processing function for access failure. The above two correspond to the WebMethod methods of WebService as follows:

1///


2///return string
3///< /summary>
4///
5[WebMethod]
6publicstringHelloWorld()
7{
8return"HelloWorld";
9}
10
11///
12///Return a simple object
13///

14///
15[WebMethod]
16publicBookGetBook()
17{
18returnnewBook
19{
20Id=1,
21Name="Romance of the Three Kingdoms",
22Author="Luo Guanzhong",
23Price=100
24};
25}

The above is the WebService method definition and passed in the Flex client (mxml)< mx:WebService> tag to access the complete process of WebService. Let’s take a look at how to call the methods defined by WebService on the Flex client:

1
2< ![CDATA[
3importmx.controls.Alert;
4importmx.rpc.events.FaultEvent;
5importmx.rpc.events.ResultEvent;
6
7/**
8* Initiate a request to WebService - call the HelloWorld method, dataService is the id of
9**/
10internalfunctiononRequest():void
11{
12dataService.HelloWorld();
13}
14
15/**
16*Request result returned after successful processing
17**/
18internalfunctiononSuccess(evt:ResultEvent):void
19{
20Alert.show(evt.result.toString());
21 }
22
23
24/**
25*Handling function for request failure
26**/
27internalfunctiononFault(evt:FaultEvent):void
28{
29Alert.show("Failed to access WebService!");
30}
31]]>
32

Through the above call, you can complete an interaction between Flex and .NET WebService. Of course, we can also pass parameters when calling WebService on the Flash/Flex client. The following WebMethod definition of WebService is as follows:

1///


2///will be passed in Convert the parameters to uppercase characters and return
3///

4///
5///
6[WebMethod]
7publicstringConvertToUpper(stringvalue)
8{
9returnvalue.ToUpper();
10}

You can access it by configuring under the tag to execute this method, as follows :

1

1 /* *
2*Initiate a request to WebService
3**/
4internalfunctiononRequest():void
5{
6//dataService.HelloWorld();
7dataService.ConvertToUpper("abcdefg");
8}


In addition, we can also pass to pass parameters. Here you only need to know that the parameter configuration in has the same name as the WebMethod method parameter provided by WebService.

Go back to the front and take a look at the method definition of WebService. One of the methods, GetBook, returns a Book object. If it is the returned object, how do we get the value of this object on the Flex client? For details, see the following code example:

1internalfunctiononObject():void
2{
3dataService.GetBook();
4}
5
6internalfunctiononObjectSuccess(evt:ResultEvent):void
7{
8 //Get the return value directly through the event's result attribute, and then access the attribute directly to OK
9Alert.show(evt.result.Name);
10}
11
12/**
13*Handling function for request failure
14**/
15internalfunctiononFault(evt: FaultEvent):void
16{
17Alert.show("Access to WebService failed!");
18}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn