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 /

Using Flash Builder 4 to build a Flex application that consumes a .NET-based web service written in C#

by Nishad Musthafa

Nishad Musthafa
  • nishadmusthafa.wordpress.com

Created

19 July 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
C# data-centric development Flash Builder 4 WSDL

Requirements

Prerequisite knowledge

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

User level

Beginning

Required products

  • Flash Builder 4 (Download trial)

Sample files

  • flashbuilder_webservice_dotnet.zip (299 KB)

This article describes how to use the data-centric development features of Flash Builder 4 to build a Flex application that accesses an SQL database created in Microsoft Visual Studio via a .NET-based web service. Web services are commonly used as a mechanism for data delivery on the Internet, and the .NET platform is widely used to implement web services. By following the steps in this tutorial, you will see how easy it is to retrieve data from a web service in a Flex application without writing any code.

Exposing the database through a .NET-based web service

The example database for this tutorial 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 for the Centre database table
Figure 1. Schema for the Centre database table

Creating the web service

To access the data from the Centre table, you can use the sample files included with this tutorial, which implement a simple web service in C#. The code consists of three files:

  • Centre.cs – This class is used to represent each tuple of the Centre table of the database.
  • Service.cs – This implements the System.Web.Services.WebService class and provides a set services (in the form of two function calls), which can be invoked from Flex. The getCentres() function retrieves all the tuples in the database and returns them as an array of Centre objects. The getCentreNames() function retrieves the list of CentreName attributes alone and returns them as an array of strings. The functions connect to the database using the System.Data.SqlClient namespace.
  • Service.asmx – This is the exposed web page that will run on the server. There is nothing in the code but a reference to the web service class Service.cs. You invoke this page to access the services.

Exposing the web service with WSDL

Before you can start using the web service functions from a Flex client, you need to know a little bit about Web Service Description Language (WSDL). WSDL is a standard XML-based language used to describe or model web services.

When you create a web services in Visual Studio, the WSDL is automatically generated. If the web service is deployed on a server, then the path of the WSDL is in the following format unless specified otherwise by the user:

http://{serverpath}/{context of the web application}/{Name of the services file- for example, services.asmx}?WSDL

While debugging in Visual Studio the default WSDL path for Service.asmx in the project named CentreServices is:

http://localhost:4682/CentreServices/Service.asmx?WSDL

This WSDL path will be used in Flash Builder 4 to connect to the service.

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

The code in Service.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.

Defining the web service in Flash Builder 4

After setting up the web service code and the database on your local server, start Flash Builder 4 and follow these steps to define the web service:

  1. Choose File > New > Flex Project to create a new project.
  2. Type a project name (for example, DotNetWebService).
  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 Web Service (see Figure 2).
Connecting to the web service
Figure 2. Connecting to the web service
  1. In the Specify A WSDL URL To Introspect dialog box, type the WSDL path in the WSDL URI text box (see Figure 3).

    The Service Name, Service Package, and Data Type Package are filled in automatically by Flash Builder 4. Since the name of the ASMX file is Service.asmx, Flash Builder 4 has given the Service Name the same value by default.

  2. Type CentreService as the Service Name to give your service a more descriptive name.
Entering the WSDL URI
Figure 3. Entering the WSDL URI
  1. Click Next.

    Flash Builder 4 will introspect the WSDL file and present a list of services, ports, and operations supported by the service.

  2. Make sure the two functions, getCentres() and getCentreNames() are selected.
  3. Click Finish.

In the Data/Services view you’ll see a list of the different operations and data types that were identified for the service (see Figure 4).

The Data/Services view
Figure 4. The Data/Services view

In just a few simple steps, you defined the .NET-based web service. Flash Builder 4 introspected the service, automatically generated value objects, and assigned the correct return types to the service’s operations.

Binding the data to the UI using data-centric development

Now that you’ve set up the service in Flash Builder 4, you’re ready to connect it to the user interface of your Flex application:

  1. In Flash Builder 4, switch to Design view.
  2. Drag a List control from the Components view and drop it in the main design area. You may want to expand the List control from its default size to accommodate the data in your database.
  3. Right-click the List control and select Bind To Data.
  4. In the Bind To Data dialog box, leave New Service Call selected, select CentreService as the Service, and select getCentreNames():String[] as the operation (see Figure 5).
  5. Click OK.
The Bind To Data dialog box
Figure 5. The Bind To Data dialog box

That’s it. Flash Builder 4 has generated all the code that’s needed to display the results of the service call in the List control.

  1. Run the application and you’ll see elements from the database in the list.Of course you’ll probably want to see more than just the Centre names.
  2. Drag a DataGrid control from the Components view and drop it in the main design area.
  3. This time drag the getCentres():Centre[] operation from the Data/Services view and drop it on the DataGrid control. (This is an alternative to selecting the control, right-clicking, and selecting Bind To Data.)
  4. Click OK in the Bind To Data dialog box.
  5. Run the program again and you’ll see that the entire table is shown.

The value object for the corresponding Centre class written in C# was automatically generated by Flash Builder 4. Here, the value object is essentially the same class written in ActionScript. To see the generated code, expand the valueObjects package in Package Explorer and open the Centre.as file. This ActionScript class can be reused for other purposes as a strongly typed class.

Where to go from here

Now that you have seen how easy it is to invoke a .NET-based web service from your Flex applications, you can use this approach to improve the richness of your web applications. With data-centric development you can even convert traditional web applications to Rich Internet Applications quickly and efficiently. For more information on data-centric development see the article Data-centric development with Flash Builder 4.

You could expand on this application in many ways. For example, you could use the data retrieved from the database to plot the location of offices (or even parcels) on a map. You can read more about how I developed a similar application using Google Maps on my blog.

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

More Like This

  • Building a Flex application that accesses an ASP.NET-based HTTP service with Flash Builder 4
  • Data paging with Flex and PHP using Flash Builder 4
  • Tool-based approaches for data-centric RIA development
  • Flash Builder 4 and PHP – Part 1: Data-centric development
  • Flash Builder 4 and PHP – Part 2: Zend AMF and Flash Remoting
  • Flash Builder 4 and PHP – Part 3: Implicit paging and data management
  • Using the Flash Builder 4 data-centric features with Parsley (and other frameworks)
  • Generating forms in Flash Builder 4
  • Working with Doctrine 2, Zend AMF, Flex, and Flash Builder
  • 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