Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center /

Building a Flex application that accesses an ASP.NET-based HTTP service with Flash Builder 4

by Nishad Musthafa

Nishad Musthafa
  • nishadmusthafa.wordpress.com

Content

Created

24 May 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ASP.NET C# data-centric development Flash Builder

Requirements

Prerequisite knowledge

Some experience with web programming using the .NET platform, as well as the ability to set up and use databases with SQL Server and ASP.NET is required. Prior experience with Flex will be helpful.

User level

Beginning

Required products

  • Flash Builder (Download trial)

Sample files

  • flashbuilder4_httpservice_dotnet.zip (312 KB)

Using the data-centric development capabilities of Flash Builder 4 you can easily build Flex applications that retrieve data from HTTP services (including ASP.NET-based services). With Flash Builder 4 you can also automatically generate value objects through introspection of the XML returned from the HTTP Service. These features let you take care of the back-end plumbing with ease (and without much coding), so you can concentrate on the development of the front end.

In this article I describe how to retrieve information from an SQL table via an HTTP service built on the Microsoft .NET framework. After defining the service model via introspection, you will bind a service operation to a Flex component to use the data in the Flex application.

Setting up the database and the .NET HTTP service

The example database for this tutorial, created using Visual Studio, is for an imaginary courier company that has stored the locations of its offices in the form of a table (named Centre) with the following columns and data types (see Figure 1):

  • Name - nchar(10)
  • Longitude - numeric(9,6)
  • Latitude - numeric(9,6)
Schema of the Centre database table
Figure 1. Schema of the Centre database table

To return the data from this table in XML format, you can use the sample files included with this tutorial. The code consists of three files:

  • Centre.cs – This C# class represents a data tuple from the Centre table of the database.
  • CentreInterface.cs – This C# class contains the method that establishes a connection to the database using the System.Data.SqlClient namespace and retrieves the list of Centre table tuples.
  • CentreXml.aspx – This is the ASPX page that returns the XML file containing the required data. Essentially, it returns the list of tuples in the database.

You can use the Database.mdf file, which is included among this tutorial's sample files, as the SQL Server database.

The code in CentreInterface.cs includes two path references that you'll need to change. Search for AttachDbFilename=/*ENTER PATH OF THE DB HERE*/ in the file and edit the two instances as necessary for your configuration. Refer to comments within the file to format the path properly.

After setting up the code and the database on your local server, you may want to access the ASPX file in your browser to verify that the code can access your data and is generating the XML response properly.

Defining the HTTP service in Flex

To create a Flex application that consumes the XML formatted data, follow these steps:

  1. In Flash Builder 4, Choose File > New > Flex Project.
  2. Type a project name (for example, I used DotNetHttpService).
  3. For the Application Type, select Web (Runs In Flash Player).
  4. For the Application Server Type, select None/Other.
  5. Click Finish.
  6. Choose Window > Data/Services to open the Data/Services view.
  7. In the Data/Services view, click Connect To Data/Service.
  8. In the Select Service Type dialog box, select HTTP (see Figure 2).
Selecting HTTP as the service type
Figure 2. Selecting HTTP as the service type
  1. In the Configure HTTP Service dialog box, select No for the option that asks if you want to use a base URL as a prefix for all operation URLs (see Figure 3).
HTTP Service Menu
Figure 3. HTTP Service Menu

If you have a lot of operations in your service, you may want to select Yes for this option. If your base URL changes, then you only have to update the address once for all operations that are accessible via that URL. For this example, however, there is only one operation so there is no need to specify a base URL.

  1. Click in the name column of the Operations grid and type GetCentres as the operation name.
  2. Select GET in the method column.
  3. In the URL column, type the URL to your ASPX page.
  4. For the Service Name type a descriptive name for the service; I used CentreService.
  5. Click Finish.

After Flash Builder 4 generates the service, you'll see it displayed in the Data/Services view (see Figure 4)

The CentreService service in the Data/Services view
Figure 4. The CentreService service in the Data/Services view

Configuring the return type

If you look at the GetCentres() function in the Data/Services view, you'll notice that its return type is a generic Object. Wouldn't it be nice if you had the same custom data type in Flex as you used on your back end?

With Flash Builder 4, you can automatically generate value objects, so your class on the Flex side is equivalent to your C# class on the .NET site.

Follow these steps to configure the return type of the operation:

  1. Right-click the GetCentres():Object in the Data/Services view and select Configure Return Type.
Configuring the return type
Figure 5. Configuring the return type
  1. Select Auto-detect The Return Type From Sample Data since this is the first time the data is going to be introspected (see Figure 5).

If your host requires authentication, then you can select Authentication Required and you be will prompted for a username and password.

You'd select the second option—Use An Existing Data Type—to use a built-in data type or to use a custom data type that you had previously configured.

  1. Click Next.
Auto-Detect Return type menu
Figure 6. Auto-Detect Return type menu

There are three options available for automatically detecting the return type. The first two require a functioning server. If the operation you require takes parameters, you can select Enter Parameter Values And Call The Operation, and then enter parameter values in the grid. Alternatively you can select Enter A Complete URL And Parameters And Get It, and then type the URL with a query string that contains the parameters. This is more convenient if you don't want to enter the values in the table and you already have the URL at hand.

If your back end has not been developed fully or the server is down, you can select Enter A Sample XML/JSON Response as long as you know the format of XML that will be returned and can supply a sample.

For example, on this project, you could supply the following XML, which is sample output from CentreXml.aspx:

<?xml version="1.0" encoding="UTF-8"?> <Centres> <Centre> <name>One</name> <longitude>29.777770000</longitude> <latitude>23.455666000</latitude> </Centre> <Centre> <name>Two</name> <longitude>54.777770000</longitude> <latitude>8.999999000</latitude> </Centre> </Centres>
  1. In this example there are no parameters, so simply select the first option and click Next.

Flash Builder 4 will call the operation and introspect the response, which in this case is an array of Centre objects (see Figure 7).

Return type detected successfully
Figure 7. Return type detected successfully
  1. Type Centre_type as the data type name, and select Centre as the root (see Figure 8).
Selecting the root property
Figure 8. Selecting the root property
  1. Click Finish (see Figure 9).
Menu after selecting the return type
Figure 9. Menu after selecting the return type

The return type for GetCentres() in theData/Services view will change to Centre_type[] (see Figure 10).

The configured return type of GetCentres()
Figure 10. The configured return type of GetCentres()

Binding the operation to the UI

Follow these steps to use the new service and display the information returned by it.

  1. In Flash Builder 4, switch to Design view.
  2. Drag a DataGrid control from the Components view and drop it in the main design area.
  3. Right-click the DataGrid control and select Bind To Data.
  4. Verify that the selected Service is CentreService and the Operation is GetCentres() (see Figure 11).
  5. Click OK.
The Bind To Data dialog box
Figure 11. The Bind To Data dialog box

The columns of the data grid will change to reflect the names of the properties of the custom type Centre (see Figure 12).

The DataGrid control with automatically generated column headings
Figure 12. The DataGrid control with automatically generated column headings

All that's left to do now is run the application.

  1. Choose Run > Run DotNetHttpService.

You will see the data grid populated with data from the database.

Where to go from here

You have seen how easy it is to connect a .NET HTTP service to your Flex front end using data-centric development. With no coding you configured the return type, automatically generated value objects, and wired up a data grid to display the results. If you are using an HTTP service as your back end, this streamlined workflow makes it easy to provide a rich and interactive application on the front end. For more information on data-centric development refer to the article Data-centric development with Flash Builder 4.

Further development of the application would depend on your business needs. You could, for instance, use the data from the HTTP service to build an application that customers can use to send their packages and track them online. You could add interactive maps and video chatting for customer service. With Flex as your front end, the possibilities are virtually endless.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

More Like This

  • Using Flash Builder 4 to build a Flex application that consumes a .NET-based web service written in C#
  • Working with Doctrine 2, Zend AMF, Flex, and Flash Builder
  • Flash Builder 4 and PHP – Part 2: Zend AMF and Flash Remoting
  • Flash Builder 4 and PHP – Part 3: Implicit paging and data management
  • Tool-based approaches for data-centric RIA development
  • Data paging with Flex and PHP using Flash Builder 4
  • Flash Builder 4 and PHP – Part 1: Data-centric development
  • Using the Flash Builder 4 data-centric features with Parsley (and other frameworks)
  • Generating forms in Flash Builder 4
  • What's new in Flash Builder 4

Tutorials & Samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex User Forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement