Monday, October 02, 2006

asp.net C# data webservice for Flex

This is kinda the next evolutionary step from my previous blog. In this example I demonstrate how to get an array of records from .net to flex. Again, I have simplified this for the purpose of this blog. In the asp.net webservice, I would normally retrieve records from the database and then iterate thru the dataset to build my array, while here in this example I have just populated the array manually.

C# Webservice:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

namespace Director
{
public class MemberService : System.Web.Services.WebService
{
public MemberService() { InitializeComponent(); }
private IContainer components = null;
private void InitializeComponent() {}
protected override void Dispose( bool disposing ) {
if(disposing && components != null) {
components.Dispose();}
base.Dispose(disposing);
}

[WebMethod]
[XmlInclude(typeof(Member))]
public Member[] getMembers()
{
ArrayList al = new ArrayList();
Member mem = new Member(0,"bob","bsmith","password1",
"bob@abc.com","Initiate",false,DateTime.Now);
al.Add(mem);
Member mem1 = new Member(0,"jim","jsmith","password2",
"jim@abc.com","Member",false,DateTime.Now);
al.Add(mem1);
Member mem2 = new Member(0,"ed","esmith","password3",
"ed@abc.com","Officer",false,DateTime.Now);
al.Add(mem2);
Member mem3 = new Member(0,"neil","nsmith","password4",
"neil@abc.com","Guest",false,DateTime.Now);
al.Add(mem3);
Member[] outArray = (Member[])al.ToArray(typeof(Member));
return outArray;
}
}

[Serializable]
public class Member
{
public int memberid;
public String name;
public string username;
public string password;
public string email;
public string comments;
public bool disabled;
public DateTime created;

public Member(int _memberid, string _name,
string _username, string _password,
string _email, string _comments,
bool _disabled, DateTime _created)
{
memberid = _memberid;
name = _name;
username = _username;
password = _password;
email = _email;
comments = _comments;
disabled = _disabled;
created = _created;
}
public Member(){}
}
}

In Flex, the Member.as file:
package com.abc.ws
{
[Managed]
[RemoteClass(alias="com.abc.ws.Member")]
public class Member
{
public var memberid:int;
public var name:String;
public var username:String;
public var email:String;
public var password:String;
public var comments:String;
public var disabled:Boolean;
public var created:Date;

public function Member(obj:Object = null)
{
if (obj != null)
{
this.memberid = obj.memberid;
this.name = obj.name;
this.comments = obj.comments;
this.username = obj.username;
this.email = obj.email;
this.disabled = obj.disabled;
this.created = obj.created;
this.password = obj.password;
}
}
}
}
In Flex, the application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import com.abc.ws.Member;
import mx.collections.ArrayCollection;

[Bindable]
private var myMembers:ArrayCollection;

private function resultHandler(event:ResultEvent):void {
myMembers = event.result as ArrayCollection;
for (var i:int=0; i<myMembers.length; i++) {
var currentMember:Member = new Member();
currentMember = new Member(myMembers.getItemAt(i));
myMembers.setItemAt(currentMember, i);
}
}
]]>
</mx:Script>
<mx:WebService id="myService"
wsdl="http://localhost/Director/MemberService.asmx?wsdl"
load="myService.getMembers()"
showBusyCursor="true"
result="resultHandler(event)"/>

<mx:DataGrid dataProvider="{myMembers}"/>
</mx:Application>

10 comments:

Stephen Sinclair said...

Hi,

Good example, what if you turned on basic authentication and had to send credentials?
-Thanks, Steve
sinclas@sintechsoft.com

Anonymous said...

great article ! thanks !

Joel Lawler said...

Great stuff!!!

Thanks for posting this.

Anonymous said...

Cool posting, however, I'm getting an "mx.data:IManaged could not be found" and "mx.data.utils:Managed could not be found" errors. Each are displaying twice.

Any ideas as to the cause/solution?

Vic Rubba said...

the [Managed] tag on the class implies that you are using FDS or LCDS, either way you will need to include the fds.swc in your library path for the project. This library comes with the LCDS, available for download from Adobe. There's a free version for developers.

Anonymous said...

Vic,

Thanks for the reply... What if you don't have that installed? Is there a way around it? What are the benefits?

Vic Rubba said...

Way back when I did this stuff I was using FDS, since that was the easiest way at the time to hook Flex to Java. Since then Adobe has released BlazeDS, which is open source, and it has many of the great features that FDS had. [Managed] implies that you will be using DataService, which allows for push of data to the browser, among other things. You can remove the [Managed] and use RemoteObject calls instead with BlazeDS to get traditional asynchronous data pull functionality from Java. Or, if BlazeDS doesn't float your boat, you can use straight Webservice calls. However, AMF serialization that you get with Blaze is pretty sweet and worth looking into.

Anonymous said...

wow! after installing Adobe Datalife Data Cycle and include fds.swc file, the sample code works! thanks for this wonderful post. helpe me a lot.

cesar.qüeb said...

Nice, But... How to use the service when this is published?

TIA

Anonymous said...

Please write a emample to pass a complex object in webservices. Simple parameter works fine but complex one is not working.