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

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

巴扎黑
巴扎黑Original
2016-12-20 15:18:441004browse

In the previous article "Flex and .NET Interoperability (2): Data Access Based on WebService (1)", we introduced access to Webservice through the tag. In fact, we can also dynamically access WebService through programming. Flex SDK provides us with the WebService class.

Using the WebService class to access WebService actually means expressing the attributes of the tag through the attribute form of the class object. In contrast, using the WebService analogy is more flexible than using the tag. Let's take a look at how to connect and call remote methods programmatically:

1 internalfunctiononClick():void
2{
3varservice:WebService=newWebService();
4service.loadWSDL("http: //localhost:1146/FlashFlexService.asmx?wsdl");
5service.addEventListener(ResultEvent.RESULT,onResult);
6service.addEventListener(FaultEvent.FAULT,onFault);
7service.GetBook();
8}

Simply call the remote WebService directly through the loadWSDL() method of the class object, dynamically specify the relevant processing function for the class object, and then call the remote WebService method just like the label.

1 internalfunctiononResult(evt:ResultEvent):void
2{
3Alert.show(evt.result.Id);
4}
5
6internalfunctiononFault(evt:FaultEvent):void
7 {
8Alert.show(evt.fault.faultDetail.toString());
9}

The above completes the use of the WebService class to programmatically access the remote WebService method call.

Let’s take a look at the responsible types such as DataTable returned by WebService, and how to parse them on the Flex client. First define the WebService method as follows:

1[WebMethod(Description="This method will return data of type DataTable")]
2publicDataTableGetDataTable()
3{
4DataTabledt=newDataTable("Books") ;
5dt.Columns.Add("Id",typeof(int));
6dt.Columns.Add("Name",typeof(string));
7dt.Columns.Add("Author",typeof(string) );
8dt.Columns.Add("Price",typeof(double));
9
10DataRowdr=dt.NewRow();
11dr["Id"]=1;
12dr["Name"]=" Flex Game Development";
13dr["Author"]="Zhang San";
14dr["Price"]=54.85;
15dt.Rows.Add(dr);
16
17dr=dt.NewRow();
18dr["Id"]=2;
19dr["Name"]=""Flash Game Development"";
20dr["Author"]="李思";
21dr["Price"]=65.50;
22dt.Rows.Add(dr);
23
24returndt;
25}

It can also be accessed through WebService on the Flex client. The following is accessed using the tag (note here, < The name of the mx:operation> tag must have the same name as the WebService method on the server side):

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

The WebService is provided and the client is connected After removing WebService, now all we have to do is call the remote method provided by WebService. As follows:

1
23dataProvider="{this.myService.GetDataTable.lastResult.Tables.Books.Rows}">
4
5
6
7
8
9

10
11
12
13

14

Bind the data source of the DataGrid component through the dataProvider property of DataGrid, in addition to directly through " {}"In addition to the help of binding expressions, we can also specify the data source for the DataGrid in the handler function that successfully calls the remote method. See the commented code part in the above code. {this.myService.GetDataTable.lastResult.Tables.Books.Rows} means that all rows of the result (DataTable) returned by the remote WebService method GetDataTable() are used as data sources to bind to the DataGrid component, where Books is the name of the data source DataTable. ,

DataSet, DataTable have a big gap in performance compared to generic collections, and the complex serialization and deserialization processes are also very responsible. I have always preferred generics since .net 2.0 launched them. Use generics to pass big data. OK, now I will introduce how to process the generic collection data returned by the WebService method in Flex. We have the following WebService method definition:

1 [WebMethod(Description="This method returns a generic collection")]
2publicListBookList()
3{
4returnnewList
5{
6newBook
7{
8Id=1,
9Name=""Flex Game Development"",
10Author="Zhang San",
11Price=54.85
12},
13newBook
14{
15Id=1,
16Name=""Flash Game Development"",
17Author="李思",
18Price=65.50
19}
20};
21}

Compared with DataSet and DataTable types, I personally use List<> to return data I think it's easier to handle.

This is the data form returned in the form of generic combination (List<>). Compared with the return result of DataTable, it is more concise and clear. Having said that, how do we get this return value and process this value in Flex? In fact, it has been clearly shown here how we can handle it. If you look carefully at the picture above, you will find "ArrayOfBook"? ? ? ? What is this? Could it be that the client can get this return value in the form of an array. In order to further understand the details of this, we need to go deep inside to understand the specific structure of the return value. Through the debugging environment of Flex Builder, we can get the following information:

Do you see it clearly? There are two objects under the lastResult structure set of the BookList method. Clicking on the node shows that it is the two Book objects we returned through List, and the type of lastResult is: mx.collections.ArrayCollection. This is not really an array in ActionScript. Assemble? Okay, in this case, the Flex client can directly get the generic collection data returned by WebService through lastResult. The following code block:

1
2
3
4
5< mx:DataGridColumnheaderText="Book title" dataField="Name"/>
6
7
8

9
10
11
12
13

That’s it for introducing the data access of WebService. Due to my limited personal ability, I hope everyone can correct me if there are any deficiencies in the article. If you have any good suggestions, you can also put them forward, let everyone discuss, learn and make progress together! !


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