Monday, October 02, 2006

asp.net c# webservice consumed by flex

Someone on the flexcoders asked to be provided with a simple example of flex consuming an asp.net webservice. I know this might be VERY simple, but it proves functionality.

ASP.NET Webservice in C#

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

namespace Director
{
public class Hello : System.Web.Services.WebService
{
public Hello()
{ InitializeComponent(); }

#region Component Designer generated code

private IContainer components = null;
private void InitializeComponent() { }

protected override void Dispose( bool disposing ) {
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion

[WebMethod]
public string HelloWorld(string variable) {
return "Hello " + variable;
}
}
}

Flex Application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:WebService id="myService"
wsdl="http://localhost/Director/Hello.asmx?WSDL"
load="myService.HelloWorld('Peter')"
showBusyCursor="true"/>

<mx:Label text="{myService.HelloWorld.lastResult}"/>
</mx:Application>

If you're accessing a webservice on another machine or domain, don't forget the crossdomain.xml file in the IIS root:
<?xml version="1.0"?>
<!-- http://www.foo.com/crossdomain.xml -->
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>

5 comments:

Anonymous said...

Is this for Flex Builder 2?

Victor Rubba said...

The Flex Application is done in Flex Builder 2. The .net part is done in Visual Studio 2003

Elton Bicalho do Carmo said...

Very useful example.

It will be very good with you write more about Flex and C# webservices.

I suggest you to leave a link with source code for download.

Keep the good work up.

Thanks to share you talent with us.

Anonymous said...

Easy and simply example. Thanks for the quick tutorial.

Alkan ARSLAN said...

Realy Easy and simply example. Thanks :)