Flex interacts with ASP. NET through remoting

Source: Internet
Author: User

Flex interacts with ASP. NET through remoting

Instructions

This article is about flex and ASP. net Communication through remoting. This article uses an example to introduce the configuration required by the server and client for Data Interaction between flex and ASP, as well as the communication process between the two.

1. Software

1. Flex platform: Adobe Flex builder 4.5

2.. NET platform: Visual Studio. NET 2008

3. remoting Gateway: Fluorine

2. Introduction

Flex and. the communication between the net server and the server can be completed through components or classes such as WebService, httpservice, urlloader, and fielreference. It is very convenient and simple to communicate with the server through these methods, however, their disadvantage is that the communication data volume is small. to transmit a large amount of data or realize serialized transmission of different objects, they cannot meet the requirements and need to find another communication protocol, another efficient transmission protocol to replace the SOAP protocol transmission scheme is AMF (ActionScript
Message format) protocol.

Fluorine is an open-source AMF (ActionScript messaging formatter) gateway dedicated to data type conversion when flex interacts with. net. Fluorine now supports ActionScript 2.0 and ActionScript 3.0. fluorine can also be used as the AMF gateway when Flash interacts with. net.

Fluorine has the following advantages:

(1) automatic data type conversion during interaction. Because flex and. Net have different data types, such as flex date and. Net datetime. In this way, the data type must be converted during data interaction between flex and. net. This type of data type conversion can be achieved by writing relevant Code. For example, compile an entity class in flex and. Net to convert the data obtained by the entity class. Fluorine can be used for data interaction. net and the data types in Flex are automatically converted, which relieves us from the trouble of processing their data types to a certain extent.

(2) improved interaction efficiency: using the gateway for data interaction, it enables flex to directly interact. NET data processing class for communication, instead of using another layer of intermediate data interaction layer. To a certain extent, its interaction efficiency will be improved a lot.

3. basic configuration 3.1. Server Creation

(1) install fluorinefx. After installation, there will be "Source" and "samples" folders in the directory. The "samples" folder contains some examples of using fluorine in vs environment. The "Source" folder contains the source code for fluorine.

(2) First, determine the storage location after the project is created. For example, in this example, all files of this example project are tentatively stored in D: \ demo, and create a file directory D in disk D: \ demo \ fluorine, right-click the default IIS website and choose create virtual directory

(3) After fluorine is installed, the system automatically adds a template: fluorinefx ASP. NET web application in vs. net 2008. Select this template to create a. Net website.

(4) After the vs template is created, the system automatically loads some references, creates related files, and performs simple configuration. Project Structure after creation:

The DLL in "bin" is the Assembly generated from the fluorine source file, and "templates" is some templates. The WEB-INF/Flex contains xml configuration files. "Gateway. aspx" is an empty page, and its webpage address is fluorine's gateway address.

There are two ways: first, create a fluorinefx servicelibrary class library file, put all data processing classes in the library, and then call such libraries on the website (for example, step 5-7 below ); second, place all data processing classes in ASP. Net folder app_code, app_code,. Net automatically compiled]

(5) create a solution through Microsoft Visual Studio 2008, add the fluroinefx service library to the website, and click File --- create --- project in sequence to set the solution, as shown in:

(6) After the fluorinefx service library is successfully added, the Project template automatically creates a sample class and an echo method for us, as shown below:

(7) Add a reference to the webpage. After adding a reference, the website will automatically reference the DLL of the fluorinefx service library.

 

 

3.2. Client Configuration

There are three methods to configure the client, one is to set parameters through the Wizard to create flex; the other is to set by specifying the services-config.xml configuration file; the third is the use of third-party components remoteobjectamf0 to connect, this method does not have to configure the services-config.xml.

(Recommended by the author: the configuration method is better, convenient and simple, and has been tested. Other two types have not been tested)

3.2.1. Wizard setting method:

(1) create a flex project.

(2) set the application server type to ASP. net, set the server to "use Internet Information Services (IIS)", and the Web application root directory points to the root path of the fluorinefx website, the Web application URL is set to the configuration path of the website in IIS. After configuring the URL according to the preceding steps, click "Verify Configuration" to verify the configuration. If the URL is verified, the result is: the root directory and URL of the Web application are valid. the configuration is correct, as shown in:

 

3.2.2.
Services-config.xml configuration file to set up

3.2.3.
Connect using third-party components remoteobjectamf0:

4. Communication Process

1. Compile a data processing class helloworld. CS file in vs. The code for the helloworld. CS file is as follows:

Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. htmlcontrols; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using fluorinefx; namespace fluorinehelloworld {// <summary> // Summary of helloworld /// </Summary> [remotingservice ()] public class helloworld {public helloworld () {// todo: Add the constructor logic here //} Public String fromfluorine (string username) {return "hello," + username + "! This message is from Fluorine flash remoting ";}}}

The remotingservice attribute is not required. However, when using this attribute, you can use console. aspx to view the remote service class file and call the Event code of the Service on the web application configured with the service browser. For example, you can set a breakpoint in the class file on the previous page, set console. aspx to the start page, and start the project. The page will jump to the fluorine. ASPX page. When you call the fromfluorine () function, it will be interrupted. Is the result of calling a function.

2. Configure the client and create a flex Project

<? Xmlversion = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "> <FX: SCRIPT> <! [CDATA [importmx. RPC. events. faultevent; importmx. RPC. events. resultevent; importmx. controls. alert; publicfunction clickbtnhelloworld (): void {sampleremoteobject.fromfluorine(this.txt helloworld. text);} publicfunction remoteresult (Re: resultevent): void {varstr: String = Re. result as string; alert. show (STR);} protectedfunctionsendremoteservice_faulthandler (Event: faultevent): void {// todo auto-generated method stub} publicfunction remotefault (Re: faultevent): void {alert. show ("message:" + RE. fault. faultstring, "error") ;}]]> </FX: SCRIPT> <FX: declarations> <s: remoteobject id = "sampleremoteobject" endpoint = ".. /gateway. aspx "Destination =" FLUORINE "Source =" fluorinehelloworld. helloworld "fault =" sendremoteservice_faulthandler (event) "> <s: method name =" fromfluorine "result =" remoteresult (event) "fault =" remotefault (Event) "/> </S: remoteobject> </FX: declarations> <mx: panel horizontalalign = "center" verticalalign = "Middle" width = "250" Height = "200" layout = "absolute"> <mx: textinput x = "35" Y = "10" id = "txthelloworld"/> <mx: button x = "82.5" Y = "40" label = "call" id = "btnhelloworld" Click = "clickbtnhelloworld ()"/> </MX: Panel> </s: application>

A remoteobject object is created and its ID attribute is set. "destination" points to the ID of "destination" in the services-config.xml. Source corresponds to vs class, And fluorinehelloworld is the namespace, the following helloworld is the class name. A Method method is created in remoteobject. The "name" attribute is the same as the function name in the vs class to be called. "result" sets the Processing Event After the returned result, "fault" sets the event to be processed after an error occurs during the interaction process.

3. Run flex and the result is as follows:

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.