Home Backend Development C#.Net Tutorial Flex and .NET interoperability (3): Data access based on WebService (2)

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

Dec 20, 2016 pm 03:18 PM
webservice

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add next-level C compiler How to add next-level C compiler Mar 03, 2025 pm 05:44 PM

This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

What are the alternatives to NULL in C language What are the alternatives to NULL in C language Mar 03, 2025 pm 05:37 PM

This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

Which C language compiler is better? Which C language compiler is better? Mar 03, 2025 pm 05:39 PM

This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

Is NULL still important in modern programming in C language? Is NULL still important in modern programming in C language? Mar 03, 2025 pm 05:35 PM

This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

What are the web versions of C language compilers? What are the web versions of C language compilers? Mar 03, 2025 pm 05:42 PM

This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

Method of copying code by C language compiler Method of copying code by C language compiler Mar 03, 2025 pm 05:43 PM

This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

C language online programming website C language compiler official website summary C language online programming website C language compiler official website summary Mar 03, 2025 pm 05:41 PM

This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

C language compiler installation tutorial (computer version) C language compiler installation tutorial (computer version) Mar 03, 2025 pm 05:41 PM

This tutorial guides users through installing C compilers on Windows, macOS, and Linux. It details installation for popular compilers (MinGW, Visual Studio, Xcode, GCC), explains environment variable configuration, and offers troubleshooting steps

See all articles